/
index.spec.ts
94 lines (73 loc) · 2.59 KB
/
index.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
import * as SlackWebApi from '@slack/web-api';
import { WebClient } from '@slack/web-api';
import { MockedWebClient, MockWebClient } from './index';
describe('Mocked @slack/web-api', () => {
let client: WebClient;
let mockedClient: MockWebClient;
beforeEach(() => {
// How typescript projects setup create tests
MockedWebClient.mockClear();
// How a upstream project would initialize a client in application code.
client = new WebClient();
[mockedClient] = MockedWebClient.mock.instances;
});
it('mocks all api families', () => {
expect.assertions(1);
const { WebClient: RealWebClient } = jest.requireActual<typeof SlackWebApi>(
'@slack/web-api',
);
const realClient = new RealWebClient();
// Keys defined in the client that aren't api families
const notFamilyKeys = [
'retryConfig',
'requestQueue',
'tlsConfig',
'logger',
];
const apiFamilies = (clientInstance: WebClient | MockWebClient) =>
Object.keys(clientInstance)
.filter(
(key) =>
typeof (clientInstance as Record<string, any>)[key] === 'object',
)
.filter((key) => !key.startsWith('_'))
.filter((key) => !notFamilyKeys.includes(key));
const expectedApiFamilies = apiFamilies(realClient);
const actualApiFamilies = apiFamilies(client);
expect(actualApiFamilies).toEqual(expectedApiFamilies);
});
it('mocks web client instance functions', () => {
expect.assertions(1);
expect(jest.isMockFunction(client.chat.postMessage)).toBeTruthy();
});
it('returns ok by default', async () => {
expect.assertions(1);
expect(await client.chat.postMessage()).toEqual({ ok: true });
});
it('return values can be overridden', async () => {
expect.assertions(1);
const newResult = {
ok: true,
message: {
text: 'Hello World',
},
};
mockedClient.chat.postMessage.mockResolvedValue(newResult);
expect(await client.chat.postMessage()).toEqual(newResult);
});
it('allows jest expect functions', async () => {
expect.assertions(1);
await client.chat.postMessage();
expect(client.chat.postMessage).toHaveBeenCalledTimes(1);
});
it('exposes instances "normally"', () => {
expect.assertions(2);
expect(MockedWebClient.mock.instances.length).toEqual(1);
expect(MockedWebClient.mock.instances[0]).toBe(client);
});
it('allows MockedWebClient to be constructable', () => {
expect.assertions(1);
const instance = new MockedWebClient();
expect(jest.isMockFunction(instance.chat.postMessage)).toBeTruthy();
});
});