-
Notifications
You must be signed in to change notification settings - Fork 34
/
mock-intercept.spec.ts
161 lines (147 loc) · 5.32 KB
/
mock-intercept.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { ApiClient, Mock, MockAdapter, SequentialMockAdapter } from '../../fwk';
import { RequestOptions, RequestPlugin } from '../core';
import { MockInterceptFetch } from './mock-intercept.fetch';
import { CUSTOM_MOCK_OPERATION_ID_HEADER } from './mock-intercept.interface';
import { MockInterceptRequest } from './mock-intercept.request';
const testMock: Mock<any> = {
mockData: {}
};
const getMockSpy = jest.fn().mockReturnValue(testMock);
const getLatestMockSpy = jest.fn().mockReturnValue(testMock);
const retrieveOperationIdSpy = jest.fn().mockReturnValue(Promise.resolve('testOperation'));
const initializeSpy = jest.fn().mockReturnValue(Promise.resolve());
const testMockAdapter: MockAdapter = {
getMock: getMockSpy,
getLatestMock: getLatestMockSpy,
initialize: initializeSpy,
retrieveOperationId: retrieveOperationIdSpy
};
const requestPlugins: RequestPlugin[] = [new MockInterceptRequest({adapter: new SequentialMockAdapter([], {})})];
const apiClient = {
options: {
requestPlugins,
basePath: 'test',
replyPlugins: []
}
} as ApiClient;
describe('Mock intercept', () => {
describe('request plugin', () => {
it('should do nothing if disabled is true', async () => {
const plugin = new MockInterceptRequest({ disabled: true, adapter: testMockAdapter });
const originalRequest: RequestOptions = {
method: 'get',
headers: new Headers({test: 'true'}),
basePath: 'myurl'
};
const loaded = plugin.load();
expect(await loaded.transform(originalRequest)).toEqual(originalRequest);
expect(initializeSpy).toHaveBeenCalled();
});
it.skip('should intercept the request', async () => {
// Disabled because Blob URL is not supported on NodeJS
const plugin = new MockInterceptRequest({ adapter: testMockAdapter });
const originalRequest: RequestOptions = {
headers: new Headers({test: 'true'}),
basePath: 'myurl',
method: 'PATCH'
};
const loaded = plugin.load();
const transformed = loaded.transform(originalRequest) as RequestOptions;
const res = await fetch(transformed.basePath, transformed);
expect(getMockSpy).toHaveBeenCalled();
expect(res).toEqual(getMockSpy);
expect(initializeSpy).toHaveBeenCalled();
});
});
describe('fetch plugin', () => {
describe('when using an initialization function', () => {
let plugin: MockInterceptFetch;
let asyncMockAdapter: MockAdapter;
beforeEach(() => {
asyncMockAdapter = {
initialize: initializeSpy,
getMock: getMockSpy,
getLatestMock: getLatestMockSpy,
retrieveOperationId: retrieveOperationIdSpy
};
plugin = new MockInterceptFetch({adapter: asyncMockAdapter});
});
it('should call initialize fn', async () => {
const loadedPlugin = plugin.load({
fetchPlugins: [],
url: 'myurl',
apiClient,
options: {
headers: new Headers({
[CUSTOM_MOCK_OPERATION_ID_HEADER]: 'testOperation'
})
}
});
const testData: any = {test: true};
await loadedPlugin.transform(Promise.resolve(testData));
expect(initializeSpy).toHaveBeenCalled();
expect(getMockSpy).not.toHaveBeenCalled();
expect(getLatestMockSpy).toHaveBeenCalledWith('testOperation');
});
it('should throw if there is no request plugin', () => {
const config = {
fetchPlugins: [],
url: 'myurl',
apiClient: {
options: {
requestPlugins: []
}
} as ApiClient,
options: {
headers: new Headers({
[CUSTOM_MOCK_OPERATION_ID_HEADER]: 'testOperation'
})
}
};
expect(() => plugin.load(config)).toThrow();
});
});
});
describe('with delay', () => {
it('should delay the response of the specific number', async () => {
const plugin = new MockInterceptFetch({ adapter: testMockAdapter, delayTiming: 700 });
const loadedPlugin = plugin.load({
fetchPlugins: [],
url: '',
apiClient,
options: {
headers: new Headers({
[CUSTOM_MOCK_OPERATION_ID_HEADER]: 'testOperation'
})
}
});
const callback = jest.fn();
const run = loadedPlugin.transform(Promise.resolve({} as any)).then(callback);
await jest.advanceTimersByTimeAsync(699);
expect(callback).not.toHaveBeenCalled();
await jest.advanceTimersByTimeAsync(1);
expect(callback).toHaveBeenCalled();
await run;
});
it('should delay the response based on callback', async () => {
const plugin = new MockInterceptFetch({ adapter: testMockAdapter, delayTiming: () => 800 });
const loadedPlugin = plugin.load({
fetchPlugins: [],
url: '',
apiClient,
options: {
headers: new Headers({
[CUSTOM_MOCK_OPERATION_ID_HEADER]: 'testOperation'
})
}
});
const callback = jest.fn();
const run = loadedPlugin.transform(Promise.resolve({} as any)).then(callback);
await jest.advanceTimersByTimeAsync(799);
expect(callback).not.toHaveBeenCalled();
await jest.advanceTimersByTimeAsync(1);
expect(callback).toHaveBeenCalled();
await run;
});
});
});