check when functions are called
This commit is contained in:
parent
31c0cd6662
commit
e1364b49e8
@ -141,6 +141,58 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getManualsAndMocks(): {
|
||||||
|
nextPrimary: () => ManualPromise<BasicWE | null>,
|
||||||
|
nextTrusted: () => ManualPromise<BasicWE | null>,
|
||||||
|
nextEnsureTrustedFuncReady: () => ManualPromise<void>,
|
||||||
|
nextVerify: () => ManualPromise<boolean>,
|
||||||
|
primaryFunc: jest.Mock<Promise<BasicWE | null>, []>,
|
||||||
|
trustedFunc: jest.Mock<Promise<BasicWE | null>, []>,
|
||||||
|
ensureTrustedFuncReadyFunc: jest.Mock<Promise<void>, []>,
|
||||||
|
verifyFunc: jest.Mock<Promise<boolean>, []>,
|
||||||
|
} {
|
||||||
|
const manuals: {
|
||||||
|
primary: ManualPromise<BasicWE | null>[],
|
||||||
|
trusted: ManualPromise<BasicWE | null>[],
|
||||||
|
ensureTrustedFuncReady: ManualPromise<void>[],
|
||||||
|
verify: ManualPromise<boolean>[],
|
||||||
|
} = {
|
||||||
|
primary: [],
|
||||||
|
trusted: [],
|
||||||
|
ensureTrustedFuncReady: [],
|
||||||
|
verify: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
const calls: {
|
||||||
|
primary: number,
|
||||||
|
trusted: number,
|
||||||
|
ensureTrustedFuncReady: number,
|
||||||
|
verify: number,
|
||||||
|
} = {
|
||||||
|
primary: 0,
|
||||||
|
trusted: 0,
|
||||||
|
ensureTrustedFuncReady: 0,
|
||||||
|
verify: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
function getNext<T>(manualList: ManualPromise<T>[]) {
|
||||||
|
const next = new ManualPromise<T>();
|
||||||
|
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(): {
|
function getGeneralMocks(): {
|
||||||
primary: ManualPromise<BasicWE>,
|
primary: ManualPromise<BasicWE>,
|
||||||
trusted: ManualPromise<BasicWE>,
|
trusted: ManualPromise<BasicWE>,
|
||||||
@ -172,9 +224,14 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
test('nothing before, primary with value, then trusted with value', async () => {
|
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
|
// TODO: Make xxxFunc mock functions that can be tested for if they were called
|
||||||
const {
|
const {
|
||||||
primary, trusted, ensureTrustedFuncReady, verify,
|
nextPrimary, nextTrusted, nextEnsureTrustedFuncReady, nextVerify,
|
||||||
primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc
|
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 av = new AutoVerifier(primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc);
|
||||||
const result = av.fetchAndVerifyIfNeeded();
|
const result = av.fetchAndVerifyIfNeeded();
|
||||||
@ -186,6 +243,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
primary.resolve(new BasicWE(2));
|
primary.resolve(new BasicWE(2));
|
||||||
await disjoint();
|
await disjoint();
|
||||||
|
|
||||||
|
expect(primaryFunc).toHaveBeenCalled();
|
||||||
expect(result).resolves.toEqual(new BasicWE(2));
|
expect(result).resolves.toEqual(new BasicWE(2));
|
||||||
expect(av.primaryPromise).toBe(null);
|
expect(av.primaryPromise).toBe(null);
|
||||||
expect(av.trustedPromise).toBe(trusted.promise);
|
expect(av.trustedPromise).toBe(trusted.promise);
|
||||||
@ -194,6 +252,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
ensureTrustedFuncReady.resolve();
|
ensureTrustedFuncReady.resolve();
|
||||||
await disjoint();
|
await disjoint();
|
||||||
|
|
||||||
|
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalled();
|
||||||
expect(av.primaryPromise).toBe(null);
|
expect(av.primaryPromise).toBe(null);
|
||||||
expect(av.trustedPromise).toBe(trusted.promise);
|
expect(av.trustedPromise).toBe(trusted.promise);
|
||||||
expect(av.trustedStatus).toBe('verifying');
|
expect(av.trustedStatus).toBe('verifying');
|
||||||
@ -201,6 +260,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
trusted.resolve(new BasicWE(2));
|
trusted.resolve(new BasicWE(2));
|
||||||
await disjoint();
|
await disjoint();
|
||||||
|
|
||||||
|
expect(trustedFunc).toHaveBeenCalled();
|
||||||
expect(av.primaryPromise).toBe(null);
|
expect(av.primaryPromise).toBe(null);
|
||||||
expect(av.trustedPromise).toBe(trusted.promise);
|
expect(av.trustedPromise).toBe(trusted.promise);
|
||||||
expect(av.trustedStatus).toBe('verifying');
|
expect(av.trustedStatus).toBe('verifying');
|
||||||
@ -208,9 +268,15 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
verify.resolve(true);
|
verify.resolve(true);
|
||||||
await disjoint();
|
await disjoint();
|
||||||
|
|
||||||
|
expect(verifyFunc).toHaveBeenCalled();
|
||||||
expect(av.primaryPromise).toBe(null);
|
expect(av.primaryPromise).toBe(null);
|
||||||
expect(av.trustedPromise).toBe(trusted.promise);
|
expect(av.trustedPromise).toBe(trusted.promise);
|
||||||
expect(av.trustedStatus).toBe('verified');
|
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
|
// Make sure to do try/catch errors
|
||||||
});
|
});
|
||||||
|
12
src/jest.config.ts
Normal file
12
src/jest.config.ts
Normal file
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user