Skip to content

Commit

Permalink
fix: expose validated config on window object
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Apr 5, 2024
1 parent 4fbf3ba commit aa6bfeb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
2 changes: 2 additions & 0 deletions apps/delivery-options/src/config/setConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export const setConfiguration = (config: InputDeliveryOptionsConfiguration): voi
addressStore.$patch(validated.address);

language.setStrings(validated.strings);

window.MyParcelConfig = validated;
};
53 changes: 30 additions & 23 deletions apps/delivery-options/src/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import {afterEach, beforeEach, describe, expect, it, type MockInstance, vi} from 'vitest';
import {KEY_CONFIG, KEY_STRINGS, KEY_CARRIER_SETTINGS, CarrierSetting, KEY_ADDRESS} from '@myparcel-do/shared';
import {CarrierName} from '@myparcel/constants';
import {RENDER_DELIVERY_OPTIONS, UPDATE_DELIVERY_OPTIONS} from './data';
import {createDiv, dispatchEvent} from './__tests__';
import {createDiv, dispatchEvent, getMockDeliveryOptionsConfiguration} from './__tests__';

describe('main', () => {
const unmountSpy: MockInstance = vi.fn();
const mountSpy: MockInstance = vi.fn();

beforeEach(async () => {
mountSpy.mockImplementation((selector: string): void => {
const element = document.querySelector<HTMLDivElement>(selector)!;

element.__vue_app__ = {unmount: unmountSpy};
});

vi.doMock('vue', async (importOriginal) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const actual = await importOriginal<typeof import('vue')>();

return {
...actual,
createApp: () => ({
use: vi.fn(),
mount: mountSpy,
unmount: unmountSpy,
}),
};
});
document.body.innerHTML = '';

await import('./main');

Expand All @@ -45,10 +29,33 @@ describe('main', () => {
expect.assertions(2);

await dispatchEvent(UPDATE_DELIVERY_OPTIONS, detail);
expect(mountSpy).toHaveBeenCalledTimes(1);

await dispatchEvent(UPDATE_DELIVERY_OPTIONS, detail);
expect(mountSpy).toHaveBeenCalledTimes(1);

const selector = detail?.selector ?? '#myparcel-delivery-options';

expect(document.querySelectorAll(selector)).toHaveLength(1);
expect(document.querySelector(selector)?.hasAttribute('data-v-app')).toBeTruthy();
});

it('exposes config on window object after booting', async () => {
expect.assertions(2);

await dispatchEvent(
UPDATE_DELIVERY_OPTIONS,
getMockDeliveryOptionsConfiguration({
[KEY_CONFIG]: {
[KEY_CARRIER_SETTINGS]: {
[CarrierName.PostNl]: {
[CarrierSetting.AllowDeliveryOptions]: true,
[CarrierSetting.AllowStandardDelivery]: true,
},
},
},
}),
);

expect(global.window.MyParcelConfig).toBeDefined();
expect(Object.keys(global.window.MyParcelConfig)).toEqual([KEY_ADDRESS, KEY_CONFIG, KEY_STRINGS]);
});

it.todo.each([
Expand Down
4 changes: 2 additions & 2 deletions apps/delivery-options/src/types/global.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {type InputDeliveryOptionsConfiguration} from '@myparcel-do/shared';
import {type InputDeliveryOptionsConfiguration, type DeliveryOptionsConfiguration} from '@myparcel-do/shared';

declare global {
export interface Window {
MyParcelConfig: InputDeliveryOptionsConfiguration;
MyParcelConfig: InputDeliveryOptionsConfiguration | DeliveryOptionsConfiguration;

Check warning on line 5 in apps/delivery-options/src/types/global.types.ts

View check run for this annotation

Codecov / codecov/patch

apps/delivery-options/src/types/global.types.ts#L5

Added line #L5 was not covered by tests
}
}

Expand Down

0 comments on commit aa6bfeb

Please sign in to comment.