-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
84 lines (53 loc) · 1.7 KB
/
index.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
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
const fetch = require('node-fetch');
global.Headers = fetch.Headers;
global.Request = fetch.Request;
import SilverRequest from "./src/index";
window.alert = jest.fn();
const fetchMock = require('fetch-mock');
let obj : SilverRequest;
beforeEach(() => {
obj = new SilverRequest(jest.fn())
})
beforeAll(() => {
fetchMock.mock('/success', { foo: 'bar' });
fetchMock.mock('/bad', 500);
})
test('Test logger', () => {
let logger = jest.fn();
expect(obj.setLogger(logger)).toBeInstanceOf(SilverRequest);
obj.setUrl('/success');
obj.send();
expect(logger).toHaveBeenCalled();
expect(obj.setLogger.bind(obj, null)).toThrow();
})
test('test setRunOnSuccess', () => {
let runOnSuccess = jest.fn();
expect(obj.setRunOnSuccess(runOnSuccess)).toBeInstanceOf(SilverRequest);
obj.setUrl('/success');
obj.send();
setTimeout(() => {
expect(runOnSuccess).toHaveBeenCalled();
}, 200);
expect(obj.setRunOnSuccess.bind(obj, null)).toThrow();
})
test('test setOnSuccess', async () => {
let runOnSuccess = jest.fn();
expect(obj.setOnSuccess(runOnSuccess)).toBeInstanceOf(SilverRequest);
obj.setUrl('/success');
await obj.send();
expect(runOnSuccess).toHaveBeenCalled();
expect(obj.setOnSuccess.bind(obj, null)).toThrow();
});
test('test setOnError', async () => {
let setOnError = jest.fn();
expect(obj.setOnError(setOnError)).toBeInstanceOf(SilverRequest);
//
obj.setUrl('/bad');
try {
let pr : Promise<any> = await obj.send();
}
catch(e){
}
expect(setOnError).toHaveBeenCalled();
expect(obj.setOnError.bind(obj, null)).toThrow();
})