-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathHighlightPostSidebarWidget.spec.tsx
More file actions
259 lines (212 loc) · 7.28 KB
/
Copy pathHighlightPostSidebarWidget.spec.tsx
File metadata and controls
259 lines (212 loc) · 7.28 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import React from 'react';
import {
act,
fireEvent,
render,
screen,
waitFor,
} from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { HighlightPostSidebarWidget } from './HighlightPostSidebarWidget';
import { useLogContext } from '../../../contexts/LogContext';
import { gqlClient } from '../../../graphql/common';
import { ONE_HOUR } from '../../../lib/time';
import { useAnonymousPostExperience } from '../../../hooks/post/useAnonymousPostExperience';
jest.mock('../../../lib/constants', () => ({
webappUrl: '/',
isDevelopment: false,
}));
jest.mock('../../../contexts/LogContext');
jest.mock('../../../hooks/post/useAnonymousPostExperience');
jest.mock('../../../graphql/common', () => ({
gqlClient: {
request: jest.fn(),
},
}));
const mockUseLogContext = jest.mocked(useLogContext);
const mockUseAnonymousPostExperience = jest.mocked(useAnonymousPostExperience);
const mockGqlRequest = jest.mocked(gqlClient.request);
const buildHighlight = (id: string, headline: string, hoursAgo = 1) => ({
id,
channel: 'agents',
headline,
highlightedAt: new Date(Date.now() - hoursAgo * ONE_HOUR).toISOString(),
post: {
id: `post-${id}`,
commentsPermalink: `/posts/post-${id}`,
},
});
const buildResponse = (highlights: ReturnType<typeof buildHighlight>[]) => ({
majorHeadlines: {
edges: highlights.map((node) => ({ node, cursor: node.id })),
pageInfo: { hasNextPage: false, endCursor: null },
},
});
const renderWidget = () => {
const client = new QueryClient({
defaultOptions: { queries: { retry: false, gcTime: 0 } },
});
return render(
<QueryClientProvider client={client}>
<HighlightPostSidebarWidget />
</QueryClientProvider>,
);
};
describe('HighlightPostSidebarWidget', () => {
const logEvent = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
mockGqlRequest.mockReset();
mockUseAnonymousPostExperience.mockReturnValue({
isAnonPostExperience: false,
isPostPageExperience: true,
});
mockUseLogContext.mockReturnValue({
logEvent,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
});
afterEach(() => {
jest.useRealTimers();
});
it('renders title and the first headline when query resolves', async () => {
mockGqlRequest.mockResolvedValue(
buildResponse([
buildHighlight('h1', 'The first highlight'),
buildHighlight('h2', 'The second highlight'),
]),
);
renderWidget();
expect(await screen.findByText('Happening Now')).toBeInTheDocument();
expect(screen.getByText('The first highlight')).toBeInTheDocument();
expect(screen.queryByText('The second highlight')).not.toBeInTheDocument();
});
it('returns null when no highlights are returned', async () => {
mockGqlRequest.mockResolvedValue(buildResponse([]));
renderWidget();
await waitFor(() => expect(mockGqlRequest).toHaveBeenCalled());
expect(screen.queryByText('Happening Now')).not.toBeInTheDocument();
expect(
screen.queryByTestId('postPageHighlightWidget'),
).not.toBeInTheDocument();
});
it('filters out highlights older than 24 hours', async () => {
mockGqlRequest.mockResolvedValue(
buildResponse([
buildHighlight('fresh', 'Fresh headline', 2),
buildHighlight('stale', 'Stale headline', 30),
]),
);
renderWidget();
expect(await screen.findByText('Fresh headline')).toBeInTheDocument();
expect(screen.queryByText('Stale headline')).not.toBeInTheDocument();
});
it('hides the widget when all highlights are older than 24 hours', async () => {
mockGqlRequest.mockResolvedValue(
buildResponse([
buildHighlight('stale1', 'Stale one', 25),
buildHighlight('stale2', 'Stale two', 48),
]),
);
renderWidget();
await waitFor(() => expect(mockGqlRequest).toHaveBeenCalled());
expect(screen.queryByText('Happening Now')).not.toBeInTheDocument();
expect(
screen.queryByTestId('postPageHighlightWidget'),
).not.toBeInTheDocument();
});
it('renders for anonymous users by default', async () => {
mockUseAnonymousPostExperience.mockReturnValue({
isAnonPostExperience: true,
isPostPageExperience: true,
});
mockGqlRequest.mockResolvedValue(
buildResponse([buildHighlight('h1', 'Anonymous headline')]),
);
renderWidget();
expect(await screen.findByText('Anonymous headline')).toBeInTheDocument();
});
it('points "Read all" to /highlights with the first highlight id', async () => {
mockGqlRequest.mockResolvedValue(
buildResponse([buildHighlight('first', 'First headline')]),
);
renderWidget();
const readAll = await screen.findByLabelText('Read all highlights');
expect(readAll).toHaveAttribute('href', '/highlights?highlight=first');
});
it('rotates headlines after the interval', async () => {
jest.useFakeTimers({ doNotFake: ['queueMicrotask'] });
mockGqlRequest.mockResolvedValue(
buildResponse([
buildHighlight('h1', 'The first highlight'),
buildHighlight('h2', 'The second highlight'),
]),
);
renderWidget();
await act(async () => {
await Promise.resolve();
});
await waitFor(() =>
expect(screen.getByText('The first highlight')).toBeInTheDocument(),
);
await act(async () => {
jest.advanceTimersByTime(6000);
});
await act(async () => {
jest.advanceTimersByTime(500);
});
expect(screen.getByText('The second highlight')).toBeInTheDocument();
expect(screen.queryByText('The first highlight')).not.toBeInTheDocument();
});
it('pauses rotation while hovering and resumes after unhover', async () => {
jest.useFakeTimers({ doNotFake: ['queueMicrotask'] });
mockGqlRequest.mockResolvedValue(
buildResponse([
buildHighlight('h1', 'The first highlight'),
buildHighlight('h2', 'The second highlight'),
]),
);
renderWidget();
await act(async () => {
await Promise.resolve();
});
await waitFor(() =>
expect(screen.getByText('The first highlight')).toBeInTheDocument(),
);
const widget = screen.getByTestId('postPageHighlightWidget');
await act(async () => {
fireEvent.mouseEnter(widget);
});
await act(async () => {
jest.advanceTimersByTime(6000);
});
await act(async () => {
jest.advanceTimersByTime(500);
});
expect(screen.getByText('The first highlight')).toBeInTheDocument();
expect(screen.queryByText('The second highlight')).not.toBeInTheDocument();
await act(async () => {
fireEvent.mouseLeave(widget);
});
await act(async () => {
jest.advanceTimersByTime(6000);
});
await act(async () => {
jest.advanceTimersByTime(500);
});
expect(screen.getByText('The second highlight')).toBeInTheDocument();
expect(screen.queryByText('The first highlight')).not.toBeInTheDocument();
});
it('logs an impression when highlights load', async () => {
mockGqlRequest.mockResolvedValue(
buildResponse([buildHighlight('h1', 'A headline')]),
);
renderWidget();
await screen.findByText('Happening Now');
await waitFor(() =>
expect(logEvent).toHaveBeenCalledWith(
expect.objectContaining({ event_name: 'impression' }),
),
);
});
});