-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathuseNotifications.test.tsx
More file actions
210 lines (170 loc) · 6.08 KB
/
Copy pathuseNotifications.test.tsx
File metadata and controls
210 lines (170 loc) · 6.08 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import Knock, { FeedClientOptions } from "@knocklabs/client";
import { renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, test, vi } from "vitest";
import useNotifications from "../../src/modules/feed/hooks/useNotifications";
const TEST_FEED_CHANNEL_ID = "e84d5ddc-fd69-44ad-a431-68d784b8c306";
describe("useNotifications", () => {
let knock: Knock;
beforeEach(() => {
knock = new Knock("test_api_key");
});
test("initializes and returns a new feed client (strict mode off)", () => {
vi.spyOn(knock.feeds, "initialize");
const options: FeedClientOptions = {
archived: "include",
page_size: 10,
status: "all",
mode: "rich",
};
const { result } = renderHook(
() => useNotifications(knock, TEST_FEED_CHANNEL_ID, options),
{ reactStrictMode: false },
);
expect(knock.feeds.initialize).toHaveBeenCalledExactlyOnceWith(
TEST_FEED_CHANNEL_ID,
options,
);
const feedClient = result.current;
expect(feedClient).toBeDefined();
expect(feedClient.feedId).toEqual(TEST_FEED_CHANNEL_ID);
expect(feedClient.defaultOptions).toEqual(options);
});
test("initializes and returns a new feed client (strict mode on)", () => {
vi.spyOn(knock.feeds, "initialize");
vi.spyOn(knock.feeds, "removeInstance");
const options: FeedClientOptions = {
archived: "include",
page_size: 10,
status: "all",
};
const { result } = renderHook(
() => useNotifications(knock, TEST_FEED_CHANNEL_ID, options),
{ reactStrictMode: true },
);
const feedClient = result.current;
// useState initializers and useEffect hooks run twice in strict mode,
// so we should see 3 calls to initialize (2 in the useState initializer,
// 1 in the useEffect hook when the disposed feed is reinitialized)
expect(knock.feeds.initialize).toHaveBeenCalledTimes(3);
expect(knock.feeds.initialize).toHaveBeenNthCalledWith(
1,
TEST_FEED_CHANNEL_ID,
options,
);
expect(knock.feeds.initialize).toHaveBeenNthCalledWith(
2,
TEST_FEED_CHANNEL_ID,
options,
);
expect(knock.feeds.initialize).toHaveBeenNthCalledWith(
3,
TEST_FEED_CHANNEL_ID,
options,
);
expect(knock.feeds.removeInstance).toHaveBeenCalledTimes(1);
const disposedFeedClient = vi.mocked(knock.feeds.removeInstance).mock
.calls?.[0]?.[0];
// The disposed feed client should have the same feed ID,
// but should *not* be the same feed client instance
expect(disposedFeedClient?.feedId).toEqual(TEST_FEED_CHANNEL_ID);
expect(disposedFeedClient).not.toBe(feedClient);
});
test("disposes feed client on unmount", () => {
vi.spyOn(knock.feeds, "initialize");
vi.spyOn(knock.feeds, "removeInstance");
const options: FeedClientOptions = {
archived: "include",
page_size: 10,
status: "all",
};
const { result, unmount } = renderHook(() =>
useNotifications(knock, TEST_FEED_CHANNEL_ID, options),
);
const feedClient = result.current;
// A feed client was initialized
expect(knock.feeds.initialize).toHaveBeenCalledTimes(1);
expect(feedClient).toBeDefined();
unmount();
// The feed client was disposed
expect(knock.feeds.removeInstance).toHaveBeenCalledExactlyOnceWith(
feedClient,
);
// No additional feed clients were initialized
expect(knock.feeds.initialize).toHaveBeenCalledTimes(1);
});
test("disposes existing feed client when feed ID changes", () => {
vi.spyOn(knock.feeds, "initialize");
vi.spyOn(knock.feeds, "removeInstance");
const feedId1 = TEST_FEED_CHANNEL_ID;
const feedId2 = "86784a77-b9ea-4683-85b3-7362b426e810";
const options: FeedClientOptions = {
archived: "include",
page_size: 10,
status: "all",
mode: "rich",
};
const { result, rerender } = renderHook(
(feedId: string) => useNotifications(knock, feedId, options),
{ initialProps: feedId1 },
);
const feedClient1 = result.current;
rerender(feedId2);
const feedClient2 = result.current;
// The old feed client should be disposed
expect(knock.feeds.removeInstance).toHaveBeenCalledExactlyOnceWith(
feedClient1,
);
expect(knock.feeds.initialize).toHaveBeenCalledTimes(2);
expect(knock.feeds.initialize).toHaveBeenNthCalledWith(1, feedId1, options);
expect(knock.feeds.initialize).toHaveBeenNthCalledWith(2, feedId2, options);
// A new feed client should be created
expect(feedClient2).toBeDefined();
expect(feedClient2).not.toBe(feedClient1);
expect(feedClient2.feedId).toEqual(feedId2);
expect(feedClient2.defaultOptions).toEqual(options);
});
test("disposes existing feed client when options change", () => {
vi.spyOn(knock.feeds, "initialize");
vi.spyOn(knock.feeds, "removeInstance");
const options1: FeedClientOptions = {
archived: "include",
page_size: 10,
status: "all",
mode: "compact",
};
const options2: FeedClientOptions = {
archived: "exclude",
page_size: 10,
status: "read",
mode: "rich",
};
const { result, rerender } = renderHook(
(options: FeedClientOptions) =>
useNotifications(knock, TEST_FEED_CHANNEL_ID, options),
{ initialProps: options1 },
);
const feedClient1 = result.current;
rerender(options2);
const feedClient2 = result.current;
// The old feed client should be disposed
expect(knock.feeds.removeInstance).toHaveBeenCalledExactlyOnceWith(
feedClient1,
);
expect(knock.feeds.initialize).toHaveBeenCalledTimes(2);
expect(knock.feeds.initialize).toHaveBeenNthCalledWith(
1,
TEST_FEED_CHANNEL_ID,
options1,
);
expect(knock.feeds.initialize).toHaveBeenNthCalledWith(
2,
TEST_FEED_CHANNEL_ID,
options2,
);
// A new feed client should be created
expect(feedClient2).toBeDefined();
expect(feedClient2).not.toBe(feedClient1);
expect(feedClient2.feedId).toEqual(TEST_FEED_CHANNEL_ID);
expect(feedClient2.defaultOptions).toEqual(options2);
});
});