/
conversationService.spec.ts
140 lines (120 loc) · 4.67 KB
/
conversationService.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
let mockFetchArgs: MockFetch;
import * as nodeFetch from 'node-fetch';
jest.mock('node-fetch', () => {
const fetch = (url, opts) => {
mockFetchArgs = { url, opts };
return {
ok: true,
json: async () => ({}),
text: async () => '{}',
};
};
(fetch as any).Headers = class {
};
(fetch as any).Response = class {
};
return fetch;
});
interface mockOpts {
headers: Headers;
method: 'GET' | 'DELETE' | 'POST';
body?: any;
}
interface MockFetch {
url?: string;
opts?: mockOpts;
}
import { ConversationService, headers as headersInstance } from './conversationService';
describe('The ConversationService should call "fetch" with the expected parameters when executing', () => {
test('the "addUser" function', () => {
ConversationService.addUser('http://localhost', 'abcdef');
const { url, opts } = mockFetchArgs;
expect(url).toBe('http://localhost/emulator/abcdef/users');
const { body, headers, method } = opts;
expect(headers instanceof nodeFetch.Headers).toBeTruthy();
expect(headers === headers).toBeTruthy();
expect(method).toBe('POST');
const members = JSON.parse(body);
expect(members[0].name).toBeFalsy();
expect(members[0].id).toBeFalsy();
expect(headersInstance).toEqual(headers);
});
test('the "removeUser" function', () => {
ConversationService.removeUser('http://localhost', 'abcdef', '1234');
const { url, opts } = mockFetchArgs;
expect(url).toBe('http://localhost/emulator/abcdef/users');
const { body, headers, method } = opts;
expect(headers instanceof nodeFetch.Headers).toBeTruthy();
expect(headers === headers).toBeTruthy();
expect(method).toBe('DELETE');
const users = JSON.parse(body);
expect(users[0].id).toBe('1234');
expect(headersInstance).toEqual(headers);
});
test('the "removeRandomUser" function', () => {
ConversationService.removeRandomUser('http://localhost', 'abcdef');
const { url, opts } = mockFetchArgs;
expect(url).toBe('http://localhost/emulator/abcdef/users');
const { body, headers, method } = opts;
expect(headers instanceof nodeFetch.Headers).toBeTruthy();
expect(headers === headers).toBeTruthy();
expect(method).toBe('DELETE');
expect(body).toBeFalsy();
expect(headersInstance).toEqual(headers);
});
test('the "botContactAdded" function', () => {
ConversationService.botContactAdded('http://localhost', 'abcdef');
const { url, opts } = mockFetchArgs;
expect(url).toBe('http://localhost/emulator/abcdef/contacts');
const { body, headers, method } = opts;
expect(headers instanceof nodeFetch.Headers).toBeTruthy();
expect(headers === headers).toBeTruthy();
expect(method).toBe('POST');
expect(body).toBeFalsy();
expect(headersInstance).toEqual(headers);
});
test('the "botContactRemoved" function', () => {
ConversationService.botContactRemoved('http://localhost', 'abcdef');
const { url, opts } = mockFetchArgs;
expect(url).toBe('http://localhost/emulator/abcdef/contacts');
const { body, headers, method } = opts;
expect(headers instanceof nodeFetch.Headers).toBeTruthy();
expect(headers === headers).toBeTruthy();
expect(method).toBe('DELETE');
expect(body).toBeFalsy();
expect(headersInstance).toEqual(headers);
});
test('the "typing" function', () => {
ConversationService.typing('http://localhost', 'abcdef');
const { url, opts } = mockFetchArgs;
expect(url).toBe('http://localhost/emulator/abcdef/typing');
const { body, headers, method } = opts;
expect(headers instanceof nodeFetch.Headers).toBeTruthy();
expect(headers === headers).toBeTruthy();
expect(method).toBe('POST');
expect(body).toBeFalsy();
expect(headersInstance).toEqual(headers);
});
test('the "ping" function', () => {
ConversationService.ping('http://localhost', 'abcdef');
const { url, opts } = mockFetchArgs;
expect(url).toBe('http://localhost/emulator/abcdef/ping');
const { body, headers, method } = opts;
expect(headers instanceof nodeFetch.Headers).toBeTruthy();
expect(headers === headers).toBeTruthy();
expect(method).toBe('POST');
expect(body).toBeFalsy();
expect(headersInstance).toEqual(headers);
});
test('the "deleteUserData" function', () => {
ConversationService.deleteUserData('http://localhost', 'abcdef');
const { url, opts } = mockFetchArgs;
expect(url).toBe('http://localhost/emulator/abcdef/userdata');
const { body, headers, method } = opts;
expect(headers instanceof nodeFetch.Headers).toBeTruthy();
expect(headers === headers).toBeTruthy();
expect(method).toBe('DELETE');
expect(body).toBeFalsy();
expect(headersInstance).toEqual(headers);
});
});