Skip to content

Commit

Permalink
fix: continued types work + fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Dec 19, 2023
1 parent 930071f commit bb8f6fa
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 55 deletions.
1 change: 0 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
}
},
"exclude": [
"test/*",
"node_modules",
"dist"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"analyze": "cem analyze --litelement",
"lint": "eslint .",
"test:mocha": "c8 mocha",
"test:wtr": "wtr \"./test/wtr/*.test.js\"",
"test:wtr": "wtr \"./test/wtr/**/*.test.js\"",
"test:wtr:watch": "npm run test:wtr -- --watch",
"test": "npm run test:mocha && npm run test:wtr",
"docs": "npx jsdoc2md -c .jsdoc.json --files './src/*.js' > docs/API.md",
Expand Down
15 changes: 10 additions & 5 deletions src/extension/app/store/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ import { fetchLanguageDict } from '../utils/i18n.js';
import { getLocation, matchProjectHost, isSupportedFileExtension } from '../utils/browser.js';

/**
* The plugins
* The sidekick configuration object type
* @typedef {import('@Types').SidekickOptionsConfig} SidekickOptionsConfig
*/

/**
* The plugin object type
* @typedef {import('@Types').Plugin} Plugin
*/

class AppStore {
export class AppStore {
// eslint-disable-next-line no-undef
@observable accessor initialized = false;

Expand Down Expand Up @@ -63,14 +68,14 @@ class AppStore {
/**
* Loads the sidekick configuration and language dictionary,
* and retrieves the location of the current document.
* @param {SiteStore} cfg The sidekick config
* @param {SidekickOptionsConfig} inputConfig The sidekick config
* @fires Sidekick#contextloaded
*/
async loadContext(sidekick, cfg) {
async loadContext(sidekick, inputConfig) {
this.sidekick = sidekick;
this.location = getLocation();

await this.siteStore.initStore(cfg);
await this.siteStore.initStore(inputConfig);

// load dictionary based on user language
this.languageDict = await fetchLanguageDict(this.siteStore);
Expand Down
36 changes: 27 additions & 9 deletions src/extension/app/store/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
import { getAdminUrl, getAdminFetchOptions } from '../utils/helix-admin.js';
import { getLanguage, i18n } from '../utils/i18n.js';

/**
* The sidekick options configuration object type
* @typedef {import('@Types').SidekickOptionsConfig} SidekickOptionsConfig
*/

/**
* The sidekick configuration object type
* @typedef {import('@Types').SidekickConfig} SidekickConfig
*/

/**
* @typedef {import('./app.js').AppStore} AppStore
*/

export class SiteStore {
/**
* The GitHub owner or organization (mandatory)
Expand Down Expand Up @@ -86,12 +100,6 @@ export class SiteStore {
*/
stdOuterHost;

/**
* If the production host is a 3rd party CDN
* @type {boolean}
*/
byocdn;

/**
* Loads configuration and plugins from the development environment
* @type {boolean}
Expand Down Expand Up @@ -128,17 +136,24 @@ export class SiteStore {
*/
views;

/**
* @param {AppStore} appStore
*/
constructor(appStore) {
this.appStore = appStore;
}

/**
* Initializes the site store
* @param {SidekickOptionsConfig} cfg
*/
async initStore(cfg) {
let config = cfg || (window.hlx && window.hlx.sidekickConfig) || {};
const {
owner,
repo,
ref = 'main',
mountpoint,
mountpoints,
devMode,
adminVersion,
_extended,
Expand Down Expand Up @@ -206,7 +221,7 @@ export class SiteStore {
this.owner = owner;
this.repo = repo;
this.ref = ref;
this.mountpoint = mountpoint;
[this.mountpoint] = mountpoints || [];
this.devMode = devMode;
this.adminVersion = adminVersion;
this._extended = _extended;
Expand All @@ -229,6 +244,10 @@ export class SiteStore {
this.appStore.initialized = true;
}

/**
* Serializes the store to JSON
* @returns { SidekickConfig }
*/
toJSON() {
return {
owner: this.owner,
Expand All @@ -243,7 +262,6 @@ export class SiteStore {
liveHost: this.liveHost,
outerHost: this.outerHost,
stdOuterHost: this.stdOuterHost,
byocdn: this.byocdn,
devMode: this.devMode,
devOrigin: this.devOrigin,
adminVersion: this.adminVersion,
Expand Down
4 changes: 2 additions & 2 deletions src/extension/app/utils/helix-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
*/

/**
* @typedef {import('../store/site.js').SiteStore} SiteStore
* @typedef {import('@Types').SidekickConfig} SidekickConfig
*/

/**
* Creates an Admin URL for an API and path.
* @private
* @param {SiteStore} siteStore The site store
* @param {SidekickConfig} sidekickConfig The sidekick config object
* @param {string} api The API endpoint to call
* @param {string} path The current path
* @returns {URL} The admin URL
Expand Down
8 changes: 4 additions & 4 deletions src/extension/app/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

/**
* @typedef {import('@SiteStore').SiteStore} SiteStore
* @typedef {import('@Types').SidekickConfig} SidekickConfig
*/

/**
Expand Down Expand Up @@ -64,13 +64,13 @@ export function getLanguage() {
/**
* Fetches the dictionary for a language.
* @private
* @param {SiteStore} siteStore The site config
* @param {SidekickConfig} sidekickConfig The site config
* @param {string} [lang] The language
* @returns {Promise<object>} The dictionary
*/
export async function fetchLanguageDict(siteStore, lang) {
export async function fetchLanguageDict(sidekickConfig, lang) {
const dict = {};
const dictPath = `${siteStore.scriptRoot}/_locales/${lang || siteStore.lang}/messages.json`;
const dictPath = `${sidekickConfig.scriptRoot}/_locales/${lang || sidekickConfig.lang}/messages.json`;

Check warning on line 73 in src/extension/app/utils/i18n.js

View check run for this annotation

Codecov / codecov/patch

src/extension/app/utils/i18n.js#L71-L73

Added lines #L71 - L73 were not covered by tests
try {
const res = await fetch(dictPath);
const messages = await res.json();
Expand Down
1 change: 1 addition & 0 deletions src/extension/app/utils/rum.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default function sampleRUM(checkpoint, data = {}) {
return true;
},
};
// @ts-ignore
sendPing(data);
if (sampleRUM.cases[checkpoint]) {
sampleRUM.cases[checkpoint]();
Expand Down
4 changes: 1 addition & 3 deletions src/extension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@
'host',
'devMode',
'devOrigin',
'pushDown',
'adminVersion',
'authTokenExpiry',
'hlx5',
].includes(k)));
curatedConfig.scriptUrl = getExtensionURL('module.js');
[curatedConfig.mountpoint] = config.mountpoints || [];
curatedConfig.scriptUrl = getExtensionURL('index.js');

sidekick = new AEMSidekick(curatedConfig);
sidekick.setAttribute('open', display);
Expand Down
4 changes: 2 additions & 2 deletions src/extension/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ export function assembleProject({
* @param {Object} config The config
* @param {string} config.owner The owner
* @param {string} config.repo The repository
* @param {string} config.ref=main The ref or branch
* @param {string} config.authToken The auth token
* @param {string} [config.ref] The ref or branch (default: 'main')
* @param {string} [config.authToken] The auth token
* @returns {Promise<Object>} The project environment
*/
export async function getProjectEnv({
Expand Down
51 changes: 25 additions & 26 deletions src/extension/types/typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,77 +13,76 @@
/* eslint-disable max-len */

/**
* @typedef {Object} SidekickOptionsUserConfig
* @prop {string} [project] The name of the project used in the sharing link (optional)
* @prop {string} [giturl] The url to the repository (optional)
* @prop {string} [mountpoint] The content source URL (optional)
* @prop {string} [previewHost] The host name of a custom preview CDN (optional)
* @prop {string} [liveHost] The host name of a custom live CDN (optional)
* @prop {string} [host] The production host name to publish content to (optional)
* @typedef {Object} OptionsConfig
* @prop {string} [project] The name of the project used in the sharing link
* @prop {string} [giturl] The url to the repository
* @prop {string[]} [mountpoints] The content source URL
* @prop {string} [previewHost] The host name of a custom preview CDN
* @prop {string} [liveHost] The host name of a custom live CDN
* @prop {string} [host] The production host name to publish content to
* @prop {boolean} [devMode] Loads configuration and plugins from the development environment
* @prop {boolean} [devOrigin] URL of the local development environment
* @prop {string} [devOrigin] URL of the local development environment
* @prop {string} [adminVersion] The specific version of admin service to use
* @prop {boolean} [hlx5] Using helix 5?
* @prop {boolean} [disabled] Is the project disabled?
* @description The sidekick configuration from the user via the options view
* @description Represents the sidekick configuration from the user via the options view
*/

/**
* @private
* @typedef {Object} SidekickOptionsDerivedConfig
* @prop {string} id The project id
* @typedef {Object} OptionsDerivedConfig
* @prop {string} [id] The project id
* @prop {string} owner The GitHub owner or organization
* @prop {string} repo The GitHub owner or organization
* @prop {string} repo The GitHub repo
* @prop {string} ref The Git reference or branch
* @prop {string} [authToken] The Git reference or branch
* @description The derived sidekick configuration from options.
*/

/**
* @typedef {SidekickOptionsUserConfig & SidekickOptionsDerivedConfig } SidekickOptionsConfig
* @typedef {OptionsConfig & OptionsDerivedConfig } SidekickOptionsConfig
*/

/**
* @typedef {Object} SidekickRepositoryConfig
* @typedef {Object} RepositoryConfig
* @prop {string} [project] The name of the project used in the sharing link (optional)
* @prop {string} [host] The production host name to publish content to (optional)
* @prop {Plugin[]} [plugins] An array of {@link Plugin|plugin configurations} (optional)
* @prop {ViewConfig[]} [specialViews] An array of custom {@link ViewConfig|view configurations}
* @description The sidekick configuration JSON.
* @description The configuration file from the respository `config.json`.
*/

/**
* @private
* @typedef {Object} SidekickDerivedConfig
* @prop {string} [mountpoint] The content source URL
* @prop {string} [innerHost] The host name of a custom preview CDN (optional)
* @prop {string} [stdInnerHost] The host name of a custom live CDN (optional)
* @prop {boolean} [outerHost] Loads configuration and plugins from the development environment
* @prop {boolean} [stdOuterHost] URL of the local development environment
* @prop {string} [outerHost] Loads configuration and plugins from the development environment
* @prop {string} [stdOuterHost] URL of the local development environment
* @prop {string} [scriptRoot] URL of the local development environment
* @prop {string} [lang] URL of the local development environment
* @prop {ViewConfig[]} [views] An array of custom {@link ViewConfig|view configurations}
* @description The derived sidekick configuration after loadContext.
*/

/**
* @typedef {SidekickOptionsConfig & SidekickRepositoryConfig & SidekickDerivedConfig} SidekickConfig
* @typedef {SidekickOptionsConfig & SidekickDerivedConfig} SidekickConfig
*/

/**
* @typedef {Object} Plugin
* @prop {string} id The plugin ID (mandatory)
* @prop {string} title The button text
* @prop {Object} titleI18n={} A map of translated button texts
* @prop {Object} titleI18n A map of translated button texts (default: {})
* @prop {string} url The URL to open when the button is clicked
* @prop {boolean} passConfig Append additional sk info to the url as query parameters:
* ref, repo, owner, host, project
* @prop {boolean} passReferrer Append the referrer URL as a query param on new URL button click
* @prop {string} event The name of a custom event to fire when the button is clicked.
* Note: Plugin events get a custom: prefix, e.g. "foo" becomes "custom:foo".
* @prop {boolean} passConfig Append additional sk info to the url as query parameters: ref, repo, owner, host, project
* @prop {boolean} passReferrer Append the referrer URL as a query param on new URL button click. Note: Plugin events get a custom: prefix, e.g. "foo" becomes "custom:foo".
* @prop {string} containerId The ID of a dropdown to add this plugin to (optional)
* @prop {boolean} isContainer Determines whether to turn this plugin into a dropdown
* @prop {boolean} isPalette Determines whether a URL is opened in a palette instead of a new tab
* @prop {string} paletteRect The dimensions and position of a palette (optional)
* @prop {string[]} environments Specifies when to show this plugin
* (admin, edit, dev, preview, live, prod)
* @prop {string[]} environments Specifies when to show this plugin (admin, edit, dev, preview, live, prod)
* @prop {string[]} excludePaths Exclude the plugin from these paths (glob patterns supported)
* @prop {string[]} includePaths Include the plugin on these paths (glob patterns supported)
* @description The plugin configuration.
Expand Down
38 changes: 38 additions & 0 deletions test/wtr/app/store/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-disable no-unused-expressions */

import { expect } from '@open-wc/testing';
import { appStore } from '../../../../src/extension/app/store/app.js';

/**
* The plugins
* @typedef {import('@Types').SidekickOptionsConfig} SidekickOptionsConfig
*/

describe('Test App Store', () => {
/**
* @type {SidekickOptionsConfig}
*/
const config = {
owner: 'adobe',
ref: 'main',
repo: 'aem-boilerplate',
mountpoints: ['https://drive.google.com/drive/folders/13zVgbn8VVfEhWuAnKhSvm5n_LcKHp1Uf'],
};

it('loadContext', async () => {
const testElement = document.createElement('div');
await appStore.loadContext(testElement, config);
expect(appStore.siteStore.repo).to.equal('aem-boilerplate');
});
});
2 changes: 2 additions & 0 deletions test/wtr/check-tab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import sinon from 'sinon';
import chromeMock from './mocks/chrome.js';
import checkTab from '../../src/extension/check-tab.js';

// @ts-ignore
window.chrome = chromeMock;

describe('Test checkTab', () => {
Expand All @@ -35,6 +36,7 @@ describe('Test checkTab', () => {
const executeScriptSpy = sandbox.spy(chrome.scripting, 'executeScript');
const sendMessageSpy = sandbox.spy(chrome.tabs, 'sendMessage');
// check tab with invalid URL
// @ts-ignore
await checkTab();
expect(executeScriptSpy.callCount).to.equal(0);
// check tab with URL from configured project
Expand Down
Loading

0 comments on commit bb8f6fa

Please sign in to comment.