Skip to content

Commit

Permalink
chore: use OR logic for find
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Apr 5, 2024
1 parent cc8c264 commit 9d9b9f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
37 changes: 28 additions & 9 deletions src/bidiMapper/modules/script/PreloadScriptStorage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const MOCKED_UUID_1 = '00000000-0000-0000-0000-00000000000a';
const MOCKED_UUID_2 = '00000000-0000-0000-0000-00000000000b';
const CDP_TARGET_ID = 'TARGET_ID';

function createPreloadScript(id: string, contexts?: string[]): PreloadScript {
const preloadScript = sinon.createStubInstance(PreloadScript);
sinon.stub(preloadScript, 'id').get(() => id);
sinon.stub(preloadScript, 'contexts').get(() => contexts);
sinon.stub(preloadScript, 'targetIds').get(() => new Set([CDP_TARGET_ID]));
return preloadScript;
}

describe('PreloadScriptStorage', () => {
let preloadScriptStorage: PreloadScriptStorage;

Expand All @@ -37,6 +45,10 @@ describe('PreloadScriptStorage', () => {
sinon.stub(cdpTarget, 'id').get(() => CDP_TARGET_ID);
});

afterEach(() => {
sinon.restore();
});

it('initial state', () => {
expect(preloadScriptStorage.find()).to.be.empty;
expect(preloadScriptStorage.find({})).to.be.empty;
Expand Down Expand Up @@ -106,14 +118,21 @@ describe('PreloadScriptStorage', () => {
});
});

afterEach(() => {
sinon.restore();
});
it(`find preload scripts using OR logic`, () => {
const preloadScript1 = createPreloadScript(MOCKED_UUID_1, [CDP_TARGET_ID]);
preloadScriptStorage.add(preloadScript1);

function createPreloadScript(id: string): PreloadScript {
const preloadScript = sinon.createStubInstance(PreloadScript);
sinon.stub(preloadScript, 'id').get(() => id);
sinon.stub(preloadScript, 'targetIds').get(() => new Set([CDP_TARGET_ID]));
return preloadScript;
}
expect(
preloadScriptStorage.find({
id: MOCKED_UUID_2,
targetId: CDP_TARGET_ID,
})
).to.deep.equal([preloadScript1]);
expect(
preloadScriptStorage.find({
targetId: CDP_TARGET_ID,
global: true,
})
).to.deep.equal([preloadScript1]);
});
});
20 changes: 11 additions & 9 deletions src/bidiMapper/modules/script/PreloadScriptStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,34 @@ export class PreloadScriptStorage {
/** Tracks all BiDi preload scripts. */
readonly #scripts = new Set<PreloadScript>();

/** Finds all entries that match the given filter. */
/**
* Finds all entries that match the given filter (OR logic).
*/
find(filter?: PreloadScriptFilter): PreloadScript[] {
if (!filter) {
return [...this.#scripts];
}

return [...this.#scripts].filter((script) => {
if (filter.id !== undefined && filter.id !== script.id) {
return false;
if (filter.id !== undefined && filter.id === script.id) {
return true;
}
if (
filter.targetId !== undefined &&
!script.targetIds.has(filter.targetId)
script.targetIds.has(filter.targetId)
) {
return false;
return true;
}
if (
filter.global !== undefined &&
// Global scripts have no contexts
((filter.global && script.contexts !== undefined) ||
((filter.global && script.contexts === undefined) ||
// Non global scripts always have contexts
(!filter.global && script.contexts === undefined))
(!filter.global && script.contexts !== undefined))
) {
return false;
return true;
}
return true;
return false;
});
}

Expand Down

0 comments on commit 9d9b9f3

Please sign in to comment.