Skip to content

Commit

Permalink
fix: rework environment mocks, organize fixtures and mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Jan 25, 2024
1 parent 2314e7a commit 5ccd9d8
Show file tree
Hide file tree
Showing 19 changed files with 654 additions and 490 deletions.
2 changes: 0 additions & 2 deletions src/extension/app/store/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ export class SiteStore {
this.project = project;
this.devUrl = devUrl;
this.lang = lang || getLanguage();

this.appStore.initialized = true;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/wtr/aem-config-picker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import fetchMock from 'fetch-mock/esm/client.js';
import sinon from 'sinon';
import { expect, waitUntil } from '@open-wc/testing';
import chromeMock from './mocks/chrome.js';
import { mockFetchEnglishMessagesSuccess } from './fixtures/i18n.js';
import { mockFetchEnglishMessagesSuccess } from './mocks/i18n.js';
import { AEMConfigPicker } from '../../src/extension/app/config-picker.js';
import { matchingConfigs } from './fixtures/stubs/sidekick-config.js';
import { matchingConfigs } from './fixtures/sidekick-config.js';
import '../../src/extension/index.js';
import { recursiveQuery, recursiveQueryAll } from './test-utils.js';

Expand Down
14 changes: 8 additions & 6 deletions test/wtr/aem-sidekick.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ import { emulateMedia } from '@web/test-runner-commands';
import { recursiveQuery } from './test-utils.js';
import chromeMock from './mocks/chrome.js';
import { AEMSidekick } from '../../src/extension/app/aem-sidekick.js';
import { mockFetchEnglishMessagesSuccess } from './fixtures/i18n.js';
import { defaultSidekickConfig } from './fixtures/stubs/sidekick-config.js';
import { mockFetchConfigJSONNotFound, mockFetchStatusSuccess } from './fixtures/helix-admin.js';
import { mockFetchEnglishMessagesSuccess } from './mocks/i18n.js';
import { defaultSidekickConfig } from './fixtures/sidekick-config.js';
import { mockFetchConfigJSONNotFound, mockFetchStatusSuccess } from './mocks/helix-admin.js';
import '../../src/extension/index.js';
import { mockHelixEnvironment, restoreEnvironment } from './mocks/environment.js';

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

describe('AEM Sidekick', () => {
beforeEach(async () => {
mockFetchEnglishMessagesSuccess();
mockFetchStatusSuccess();
mockHelixEnvironment(document, 'inner');
});

afterEach(() => {
fetchMock.restore();
restoreEnvironment(document);
});

it('renders theme and action-bar', async () => {
Expand Down Expand Up @@ -77,7 +81,6 @@ describe('AEM Sidekick', () => {

describe('configuration loading', () => {
it('default config', async () => {
mockFetchStatusSuccess();
mockFetchConfigJSONNotFound();
const sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -96,13 +99,12 @@ describe('AEM Sidekick', () => {
expect(data.config.views.length).to.eq(1);

expect(data.location).to.exist;
expect(data.location.host).to.eq('localhost:2000');
expect(data.location.host).to.eq('main--aem-boilerplate--adobe.hlx.page');
});
});
});

it('passes the a11y audit', async () => {
mockFetchStatusSuccess();
mockFetchConfigJSONNotFound();
const sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand Down
2 changes: 1 addition & 1 deletion test/wtr/app/components/modal/modal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { EVENTS, MODALS } from '../../../../../src/extension/app/constants.js';
import chromeMock from '../../../mocks/chrome.js';
import '../../../../../src/extension/index.js';
import { recursiveQuery } from '../../../test-utils.js';
import { mockFetchEnglishMessagesSuccess } from '../../../fixtures/i18n.js';
import { mockFetchEnglishMessagesSuccess } from '../../../mocks/i18n.js';
import { appStore } from '../../../../../src/extension/app/store/app.js';
import { fetchLanguageDict } from '../../../../../src/extension/app/utils/i18n.js';

Expand Down
10 changes: 5 additions & 5 deletions test/wtr/app/components/plugin/env-switcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import { expect, waitUntil } from '@open-wc/testing';
import { recursiveQuery } from '../../../test-utils.js';
import chromeMock from '../../../mocks/chrome.js';
import { AEMSidekick } from '../../../../../src/extension/app/aem-sidekick.js';
import { mockFetchEnglishMessagesSuccess } from '../../../fixtures/i18n.js';
import { defaultSidekickConfig } from '../../../fixtures/stubs/sidekick-config.js';
import { mockFetchEnglishMessagesSuccess } from '../../../mocks/i18n.js';
import { defaultSidekickConfig } from '../../../fixtures/sidekick-config.js';
import {
mockFetchConfigJSONNotFound,
mockFetchStatusSuccess,
} from '../../../fixtures/helix-admin.js';
} from '../../../mocks/helix-admin.js';
import '../../../../../src/extension/index.js';
import { appStore } from '../../../../../src/extension/app/store/app.js';
import { mockEnvironment, restoreEnvironment } from '../../../mocks/environment.js';
import { mockHelixEnvironment, restoreEnvironment } from '../../../mocks/environment.js';

// @ts-ignore
window.chrome = chromeMock;
Expand All @@ -47,7 +47,7 @@ describe('Environment Switcher', () => {
it('change environment - inner -> live', async () => {
mockFetchStatusSuccess();
mockFetchConfigJSONNotFound();
mockEnvironment(document, 'inner');
mockHelixEnvironment(document, 'inner');

const switchEnvStub = sinon.stub(appStore, 'switchEnv').resolves();

Expand Down
22 changes: 11 additions & 11 deletions test/wtr/app/components/plugin/palette-container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import { expect, waitUntil } from '@open-wc/testing';
import { recursiveQuery, recursiveQueryAll } from '../../../test-utils.js';
import chromeMock from '../../../mocks/chrome.js';
import { AEMSidekick } from '../../../../../src/extension/app/aem-sidekick.js';
import { mockFetchEnglishMessagesSuccess } from '../../../fixtures/i18n.js';
import { defaultSidekickConfig } from '../../../fixtures/stubs/sidekick-config.js';
import { mockFetchEnglishMessagesSuccess } from '../../../mocks/i18n.js';
import { defaultSidekickConfig } from '../../../fixtures/sidekick-config.js';
import {
mockEditorFetchStatusSuccess,
mockSharepointEditorFetchStatusSuccess,
mockFetchConfigWithPluginsJSONSuccess,
} from '../../../fixtures/helix-admin.js';
} from '../../../mocks/helix-admin.js';
import '../../../../../src/extension/index.js';
import { mockEnvironment, restoreEnvironment } from '../../../mocks/environment.js';
import { mockEditorAdminEnvironment, restoreEnvironment } from '../../../mocks/environment.js';

// @ts-ignore
window.chrome = chromeMock;
Expand Down Expand Up @@ -63,9 +63,9 @@ describe('Palette container', () => {
}

it('closes palette plugin via close button', async () => {
mockEditorFetchStatusSuccess();
mockSharepointEditorFetchStatusSuccess();
mockFetchConfigWithPluginsJSONSuccess();
mockEnvironment(document, 'editor');
mockEditorAdminEnvironment(document, 'editor');

await openPallete();

Expand All @@ -80,9 +80,9 @@ describe('Palette container', () => {
});

it('closes palette plugin via esc key', async () => {
mockEditorFetchStatusSuccess();
mockSharepointEditorFetchStatusSuccess();
mockFetchConfigWithPluginsJSONSuccess();
mockEnvironment(document, 'editor');
mockEditorAdminEnvironment(document, 'editor');

await openPallete();

Expand All @@ -99,9 +99,9 @@ describe('Palette container', () => {
});

it('palette renders titleI18n', async () => {
mockEditorFetchStatusSuccess();
mockSharepointEditorFetchStatusSuccess();
mockFetchConfigWithPluginsJSONSuccess();
mockEnvironment(document, 'editor');
mockEditorAdminEnvironment(document, 'editor');

await openPallete('localize');

Expand Down
76 changes: 48 additions & 28 deletions test/wtr/app/components/plugin/plugin-action-bar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ import sinon from 'sinon';
import { recursiveQuery, recursiveQueryAll } from '../../../test-utils.js';
import chromeMock from '../../../mocks/chrome.js';
import { AEMSidekick } from '../../../../../src/extension/app/aem-sidekick.js';
import { mockFetchEnglishMessagesSuccess } from '../../../fixtures/i18n.js';
import { defaultSidekickConfig } from '../../../fixtures/stubs/sidekick-config.js';
import { mockFetchEnglishMessagesSuccess } from '../../../mocks/i18n.js';
import { defaultSidekickConfig } from '../../../fixtures/sidekick-config.js';
import {
mockDirectoryFetchStatusSuccess,
mockEditorFetchStatusSuccess,
mockSharepointDirectoryFetchStatusSuccess,
mockSharepointEditorFetchStatusSuccess,
mockFetchConfigJSONNotFound,
mockFetchConfigWithPluginsJSONSuccess,
mockFetchConfigWithoutPluginsJSONSuccess,
mockFetchStatusSuccess,
} from '../../../fixtures/helix-admin.js';
} from '../../../mocks/helix-admin.js';
import '../../../../../src/extension/index.js';
import { mockEnvironment, restoreEnvironment } from '../../../mocks/environment.js';
import { mockHelixEnvironment, mockEditorAdminEnvironment, restoreEnvironment } from '../../../mocks/environment.js';
import { EXTERNAL_EVENTS } from '../../../../../src/extension/app/constants.js';
import { pluginFactory } from '../../../../../src/extension/app/plugins/plugin-factory.js';

// @ts-ignore
window.chrome = chromeMock;
Expand Down Expand Up @@ -80,7 +81,7 @@ describe('Plugin action bar', () => {
it('isInner', async () => {
mockFetchStatusSuccess();
mockFetchConfigJSONNotFound();
mockEnvironment(document, 'inner');
mockHelixEnvironment(document, 'inner');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -95,10 +96,11 @@ describe('Plugin action bar', () => {
expectPlugin('.publish');
});

it('isInner - w/custom plugins', async () => {
it('editor - w/custom plugins', async () => {
mockFetchStatusSuccess();
mockFetchConfigWithPluginsJSONSuccess();
mockEnvironment(document, 'editor');
mockSharepointEditorFetchStatusSuccess();
mockEditorAdminEnvironment(document, 'editor');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -107,7 +109,7 @@ describe('Plugin action bar', () => {

expectPluginCount(5);

expectEnvPlugin(['preview', 'live']);
expectEnvPlugin(['preview', 'live', 'prod']);

expectPlugin('env-switcher');
expectPlugin('.edit-preview');
Expand All @@ -123,7 +125,7 @@ describe('Plugin action bar', () => {
it('isOuter', async () => {
mockFetchStatusSuccess();
mockFetchConfigJSONNotFound();
mockEnvironment(document, 'outer');
mockHelixEnvironment(document, 'outer');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -141,7 +143,7 @@ describe('Plugin action bar', () => {
it('isProd', async () => {
mockFetchStatusSuccess();
mockFetchConfigJSONNotFound();
mockEnvironment(document, 'prod');
mockHelixEnvironment(document, 'prod');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -157,9 +159,9 @@ describe('Plugin action bar', () => {
});

it('isEditor', async () => {
mockEditorFetchStatusSuccess();
mockSharepointEditorFetchStatusSuccess();
mockFetchConfigJSONNotFound();
mockEnvironment(document, 'editor');
mockEditorAdminEnvironment(document, 'editor');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -177,9 +179,9 @@ describe('Plugin action bar', () => {
});

it('isEditor - custom config with prod host', async () => {
mockEditorFetchStatusSuccess();
mockSharepointEditorFetchStatusSuccess();
mockFetchConfigWithoutPluginsJSONSuccess();
mockEnvironment(document, 'editor');
mockEditorAdminEnvironment(document, 'editor');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -199,7 +201,7 @@ describe('Plugin action bar', () => {
it('isInner - custom config with prod host', async () => {
mockFetchStatusSuccess();
mockFetchConfigWithoutPluginsJSONSuccess();
mockEnvironment(document, 'inner');
mockHelixEnvironment(document, 'inner');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -215,9 +217,25 @@ describe('Plugin action bar', () => {
});

it('core plugin clicked', async () => {
window.hlx = {};

const actionFunction = async () => {};

// Create a spy for the action function
const actionSpy = sinon.spy(actionFunction);

const stub = sinon.stub(pluginFactory, 'createPublishPlugin').returns({
id: 'publish',
condition: () => true,
button: {
text: 'Publish',
action: actionSpy,
},
});

mockFetchStatusSuccess();
mockFetchConfigWithoutPluginsJSONSuccess();
mockEnvironment(document, 'inner');
mockHelixEnvironment(document, 'inner');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -230,16 +248,16 @@ describe('Plugin action bar', () => {

const publishButton = recursiveQuery(sidekick, '.publish');
publishButton.dispatchEvent(new Event('click'));
await waitUntil(() => recursiveQuery(sidekick, 'dialog-view'));

const dialogView = recursiveQuery(sidekick, 'dialog-view');
expect(dialogView).to.exist;
expect(actionSpy.calledOnce).to.be.true;

stub.restore();
});

it('isAdmin', async () => {
mockDirectoryFetchStatusSuccess();
it('isAdmin - loads correct plugins', async () => {
mockSharepointEditorFetchStatusSuccess();
mockFetchConfigJSONNotFound();
mockEnvironment(document, 'admin');
mockEditorAdminEnvironment(document, 'admin');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -255,9 +273,10 @@ describe('Plugin action bar', () => {
});

it('custom container plugin', async () => {
mockEditorFetchStatusSuccess();
mockSharepointEditorFetchStatusSuccess();
mockSharepointDirectoryFetchStatusSuccess();
mockFetchConfigWithPluginsJSONSuccess();
mockEnvironment(document, 'editor');
mockEditorAdminEnvironment(document, 'editor');

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);
Expand All @@ -275,9 +294,10 @@ describe('Plugin action bar', () => {
});

it('clicks custom plugin', async () => {
mockEditorFetchStatusSuccess();
mockSharepointEditorFetchStatusSuccess();
mockSharepointDirectoryFetchStatusSuccess();
mockFetchConfigWithPluginsJSONSuccess();
mockEnvironment(document, 'editor');
mockEditorAdminEnvironment(document, 'editor');

const openStub = sinon.stub(window, 'open');
const pluginUsedEventSpy = sinon.spy();
Expand Down
6 changes: 3 additions & 3 deletions test/wtr/app/store/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import {
mockFetchStatusServerError,
mockFetchStatusSuccess,
mockFetchStatusUnauthorized,
} from '../../fixtures/helix-admin.js';
import { mockFetchEnglishMessagesSuccess } from '../../fixtures/i18n.js';
import { defaultSidekickConfig } from '../../fixtures/stubs/sidekick-config.js';
} from '../../mocks/helix-admin.js';
import { mockFetchEnglishMessagesSuccess } from '../../mocks/i18n.js';
import { defaultSidekickConfig } from '../../fixtures/sidekick-config.js';
import { EventBus } from '../../../../src/extension/app/utils/event-bus.js';
import { EVENTS, MODALS } from '../../../../src/extension/app/constants.js';

Expand Down
4 changes: 2 additions & 2 deletions test/wtr/app/store/site.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
mockFetchConfigWithPluginsJSONSuccess,
mockFetchLocalConfigJSONSuccess,
mockFetchStatusSuccess,
} from '../../fixtures/helix-admin.js';
import { mockFetchEnglishMessagesSuccess } from '../../fixtures/i18n.js';
} from '../../mocks/helix-admin.js';
import { mockFetchEnglishMessagesSuccess } from '../../mocks/i18n.js';

// @ts-ignore
window.chrome = chromeMock;
Expand Down
Loading

0 comments on commit 5ccd9d8

Please sign in to comment.