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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370import { render, screen, waitFor, fireEvent } from '@testing-library/vue'
import i18n from './utils/utils'
import SubscriptionList from '../pages/subscription/components/SubscriptionList.vue'
import axios from 'axios'
import type { Category, SubscriptionQueryParams, SubscriptionListResponse } from '../pages/subscription/types/types'
import { page1Data, page2Data, page3Data } from './utils/subscription-list-mock-data'
import { mockCategories } from './utils/categories-mock-data'
import { mockSubscriptions } from './utils/subscription-list-mock-data'
const realAxiosGet = axios.get
jest.mock('axios')
const mockedAxios = axios as jest.Mocked<typeof axios>;
const alertMock = jest.spyOn(window, 'alert').mockImplementation();
const BASE_URL = 'http://localhost:3000/v1'
const initGetCategoriesMock = (status: number, categories: Category[] | null, error: Error | null = null) => {
mockedAxios.get.mockImplementationOnce((url: string, config) => {
if (url === `${BASE_URL}/categories`) {
return status === 200 ? Promise.resolve({ data: { categories } }) : Promise.reject(error);
}
return realAxiosGet(url, config)
})
}
const initGetSubscriptionsMock = (
status: number,
queryParams: SubscriptionQueryParams | null,
response: SubscriptionListResponse,
error: Error | null = null
) => {
mockedAxios.get.mockImplementationOnce((url: string, config) => {
if (url.startsWith(`${BASE_URL}/subscriptions`) && JSON.stringify(config?.params) === JSON.stringify(queryParams)) {
return status === 200
? Promise.resolve({ data: response })
: Promise.reject(error || new Error('Server error'));
}
return realAxiosGet(url, config)
})
}
const renderComponent = () => {
return render(SubscriptionList, {
global: {
plugins: [i18n]
}
})
}
describe('SubscriptionList Component', () => {
beforeEach(() => {
mockedAxios.get.mockReset();
alertMock.mockClear();
})
afterAll(() => {
mockedAxios.get.mockReset();
alertMock.mockRestore();
})
it('should load successfully when categories and subscriptions are fetched successfully', async () => {
initGetCategoriesMock(200, mockCategories);
initGetSubscriptionsMock(200,
{ limit: 10,
pagination_direction: 'forward'
},
{
subscriptions: mockSubscriptions,
next_cursor: 3,
previous_cursor: 1,
has_more: false
});
renderComponent();
await waitFor(() => {
expect(screen.queryAllByText('Product updates')).toHaveLength(2);
expect(screen.queryAllByText('Articles and market insights')).toHaveLength(2);
expect(screen.queryAllByText('Case studies')).toHaveLength(1);
expect(screen.getByText('john@example.com')).toBeInTheDocument();
expect(screen.getByText('jane@example.com')).toBeInTheDocument();
expect(screen.queryByText('Previous')).not.toBeInTheDocument();
expect(screen.queryByText('Next')).not.toBeInTheDocument();
});
});
it('should show error when categories fetching fails', async () => {
initGetCategoriesMock(500, null, new Error('Failed to fetch categories'));
initGetSubscriptionsMock(200,
{ limit: 10,
pagination_direction: 'forward'
},
{
subscriptions: mockSubscriptions,
next_cursor: 3,
previous_cursor: 1,
has_more: false
});
renderComponent();
await waitFor(() => {
expect(alertMock).toHaveBeenCalledWith('Failed to fetch categories');
expect(alertMock).not.toHaveBeenCalledWith('Failed to fetch subscriptions');
expect(screen.getByText('No subscriptions found')).toBeInTheDocument();
expect(screen.queryByText('Product updates')).not.toBeInTheDocument();
expect(screen.queryByText('Articles and market insights')).not.toBeInTheDocument();
expect(screen.queryByText('Case studies')).not.toBeInTheDocument();
expect(screen.queryByText('john@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('jane@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('Previous')).not.toBeInTheDocument();
expect(screen.queryByText('Next')).not.toBeInTheDocument();
expect(screen.queryByText('Filter subscriptions')).not.toBeInTheDocument();
expect(screen.getByText('No subscriptions found')).toBeInTheDocument();
});
});
it('should show error when subscriptions fetching fails', async () => {
initGetCategoriesMock(200, mockCategories);
initGetSubscriptionsMock(500,
{ limit: 10,
pagination_direction: 'forward'
},
{
subscriptions: mockSubscriptions,
next_cursor: 3,
previous_cursor: 1,
has_more: false
},
new Error('Failed to fetch subscriptions'));
renderComponent();
await waitFor(() => {
expect(alertMock).toHaveBeenCalledWith('Failed to fetch subscriptions');
expect(alertMock).not.toHaveBeenCalledWith('Failed to fetch categories');
expect(screen.getByText('No subscriptions found')).toBeInTheDocument();
expect(screen.queryByText('Product updates')).not.toBeInTheDocument();
expect(screen.queryByText('Articles and market insights')).not.toBeInTheDocument();
expect(screen.queryByText('Case studies')).not.toBeInTheDocument();
expect(screen.queryByText('john@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('jane@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('Previous')).not.toBeInTheDocument();
expect(screen.queryByText('Next')).not.toBeInTheDocument();
expect(screen.queryByText('Filter subscriptions')).not.toBeInTheDocument();
});
});
it('should filter subscriptions when one category is selected', async () => {
initGetCategoriesMock(200, mockCategories);
initGetSubscriptionsMock(200,
{ limit: 10, pagination_direction: 'forward' },
{
subscriptions: mockSubscriptions,
next_cursor: 3,
previous_cursor: 1,
has_more: false
}
);
renderComponent();
await waitFor(() => {
expect(screen.queryAllByText('Product updates')).toHaveLength(2);
});
const checkbox = screen.queryAllByText('Product updates')[0];
await fireEvent.click(checkbox);
initGetSubscriptionsMock(200,
{
limit: 10,
pagination_direction: 'forward',
category_guids: ['123-456-789']
},
{
subscriptions: [mockSubscriptions[0]],
next_cursor: 0,
previous_cursor: 0,
has_more: false
}
);
await fireEvent.click(screen.getByText('Filter subscriptions'));
await waitFor(() => {
expect(screen.getByText('john@example.com')).toBeInTheDocument();
expect(screen.queryAllByText('Product updates')).toHaveLength(2);
expect(screen.queryByText('jane@example.com')).not.toBeInTheDocument();
});
});
it('should filter subscriptions when multiple categories are selected', async () => {
initGetCategoriesMock(200, mockCategories);
initGetSubscriptionsMock(200,
{ limit: 10, pagination_direction: 'forward' },
{
subscriptions: mockSubscriptions,
next_cursor: 3,
previous_cursor: 1,
has_more: false
}
);
renderComponent();
await waitFor(() => {
expect(screen.queryAllByText('Product updates')).toHaveLength(2);
expect(screen.queryAllByText('Articles and market insights')).toHaveLength(2);
});
const productUpdatesCheckbox = screen.queryAllByText('Product updates')[0];
const articlesCheckbox = screen.queryAllByText('Articles and market insights')[0];
await fireEvent.click(productUpdatesCheckbox);
await fireEvent.click(articlesCheckbox);
initGetSubscriptionsMock(200,
{
limit: 10,
pagination_direction: 'forward',
category_guids: ['123-456-789', '234-567-890']
},
{
subscriptions: mockSubscriptions,
next_cursor: 0,
previous_cursor: 0,
has_more: false
}
);
await fireEvent.click(screen.getByText('Filter subscriptions'));
await waitFor(() => {
expect(screen.getByText('john@example.com')).toBeInTheDocument();
expect(screen.getByText('jane@example.com')).toBeInTheDocument();
expect(screen.queryAllByText('Product updates')).toHaveLength(2);
expect(screen.queryAllByText('Articles and market insights')).toHaveLength(2);
});
});
it('should enable users to be able to paginate forward and backward', async () => {
initGetCategoriesMock(200, mockCategories);
initGetSubscriptionsMock(200,
{ limit: 10, pagination_direction: 'forward' },
{
subscriptions: page1Data,
next_cursor: 3,
previous_cursor: 1,
has_more: true
}
);
renderComponent();
await waitFor(() => {
expect(screen.getByText('alice@example.com')).toBeInTheDocument();
expect(screen.getByText('bob@example.com')).toBeInTheDocument();
expect(screen.getByText('charlie@example.com')).toBeInTheDocument();
expect(screen.getByText('Next')).toBeInTheDocument();
expect(screen.queryByText('Previous')).not.toBeInTheDocument();
});
initGetSubscriptionsMock(200,
{
limit: 10,
pagination_direction: 'forward',
pagination_id: 3
},
{
subscriptions: page2Data,
next_cursor: 6,
previous_cursor: 4,
has_more: true
}
);
// Paginate Forward
await fireEvent.click(screen.getByText('Next'));
await waitFor(() => {
expect(screen.getByText('david@example.com')).toBeInTheDocument();
expect(screen.getByText('eve@example.com')).toBeInTheDocument();
expect(screen.getByText('frank@example.com')).toBeInTheDocument();
expect(screen.queryByText('alice@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('bob@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('charlie@example.com')).not.toBeInTheDocument();
expect(screen.getByText('Next')).toBeInTheDocument();
expect(screen.getByText('Previous')).toBeInTheDocument();
});
initGetSubscriptionsMock(200,
{
limit: 10,
pagination_direction: 'forward',
pagination_id: 6
},
{
subscriptions: page3Data,
next_cursor: 9,
previous_cursor: 7,
has_more: false
}
);
await fireEvent.click(screen.getByText('Next'));
await waitFor(() => {
expect(screen.getByText('grace@example.com')).toBeInTheDocument();
expect(screen.getByText('henry@example.com')).toBeInTheDocument();
expect(screen.getByText('ivy@example.com')).toBeInTheDocument();
expect(screen.queryByText('david@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('eve@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('frank@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('alice@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('bob@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('charlie@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('Next')).not.toBeInTheDocument();
expect(screen.getByText('Previous')).toBeInTheDocument();
});
// Paginate Backward
initGetSubscriptionsMock(200,
{
limit: 10,
pagination_direction: 'backward',
pagination_id: 7
},
{
subscriptions: page2Data,
next_cursor: 6,
previous_cursor: 4,
has_more: true
}
);
await fireEvent.click(screen.getByText('Previous'));
await waitFor(() => {
expect(screen.getByText('david@example.com')).toBeInTheDocument();
expect(screen.getByText('eve@example.com')).toBeInTheDocument();
expect(screen.getByText('frank@example.com')).toBeInTheDocument();
expect(screen.queryByText('grace@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('henry@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('ivy@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('alice@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('bob@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('charlie@example.com')).not.toBeInTheDocument();
expect(screen.getByText('Next')).toBeInTheDocument();
expect(screen.getByText('Previous')).toBeInTheDocument();
});
initGetSubscriptionsMock(200,
{
limit: 10,
pagination_direction: 'backward',
pagination_id: 4
},
{
subscriptions: page1Data,
next_cursor: 3,
previous_cursor: 1,
has_more: false
}
);
await fireEvent.click(screen.getByText('Previous'));
await waitFor(() => {
expect(screen.getByText('alice@example.com')).toBeInTheDocument();
expect(screen.getByText('bob@example.com')).toBeInTheDocument();
expect(screen.getByText('charlie@example.com')).toBeInTheDocument();
expect(screen.queryByText('david@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('eve@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('frank@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('grace@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('henry@example.com')).not.toBeInTheDocument();
expect(screen.queryByText('ivy@example.com')).not.toBeInTheDocument();
expect(screen.getByText('Next')).toBeInTheDocument();
expect(screen.queryByText('Previous')).not.toBeInTheDocument();
});
});
});