diff --git a/src/client/tests/webapp/auto-verifier.test.ts b/src/client/tests/webapp/auto-verifier.test.ts index 3a9a8c5..b65dc45 100644 --- a/src/client/tests/webapp/auto-verifier.test.ts +++ b/src/client/tests/webapp/auto-verifier.test.ts @@ -141,6 +141,58 @@ describe('fetchAndVerifyIfNeeded tests', () => { }); } } + + function getManualsAndMocks(): { + nextPrimary: () => ManualPromise, + nextTrusted: () => ManualPromise, + nextEnsureTrustedFuncReady: () => ManualPromise, + nextVerify: () => ManualPromise, + primaryFunc: jest.Mock, []>, + trustedFunc: jest.Mock, []>, + ensureTrustedFuncReadyFunc: jest.Mock, []>, + verifyFunc: jest.Mock, []>, + } { + const manuals: { + primary: ManualPromise[], + trusted: ManualPromise[], + ensureTrustedFuncReady: ManualPromise[], + verify: ManualPromise[], + } = { + primary: [], + trusted: [], + ensureTrustedFuncReady: [], + verify: [], + }; + + const calls: { + primary: number, + trusted: number, + ensureTrustedFuncReady: number, + verify: number, + } = { + primary: 0, + trusted: 0, + ensureTrustedFuncReady: 0, + verify: 0 + }; + + function getNext(manualList: ManualPromise[]) { + const next = new ManualPromise(); + manualList.push(next); + return next; + } + + return { + nextPrimary: () => getNext(manuals.primary), + nextTrusted: () => getNext(manuals.trusted), + nextEnsureTrustedFuncReady: () => getNext(manuals.ensureTrustedFuncReady), + nextVerify: () => getNext(manuals.verify), + primaryFunc: jest.fn(() => manuals.primary[calls.primary++]!.promise), + trustedFunc: jest.fn(() => manuals.trusted[calls.trusted++]!.promise), + ensureTrustedFuncReadyFunc: jest.fn(() => manuals.ensureTrustedFuncReady[calls.ensureTrustedFuncReady++]!.promise), + verifyFunc: jest.fn(() => manuals.verify[calls.verify++]!.promise), + } + } function getGeneralMocks(): { primary: ManualPromise, trusted: ManualPromise, @@ -172,9 +224,14 @@ describe('fetchAndVerifyIfNeeded tests', () => { test('nothing before, primary with value, then trusted with value', async () => { // TODO: Make xxxFunc mock functions that can be tested for if they were called const { - primary, trusted, ensureTrustedFuncReady, verify, + nextPrimary, nextTrusted, nextEnsureTrustedFuncReady, nextVerify, primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc - } = getGeneralMocks(); + } = getManualsAndMocks(); + + const primary = nextPrimary(); + const trusted = nextTrusted(); + const ensureTrustedFuncReady = nextEnsureTrustedFuncReady(); + const verify = nextVerify(); const av = new AutoVerifier(primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc); const result = av.fetchAndVerifyIfNeeded(); @@ -186,6 +243,7 @@ describe('fetchAndVerifyIfNeeded tests', () => { primary.resolve(new BasicWE(2)); await disjoint(); + expect(primaryFunc).toHaveBeenCalled(); expect(result).resolves.toEqual(new BasicWE(2)); expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(trusted.promise); @@ -194,6 +252,7 @@ describe('fetchAndVerifyIfNeeded tests', () => { ensureTrustedFuncReady.resolve(); await disjoint(); + expect(ensureTrustedFuncReadyFunc).toHaveBeenCalled(); expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedStatus).toBe('verifying'); @@ -201,6 +260,7 @@ describe('fetchAndVerifyIfNeeded tests', () => { trusted.resolve(new BasicWE(2)); await disjoint(); + expect(trustedFunc).toHaveBeenCalled(); expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedStatus).toBe('verifying'); @@ -208,9 +268,15 @@ describe('fetchAndVerifyIfNeeded tests', () => { verify.resolve(true); await disjoint(); + expect(verifyFunc).toHaveBeenCalled(); expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedStatus).toBe('verified'); + + expect(primaryFunc).toHaveBeenCalledTimes(1); + expect(trustedFunc).toHaveBeenCalledTimes(1); + expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1); + expect(verifyFunc).toHaveBeenCalledTimes(1); }); // Make sure to do try/catch errors }); diff --git a/src/jest.config.ts b/src/jest.config.ts new file mode 100644 index 0000000..5e5b23e --- /dev/null +++ b/src/jest.config.ts @@ -0,0 +1,12 @@ +import type { Config } from 'jest'; + +const config: Config = { + preset: 'ts-jest', + testEnvironment: 'node', + coverageDirectory: './coverage', + collectCoverage: true, + testPathIgnorePatterns: [ '/^node_modules$/', '/^archive$/' ], +}; + +export default config; +