Skip to content

Commit

Permalink
add tests + improve getEngineEntryOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
zetmate committed Jun 24, 2024
1 parent 132bca6 commit 035cf22
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
30 changes: 15 additions & 15 deletions packages/core/src/helpers/web.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IRunOptions } from '../types';

export function injectScript(win: Window, rootComId: string, scriptUrl: string) {
return new Promise<Window>((res, rej) => {
// This is the contract of the communication to get the root communication id
Expand All @@ -10,21 +12,19 @@ export function injectScript(win: Window, rootComId: string, scriptUrl: string)
});
}

export function getEngineEntryOptions(envName: string) {
const urlParams = new URLSearchParams(globalThis.location.search);
const currentScript = globalThis.document?.currentScript
const isHtmlScript = !!currentScript && 'src' in currentScript;
interface EngineWebEntryGlobalObj {
document?: Document,
location?: Location

engineEntryOptions(options: { urlParams: URLSearchParams; envName: string }): IRunOptions;
}

export function getEngineEntryOptions(envName: string, globalObj: EngineWebEntryGlobalObj): IRunOptions {
const urlParams = new URLSearchParams(globalObj?.location?.search);
const currentScript = globalObj?.document?.currentScript;

const scriptQueryString = isHtmlScript && currentScript.src?.split?.('?')?.[1] || ''
const scriptUrlParams = new URLSearchParams(scriptQueryString)
const injectedOptions = globalThis.engineEntryOptions?.({ urlParams, envName }) ?? new URLSearchParams('');
const optionsFromScript = new URLSearchParams(currentScript && currentScript.dataset.engineRunOptions || undefined);
const injectedOptions = globalObj?.engineEntryOptions?.({ urlParams, envName }) ?? new URLSearchParams('');

const definedParams = new Set<string>()
return new URLSearchParams([...injectedOptions, ...urlParams, ...scriptUrlParams].filter(([key]) => {
if (definedParams.has(key)) {
return false
}
definedParams.add(key)
return true
}))
return new Map([...optionsFromScript, ...urlParams, ...injectedOptions]);
}
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ export interface IRunOptions {
has(key: string): boolean;
get(key: string): string | string[] | boolean | null | undefined;
entries(): IterableIterator<[string, string | string[] | boolean | null | undefined]>;

[Symbol.iterator](): IterableIterator<[string, string | string[] | boolean | null | undefined]>;
}

export type RegisteringFeature<
Expand Down
25 changes: 25 additions & 0 deletions packages/core/test/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect } from 'chai';
import { getEngineEntryOptions } from '@wixc3/engine-core';

describe('helpers', () => {
describe('getEngineEntryOptions', () => {
it('it gets engine entry options from query string, from script url, from engineEntryOptions', () => {
const document = { currentScript: { dataset: { engineRunOptions: 'http://localhost:3000?one=0&two=1&script=123' } } } as unknown as Document;
const location = { search: 'one=2&two=2&search=123' } as Location;
const engineEntryOptions = ({ envName }: { envName: string }) => {
if (envName === 'thisTest') {
return new URLSearchParams([['injected', '123'], ['one', '1']]);
}
return new URLSearchParams();
};
const result = getEngineEntryOptions('thisTest', { document, location, engineEntryOptions });

expect(result.get('one'), 'Priorities injected options over search string and script url').to.eql('1');
expect(result.get('two'), 'Priorities search params over script url').to.eql('2');
expect(result.get('script'), 'Gets options from script url').to.eql('123');
expect(result.get('search'), 'Gets options from search string').to.eql('123');
expect(result.get('injected'), 'Gets options from engineEntryOptions').to.eql('123');
});
});
});

2 changes: 1 addition & 1 deletion packages/scripts/src/create-web-entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function createMainEntrypoint({
return `
import { main, COM, getEngineEntryOptions } from '@wixc3/engine-core';
const options = getEngineEntryOptions(${stringify(env.name)})
const options = getEngineEntryOptions(${stringify(env.name)}, globalThis)
const runtimePublicPath = ${runtimePublicPath};
main({
featureName: ${stringify(featureName)},
Expand Down

0 comments on commit 035cf22

Please sign in to comment.