39 lines
1.8 KiB
JavaScript
39 lines
1.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import ts from 'typescript';
|
|
|
|
const source = ts.transpileModule(readFileSync(new URL('../src/importLifecycleGuards.ts', import.meta.url), 'utf8'), {
|
|
compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext },
|
|
}).outputText.replace(/export /g, '');
|
|
const guards = new Function(`${source};return { importIssuePageKey, matchesImportIssuePage, runImportPollFollowup };`)();
|
|
|
|
test('import issue page identity includes both run and page', () => {
|
|
assert.equal(guards.importIssuePageKey('run-a', 2), 'run-a:2');
|
|
assert.notEqual(guards.importIssuePageKey('run-a', 2), guards.importIssuePageKey('run-a', 3));
|
|
assert.notEqual(guards.importIssuePageKey('run-a', 2), guards.importIssuePageKey('run-b', 2));
|
|
assert.equal(guards.matchesImportIssuePage('run-a', 2, 'run-a', 2), true);
|
|
assert.equal(guards.matchesImportIssuePage('run-a', 2, 'run-a', 3), false);
|
|
assert.equal(guards.matchesImportIssuePage('run-a', 2, 'run-b', 2), false);
|
|
});
|
|
|
|
test('an import poll does not start candidate or issue follow-up reads after the view becomes inactive', async () => {
|
|
let active = false;
|
|
let followupCalls = 0;
|
|
const skipped = await guards.runImportPollFollowup('run-a', () => active, async () => { followupCalls++; });
|
|
assert.deepEqual(skipped, { started: false, current: false });
|
|
assert.equal(followupCalls, 0);
|
|
|
|
active = true;
|
|
let resolveRead;
|
|
const pending = guards.runImportPollFollowup('run-a', () => active, () => {
|
|
followupCalls++;
|
|
return new Promise(resolve => { resolveRead = resolve; });
|
|
});
|
|
active = false;
|
|
resolveRead({ items: [] });
|
|
const stale = await pending;
|
|
assert.deepEqual(stale, { started: true, current: false, value: { items: [] } });
|
|
assert.equal(followupCalls, 1);
|
|
});
|