This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
delta.spec.ts
137 lines (118 loc) · 3.23 KB
/
delta.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* eslint-env jest */
import { fakeServer, FakeServer } from 'nise';
import { searchDelta } from '../delta';
import { search } from '../actions';
import { searchBlobsApiResponse } from '../../mocks';
import { ValidationError, JsonError } from '../../api';
const state = {
root: '/api/',
nonce: 'abcd',
term: 'js code',
collection: 'blobs',
};
describe('delta', () => {
describe('searchDelta', () => {
let server: FakeServer;
beforeEach(() => {
server?.restore();
server = fakeServer.create();
});
afterAll(() => {
server?.restore();
});
it('should ignore random actions', () => {
const action = {
type: 'RANDOM',
};
expect(searchDelta).toEmitFromDelta([], send => {
send(action, state);
});
});
it('should ignore request with empty term', () => {
expect(searchDelta).toEmitFromDelta([], send => {
send(search.request(), { ...state, term: '' });
});
});
it('should debounce 300ms before making request', () => {
expect(searchDelta).toEmitFromDelta([], (send, tick) => {
send(search.request(), state);
tick(299);
expect(server.requests).toHaveLength(0);
});
});
it('should cancel in flight request', () => {
expect(searchDelta).toEmitFromDelta([], (send, tick) => {
send(search.request(), state);
tick(350);
expect(server.requests).toHaveLength(1);
send(search.cancel(), state);
expect(server.lastRequest?.status).toBe(0);
});
});
it('should emit error event on bad JSON', () => {
expect(searchDelta).toEmitFromDelta(
[
[
350,
KTU.value(
search.failure(
new JsonError(
new TypeError(
'Error parsing JSON response: Unexpected end of JSON input',
),
),
),
),
],
],
(send, tick) => {
send(search.request(), state);
tick(350);
server.lastRequest?.respond(200, {}, '{"broken"');
},
);
});
it('should emit error event on bad response format', () => {
expect(searchDelta).toEmitFromDelta(
[
[
350,
KTU.value(search.failure(new ValidationError(expect.any(Array)))),
],
],
(send, tick) => {
send(search.request(), state);
tick(350);
server.lastRequest?.respond(200, {}, JSON.stringify({}));
},
);
});
it('should emit success', () => {
expect(searchDelta).toEmitFromDelta(
[
[
350,
KTU.value(
search.success({
collection: 'blobs',
response: searchBlobsApiResponse,
}),
),
],
],
(send, tick) => {
send(search.request(), state);
tick(350);
expect(server.lastRequest?.url).toEqual(
'/api/search/blobs?s=js%20code',
);
server.lastRequest?.respond(
200,
{},
JSON.stringify(searchBlobsApiResponse),
);
},
);
});
});
});