primary rejects test

This commit is contained in:
Michael Peters 2022-10-05 22:03:48 -07:00
parent 1e764419fb
commit 0df3f89ea6
3 changed files with 60 additions and 1 deletions

View File

@ -254,6 +254,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(av.trustedPromise).toBe(trusted.promise);
expect(av.trustedStatus).toBe('verified');
await disjoint(); // sanity check
expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
@ -311,11 +313,13 @@ describe('fetchAndVerifyIfNeeded tests', () => {
verify.resolve(true);
await disjoint();
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(trusted.promise);
expect(av.trustedStatus).toBe('verified');
await disjoint(); // sanity check
expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
@ -378,6 +382,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(av.trustedPromise).toBe(trusted.promise);
expect(av.trustedStatus).toBe('none');
await disjoint(); // sanity check
expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
@ -440,6 +446,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(av.trustedPromise).toBe(trusted.promise);
expect(av.trustedStatus).toBe('none');
await disjoint(); // sanity check
expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
@ -508,6 +516,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(av.trustedPromise).toBe(trusted.promise);
expect(av.trustedStatus).toBe('verified');
await disjoint(); // sanity check
expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
@ -539,12 +549,52 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(result).toEqual(new BasicWE(2));
expect(av.trustedStatus).toBe('none');
await disjoint(); // sanity check
expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(0);
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(0);
expect(verifyFunc).toHaveBeenCalledTimes(0);
});
test('primary rejects - cache fail', async () => {
// expecting the promise to reject, the server to succeed, but the verify function not to be called
const {
nextPrimary, nextTrusted,
primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary();
const trusted = nextTrusted();
const av = new AutoVerifier(primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded();
let rejection: Error | undefined = undefined;
resultPromise.catch(value => { rejection = value; });
expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise);
expect(av.trustedStatus).toBe('fetching');
expect(primaryFunc).toHaveBeenCalled();
expect(trustedFunc).toHaveBeenCalled(); // TODO: Is this the source of the problem? - trustedFunc needs to wait to be called until ensureTrustedFuncReadyFunc is resolved
expect(rejection).toBeUndefined();
primary.reject(new BasicWE(2)); // this will also 'unverify' the AutoVerifier
await disjoint();
expect(rejection).toBeDefined();
expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(null);
expect(av.trustedStatus).toBe('none');
await disjoint(); // sanity check
expect(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1); // notably, this server response will be thrown out
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(0);
expect(verifyFunc).toHaveBeenCalledTimes(0);
});
// Make sure to do try/catch errors/rejections, verification failures, unverifies in the middle
// Multiple tryResolveTrustedPromises
});

View File

@ -6,6 +6,7 @@ const config: Config = {
coverageDirectory: './coverage',
collectCoverage: true,
testPathIgnorePatterns: [ '/^node_modules$/', '/^archive$/' ],
setupFilesAfterEnv: [ './jest.setup.ts' ],
};
export default config;

8
src/jest.setup.ts Normal file
View File

@ -0,0 +1,8 @@
// silence debug/log/warn console logs (since they are HUGE in the jest output)
global.console = {
...console,
debug: jest.fn(),
log: jest.fn(),
warn: jest.fn(),
};