-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspec.js
129 lines (114 loc) · 4.07 KB
/
spec.js
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
const { create } = require('axios');
const moxios = require('moxios');
const PXResponse = require('./specHelpers/PXResponse');
let attach, detach;
let axios;
let resolved = false;
let rejected = false;
async function checkResolved(promise) {
await promise.then(() => {
resolved = true;
}).catch(() => {
rejected = true;
});
}
const REQUEST_PERIMETERX_BLOCK = '/perimeterx-block';
const REQUEST_PERIMETERX_BLOCK_ALT = '/perimeterx-block-alt';
const REQUEST_NOT_FOUND = '/not-found';
const REQUEST_OKAY = '/okay';
describe('perimeterx-axios-interceptor', () => {
beforeAll(() => {
jest.mock('./lib/openModal', () => jest.fn(() => Promise.resolve()));
({ attach, detach } = require('.'));
});
beforeEach(() => {
resolved = false;
rejected = false;
moxios.install();
moxios.stubRequest(REQUEST_NOT_FOUND, {
status: 404,
response: 'Not found'
});
moxios.stubRequest(REQUEST_OKAY, {
status: 200,
response: 'Success'
});
moxios.stubRequest(REQUEST_PERIMETERX_BLOCK, new PXResponse({ appId: PXResponse.defaultAppId }));
moxios.stubRequest(REQUEST_PERIMETERX_BLOCK_ALT, new PXResponse({ appId: PXResponse.defaultAppId }));
axios = create();
});
afterEach(() => {
moxios.uninstall();
});
afterAll(() => {
jest.unmock('./lib/openModal');
});
it('should add an interceptor (use), then remove it (eject)', () => {
expect(axios.interceptors.response.handlers).toHaveLength(0);
attach(axios);
expect(axios.interceptors.response.handlers).toHaveLength(1);
const [ handler ] = axios.interceptors.response.handlers;
const keys = Object.keys(handler); // 'fulfilled', 'rejected', 'synchronous', 'runWhen'
expect(keys).toContain('fulfilled');
expect(keys).toContain('rejected');
const types = Object.values(handler).map((value) => typeof value);
expect(types.filter((i) => i === 'function')).toHaveLength(2);
detach(axios);
expect(axios.interceptors.response.handlers).toEqual([null]);
});
it('should fall back to natural flow for non perimeterx errors', async() => {
attach(axios);
expect(axios.get(REQUEST_NOT_FOUND)).rejects.toThrow('Request failed with status code 404');
});
it('should bring perimeterx errors to resolve', async() => {
attach(axios);
const { data } = await axios.get(REQUEST_PERIMETERX_BLOCK);
expect(data).toEqual('Successful request');
});
it('should call filter function', async() => {
expect.assertions(3);
attach(axios, {
filter: () => expect().toBe() || true
});
await checkResolved(
axios.get(REQUEST_PERIMETERX_BLOCK)
);
expect(resolved).toBe(true);
expect(rejected).toBe(false);
});
it.each([true, 1, 'yes', {}])(
'should filter in when function returns %p',
async(item) => {
attach(axios, {
filter: () => item
});
await checkResolved(
axios.get(REQUEST_PERIMETERX_BLOCK)
);
expect(resolved).toBe(true);
expect(rejected).toBe(false);
}
);
it.each([false, 0, '', null])(
'should filter out when function returns %p',
async(item) => {
attach(axios, {
filter: () => item
});
await checkResolved(
axios.get(REQUEST_PERIMETERX_BLOCK)
);
expect(resolved).toBe(false);
expect(rejected).toBe(true);
}
);
it('should expost "path" and "appId" arguments to filter function', async() => {
const expected = {};
attach(axios, {
filter: (args) => Object.assign(expected, args)
});
await axios.get(REQUEST_PERIMETERX_BLOCK).catch(() => null);
expect(expected.appId).toBe(PXResponse.defaultAppId);
expect(expected.path).toBe('/perimeterx-block');
});
});