/
utils.spec.ts
89 lines (79 loc) · 2.53 KB
/
utils.spec.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { expect } from 'chai';
import { PromiseDelegate } from '@lumino/coreutils';
import { Signal } from '@lumino/signaling';
import {
expectFailure,
isFulfilled,
testEmission
} from '@jupyterlab/testutils';
describe('test/utils', () => {
describe('testEmission', () => {
it('should resolve to the given value', async () => {
const owner = {};
const x = new Signal<{}, number>(owner);
const emission = testEmission(x, {
value: 'done'
});
x.emit(0);
expect(await emission).to.equal('done');
});
it('should find the given emission', async () => {
const owner = {};
const x = new Signal<{}, number>(owner);
const emission = testEmission(x, {
find: (a, b) => b === 1,
value: 'done'
});
x.emit(0);
expect(await isFulfilled(emission)).to.equal(false);
x.emit(1);
expect(await emission).to.equal('done');
});
it('should reject if the test throws an error', async () => {
const owner = {};
const x = new Signal<{}, number>(owner);
const emission = testEmission(x, {
find: (a, b) => b === 1,
test: (a, b) => {
throw new Error('my error');
},
value: 'done'
});
x.emit(0);
expect(await isFulfilled(emission)).to.equal(false);
x.emit(1);
await expectFailure(emission, 'my error');
});
it('should resolve if the test succeeds', async () => {
const owner = {};
const x = new Signal<{}, number>(owner);
const emission = testEmission(x, {
find: (a, b) => b === 1,
test: (a, b) => {
expect(b).to.equal(1);
},
value: 'done'
});
x.emit(0);
expect(await isFulfilled(emission)).to.equal(false);
x.emit(1);
expect(await emission).to.equal('done');
});
});
describe('isFulfilled', () => {
it('should resolve to true only after a promise is fulfilled', async () => {
const p = new PromiseDelegate<number>();
expect(await isFulfilled(p.promise)).to.equal(false);
p.resolve(10);
expect(await isFulfilled(p.promise)).to.equal(true);
});
it('should resolve to true even if the promise is rejected', async () => {
const p = new PromiseDelegate<number>();
expect(await isFulfilled(p.promise)).to.equal(false);
p.reject(new Error('my error'));
expect(await isFulfilled(p.promise)).to.equal(true);
});
});
});