-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.test.js
353 lines (305 loc) · 11.7 KB
/
results.test.js
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
require('../public/scripts/results');
const fs = require('fs');
const path = require('path');
const { TextEncoder, TextDecoder } = require('util');
Object.assign(global, { TextDecoder, TextEncoder });
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
jest.mock('../public/scripts/results.js', () => ({
fetchBooks: jest.fn().mockResolvedValue({
totalItems: 2,
items: [
{
title: 'Book 1',
author: 'Author 1',
thumbnailURL: 'https://example.com/book1.jpg',
volumeId: '123456789'
},
{
title: 'Book 2',
author: 'Author 2',
thumbnailURL: 'https://example.com/book2.jpg',
volumeId: '987654321'
}
]
})
}));
describe('Search results page', () => {
let dom;
beforeEach(() => {
const htmlFilePath = path.resolve(__dirname, '../public/results.html');
const htmlFile = fs.readFileSync(htmlFilePath, 'utf-8');
dom = new JSDOM(htmlFile, { url: 'http://localhost/books/search' });
global.document = dom.window.document;
global.window = dom.window;
Object.defineProperty(window, 'localStorage', {
value: {
getItem: jest.fn().mockReturnValueOnce('mockToken')
},
writable: true
});
global.fetch = jest.fn().mockResolvedValueOnce({
json: () => ({ authenticated: true })
});
document.dispatchEvent(new Event('DOMContentLoaded'));
});
afterEach(() => {
global.document = undefined;
global.window = undefined;
});
// test home button click
test('home button click', () => {
const homeButton = dom.window.document.querySelector('.home');
if (homeButton) {
homeButton.click();
dom.reconfigure({ url: 'http://localhost/' });
expect(dom.window.location.href).toBe('http://localhost/');
}
});
test('logo click', () => {
const logo = dom.window.document.querySelector('.logo');
if (logo) {
logo.click();
dom.reconfigure({ url: 'http://localhost/' });
expect(dom.window.location.href).toBe('http://localhost/');
}
});
test('sign up button click', () => {
window.location.assign = jest.fn();
const signUpButton = dom.window.document.querySelector('.signup');
if (signUpButton) {
signUpButton.click();
dom.reconfigure({ url: 'http://localhost/signup' });
expect(dom.window.location.href).toBe('http://localhost/signup');
}
});
test('sign in button click', () => {
const signInButton = dom.window.document.querySelector('.sign-in');
if (signInButton) {
signInButton.click();
dom.reconfigure({ url: 'http://localhost/signin' });
expect(dom.window.location.href).toBe('http://localhost/signin');
}
});
test('sign out button click', () => {
const signOutButton = dom.window.document.querySelector('.signout');
if (signOutButton) {
signOutButton.click();
dom.reconfigure({ url: 'http://localhost/' });
expect(dom.window.location.href).toBe('http://localhost/');
}
});
test('profile button click', () => {
const profileButton = dom.window.document.querySelector('.profile');
if (profileButton) {
profileButton.click();
dom.reconfigure({ url: 'http://localhost/user' });
expect(dom.window.location.href).toBe('http://localhost/user');
}
});
test('search query submission with empty query', () => {
const searchInput = dom.window.document.getElementById('book-search');
if (searchInput) {
searchInput.value = '';
const searchButton = dom.window.document.querySelector('.search-button');
if (searchButton) {
const event = new dom.window.Event('click');
searchButton.dispatchEvent(event);
expect(dom.window.location.href).toBe('http://localhost/books/search');
}
}
});
test('search query submission with whitespace query', () => {
const searchInput = dom.window.document.getElementById('book-search');
if (searchInput) {
searchInput.value = ' ';
const searchButton = dom.window.document.querySelector('.search-button');
if (searchButton) {
const event = new dom.window.Event('click');
searchButton.dispatchEvent(event);
expect(dom.window.location.href).toBe('http://localhost/books/search');
}
}
});
test('search query submission with null query', () => {
const searchInput = dom.window.document.getElementById('book-search');
if (searchInput) {
searchInput.value = null;
const searchButton = dom.window.document.querySelector('.search-button');
if (searchButton) {
const event = new dom.window.Event('click');
searchButton.dispatchEvent(event);
expect(dom.window.location.href).toBe('http://localhost/books/search');
}
}
});
test('search query submission with undefined query', () => {
const searchInput = dom.window.document.getElementById('book-search');
if (searchInput) {
searchInput.value = undefined;
const searchButton = dom.window.document.querySelector('.search-button');
if (searchButton) {
const event = new dom.window.Event('click');
searchButton.dispatchEvent(event);
expect(dom.window.location.href).toBe('http://localhost/books/search');
}
}
});
test('search query submission with valid query', () => {
const searchInput = dom.window.document.getElementById('book-search');
if (searchInput) {
searchInput.value = 'harry potter';
const searchButton = dom.window.document.querySelector('.search-button');
if (searchButton) {
searchButton.click();
dom.reconfigure({ url: 'http://localhost/search?q=harry%20potter' });
expect(dom.window.location.href).toBe('http://localhost/search?q=harry%20potter');
}
}
});
test('handleBookClick function', () => {
const book = dom.window.document.querySelector('.book');
if (book) {
book.click();
dom.reconfigure({ url: 'http://localhost/books/1' });
expect(dom.window.location.href).toBe('http://localhost/books/1');
}
});
test('fetchBooks function', () => {
const fetchBooks = jest.fn();
fetchBooks();
expect(fetchBooks).toHaveBeenCalled();
});
test('display initial set of books', () => {
const books = dom.window.document.querySelectorAll('.book');
expect(books.length).toBe(0);
});
test('Books are displayed in the results section', async () => {
await new Promise(resolve => setTimeout(resolve, 0));
const bookList = document.querySelector('.book-list');
if (bookList) {
expect(bookList.innerHTML).toContain('Book 1');
expect(bookList.innerHTML).toContain('Book 2');
expect(bookList.innerHTML).toContain('Author 1');
expect(bookList.innerHTML).toContain('Author 2');
expect(bookList.innerHTML).toContain('https://example.com/book1.jpg');
expect(bookList.innerHTML).toContain('https://example.com/book2.jpg');
expect(bookList.innerHTML).toContain('123456789');
expect(bookList.innerHTML).toContain('987654321');
}
});
test('Authenticated user - Show profile and sign out buttons, hide sign up and sign in buttons', async () => {
// Wait for the checkAuthentication function to be called
await new Promise(resolve => setTimeout(resolve, 0));
const signUpButton = document.querySelector('.signup');
const signInButton = document.querySelector('.sign-in');
const signOutButton = document.querySelector('.signout');
const profileButton = document.querySelector('.profile');
if (signUpButton && signInButton && signOutButton && profileButton) {
expect(signUpButton.style.display).toBe('none');
expect(signInButton.style.display).toBe('none');
expect(signOutButton.style.display).toBe('flex');
expect(profileButton.style.display).toBe('flex');
}
});
test('Unauthenticated user - Show sign up and sign in buttons, hide profile and sign out buttons', async () => {
global.fetch.mockResolvedValueOnce({
json: () => ({ authenticated: false })
});
// Wait for the checkAuthentication function to be called
await new Promise(resolve => setTimeout(resolve, 0));
const signUpButton = document.querySelector('.signup');
const signInButton = document.querySelector('.sign-in');
const signOutButton = document.querySelector('.signout');
const profileButton = document.querySelector('.profile');
if (signUpButton && signInButton && signOutButton && profileButton) {
expect(signUpButton.style.display).toBe('flex');
expect(signInButton.style.display).toBe('flex');
expect(signOutButton.style.display).toBe('none');
expect(profileButton.style.display).toBe('none');
}
});
test('Pagination next button click works', async () => {
await new Promise(resolve => setTimeout(resolve, 0));
const pagination = dom.window.document.querySelector('.pagination');
if (pagination) {
const nextPageButton = pagination.querySelector('.next');
if (nextPageButton) {
nextPageButton.click();
dom.reconfigure({ url: 'http://localhost/books/search?page=2' });
expect(dom.window.location.href).toBe('http://localhost/books/search?page=2');
}
}
});
test('Pagination previous button click works', async () => {
await new Promise(resolve => setTimeout(resolve, 0));
const pagination = dom.window.document.querySelector('.pagination');
if (pagination) {
const previousPageButton = pagination.querySelector('.previous');
if (previousPageButton) {
previousPageButton.click();
dom.reconfigure({ url: 'http://localhost/books/search?page=1' });
expect(dom.window.location.href).toBe('http://localhost/books/search?page=1');
}
}
});
test('Add Book Button is displayed for authenticated user', async () => {
// Wait for the checkAuthentication function to be called
await new Promise(resolve => setTimeout(resolve, 0));
const addBookButton = document.querySelector('.add');
if (addBookButton) {
expect(addBookButton).toBeTruthy();
}
});
test('Add Book Button is hidden for unauthenticated user', async () => {
global.fetch.mockResolvedValueOnce({
json: () => ({ authenticated: false })
});
await new Promise(resolve => setTimeout(resolve, 0));
const addBookButton = document.querySelector('.add');
if (addBookButton) {
expect(addBookButton).toBeFalsy();
}
});
test('Add Book Button click opens modal', async () => {
await new Promise(resolve => setTimeout(resolve, 0));
const addBookButton = document.querySelector('.add');
if (addBookButton) {
addBookButton.click();
const modal = document.querySelector('.modal');
if (modal) {
expect(modal.style.display).toBe('block');
}
}
});
test('Close modal button click closes modal', async () => {
await new Promise(resolve => setTimeout(resolve, 0));
const addBookButton = document.querySelector('.add');
if (addBookButton) {
addBookButton.click();
const modal = document.querySelector('.modal');
if (modal) {
const closeButton = modal.querySelector('.close');
if (closeButton) {
closeButton.click();
expect(modal.style.display).toBe('none');
}
}
}
});
test('Clicking a bookshelf element in modal closes modal', async () => {
await new Promise(resolve => setTimeout(resolve, 0));
const addBookButton = document.querySelector('.add');
if (addBookButton) {
addBookButton.click();
const modal = document.querySelector('.modal');
if (modal) {
const bookshelf = modal.querySelector('.bookshelf');
if (bookshelf) {
bookshelf.click();
expect(modal.style.display).toBe('none');
}
}
}
});
});