primary rejects test
This commit is contained in:
parent
1e764419fb
commit
0df3f89ea6
@ -254,6 +254,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
expect(av.trustedPromise).toBe(trusted.promise);
|
expect(av.trustedPromise).toBe(trusted.promise);
|
||||||
expect(av.trustedStatus).toBe('verified');
|
expect(av.trustedStatus).toBe('verified');
|
||||||
|
|
||||||
|
await disjoint(); // sanity check
|
||||||
|
|
||||||
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
||||||
@ -316,6 +318,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
expect(av.trustedPromise).toBe(trusted.promise);
|
expect(av.trustedPromise).toBe(trusted.promise);
|
||||||
expect(av.trustedStatus).toBe('verified');
|
expect(av.trustedStatus).toBe('verified');
|
||||||
|
|
||||||
|
await disjoint(); // sanity check
|
||||||
|
|
||||||
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
||||||
@ -378,6 +382,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
expect(av.trustedPromise).toBe(trusted.promise);
|
expect(av.trustedPromise).toBe(trusted.promise);
|
||||||
expect(av.trustedStatus).toBe('none');
|
expect(av.trustedStatus).toBe('none');
|
||||||
|
|
||||||
|
await disjoint(); // sanity check
|
||||||
|
|
||||||
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
||||||
@ -440,6 +446,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
expect(av.trustedPromise).toBe(trusted.promise);
|
expect(av.trustedPromise).toBe(trusted.promise);
|
||||||
expect(av.trustedStatus).toBe('none');
|
expect(av.trustedStatus).toBe('none');
|
||||||
|
|
||||||
|
await disjoint(); // sanity check
|
||||||
|
|
||||||
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
||||||
@ -508,6 +516,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
expect(av.trustedPromise).toBe(trusted.promise);
|
expect(av.trustedPromise).toBe(trusted.promise);
|
||||||
expect(av.trustedStatus).toBe('verified');
|
expect(av.trustedStatus).toBe('verified');
|
||||||
|
|
||||||
|
await disjoint(); // sanity check
|
||||||
|
|
||||||
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
expect(trustedFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1);
|
||||||
@ -539,12 +549,52 @@ describe('fetchAndVerifyIfNeeded tests', () => {
|
|||||||
expect(result).toEqual(new BasicWE(2));
|
expect(result).toEqual(new BasicWE(2));
|
||||||
expect(av.trustedStatus).toBe('none');
|
expect(av.trustedStatus).toBe('none');
|
||||||
|
|
||||||
|
await disjoint(); // sanity check
|
||||||
|
|
||||||
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
expect(primaryFunc).toHaveBeenCalledTimes(1);
|
||||||
expect(trustedFunc).toHaveBeenCalledTimes(0);
|
expect(trustedFunc).toHaveBeenCalledTimes(0);
|
||||||
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(0);
|
expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(0);
|
||||||
expect(verifyFunc).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
|
// Make sure to do try/catch errors/rejections, verification failures, unverifies in the middle
|
||||||
// Multiple tryResolveTrustedPromises
|
// Multiple tryResolveTrustedPromises
|
||||||
});
|
});
|
||||||
|
@ -6,6 +6,7 @@ const config: Config = {
|
|||||||
coverageDirectory: './coverage',
|
coverageDirectory: './coverage',
|
||||||
collectCoverage: true,
|
collectCoverage: true,
|
||||||
testPathIgnorePatterns: [ '/^node_modules$/', '/^archive$/' ],
|
testPathIgnorePatterns: [ '/^node_modules$/', '/^archive$/' ],
|
||||||
|
setupFilesAfterEnv: [ './jest.setup.ts' ],
|
||||||
};
|
};
|
||||||
|
|
||||||
export default config;
|
export default config;
|
||||||
|
8
src/jest.setup.ts
Normal file
8
src/jest.setup.ts
Normal 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(),
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user