-
Notifications
You must be signed in to change notification settings - Fork 42
/
FallbackPolicy.test.ts
36 lines (32 loc) 路 1.08 KB
/
FallbackPolicy.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { expect } from 'chai';
import { stub } from 'sinon';
import { fallback, handleAll } from './Policy';
describe('FallbackPolicy', () => {
it('does not fall back when not necessary', async () => {
const result = await fallback(handleAll, 'error').execute(() => 'ok');
expect(result).to.equal('ok');
});
it('returns a fallback and emits an error if necessary', async () => {
const policy = fallback(handleAll, 'error');
const onFallback = stub();
policy.onFailure(onFallback);
const error = new Error('oh no!');
const result = await policy.execute(() => {
throw error;
});
expect(result).to.equal('error');
expect(onFallback).calledWith({
reason: { error },
handled: true,
duration: onFallback.args[0]?.[0].duration,
});
});
it('links parent cancellation token', async () => {
const parent = new AbortController();
await fallback(handleAll, 'error').execute(({ signal }) => {
expect(signal.aborted).to.be.false;
parent.abort();
expect(signal.aborted).to.be.true;
}, parent.signal);
});
});