-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.test.js
More file actions
322 lines (239 loc) · 10 KB
/
main.test.js
File metadata and controls
322 lines (239 loc) · 10 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
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
const { resolve } = require('path');
const { readFileSync } = require('fs');
const { TextEncoder, TextDecoder } = require('util');
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
const { JSDOM } = require('jsdom');
const { waitFor, fireEvent } = require('@testing-library/dom');
const axios = require('axios');
const data = readFileSync(resolve(__dirname, './server/initial.json'), 'utf8');
const dataJSON = JSON.parse(data);
const { studentId, studentName, NetlifyDeployUrl } = require('./deployData.js');
let windowJSDOM = null;
beforeEach(async () => {
const { window } = await JSDOM.fromFile('./client/index.html', {
runScripts: 'dangerously',
resources: 'usable',
});
fetch = jest.fn().mockReturnValueOnce(Promise.resolve({ json: () => dataJSON.books }));
window.fetch = fetch;
const d = await new Promise((resolve) => {
window.addEventListener('load', () => {
resolve({
window: window,
document: window.document,
});
});
});
windowJSDOM = d;
});
describe('Testing the main tag in "Daftar Buku Perpustakaan" page', () => {
it('should be fetch data from API, http://localhost:3333/books', async () => {
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(1));
expect(fetch).toHaveBeenLastCalledWith('http://localhost:3333/books');
});
it('should be render data from API', async () => {
await waitFor(() => {
const bookList = windowJSDOM.document.querySelectorAll('.book-item');
expect(bookList.length).toBe(15);
// get all booklist children
const book1 = bookList[0].children;
// check if book1 has 5 children
expect(book1.length).toBe(5);
// check if book1 title is correct
expect(book1[0].textContent).toBe('Laskar Pelangi');
// check if book1 author is correct
expect(book1[1].textContent).toBe('Andrea Hirata');
// check if book1 year is correct
expect(book1[2].textContent).toBe('2005');
// check if book1 quantity is correct
expect(book1[3].textContent).toBe('3');
// check if book1 action is correct
const actions = book1[4].children;
expect(actions.length).toBe(2);
// check if book1 action edit is correct
expect(actions[0].textContent).toBe('Edit');
// check if book1 action delete is correct
expect(actions[1].textContent).toBe('Hapus');
});
});
describe('Test click delete button', () => {
it('should be delete book', async () => {
const dataJSONDelete = [...dataJSON.books];
dataJSONDelete.shift();
let bookList = null;
await waitFor(() => {
bookList = windowJSDOM.document.querySelectorAll('.book-item');
});
expect(bookList.length).toBe(15);
const book1 = bookList[0].children;
const actions = book1[4].children;
fireEvent.click(actions[1]);
const lastCall = fetch.mock.lastCall;
expect(lastCall[0]).toBe('http://localhost:3333/books/1');
expect(lastCall[1].method).toBe('DELETE');
window.fetch.mockResolvedValueOnce({
json: () => Promise.resolve(dataJSONDelete),
});
await waitFor(() => {
const bookListAfterDelete = windowJSDOM.document.querySelectorAll('.book-item');
expect(bookListAfterDelete.length).toBe(14);
});
});
});
describe('Test click edit button', () => {
it('should be move to "edit buku" page', async () => {
const mockData = [...dataJSON.books];
const mockDataEdit = mockData.shift();
let bookList = null;
await waitFor(() => {
bookList = windowJSDOM.document.querySelectorAll('.book-item');
});
const book1 = bookList[0].children;
const actions = book1[4].children;
window.fetch.mockResolvedValueOnce({
json: () => Promise.resolve(mockDataEdit),
});
fireEvent.click(actions[0]);
const lastCall = fetch.mock.lastCall;
expect(lastCall[0]).toBe('http://localhost:3333/books/1');
await waitFor(() => {
const h2 = windowJSDOM.document.getElementsByTagName('h2')[0];
expect(h2.textContent).toBe('Edit Buku');
});
});
});
describe('Test Edit Book Form', () => {
it('should can update book', async () => {
const mockData = [...dataJSON.books];
let mockDataEdit = mockData.shift();
let bookList = null;
await waitFor(() => {
bookList = windowJSDOM.document.querySelectorAll('.book-item');
});
const book1 = bookList[0].children;
const actions = book1[4].children;
window.fetch.mockResolvedValueOnce({
json: () => Promise.resolve(mockDataEdit),
});
fireEvent.click(actions[0]);
const lastCall = fetch.mock.lastCall;
expect(lastCall[0]).toBe('http://localhost:3333/books/1');
await waitFor(() => {
const h2 = windowJSDOM.document.getElementsByTagName('h2')[0];
expect(h2.textContent).toBe('Edit Buku');
});
const inputTitle = windowJSDOM.document.getElementById('title');
const inputAuthor = windowJSDOM.document.getElementById('author');
const inputYear = windowJSDOM.document.getElementById('year');
const inputQuantity = windowJSDOM.document.getElementById('quantity');
const buttonSubmit = windowJSDOM.document.querySelector('input[type="submit"]');
expect(inputTitle.value).toBe('Laskar Pelangi');
expect(inputAuthor.value).toBe('Andrea Hirata');
expect(inputYear.value).toBe('2005');
expect(inputQuantity.value).toBe('3');
fireEvent.change(inputTitle, { target: { value: 'Laskar Pelangi 2' } });
fireEvent.change(inputAuthor, { target: { value: 'Andrea Hirata 2' } });
fireEvent.change(inputYear, { target: { value: '2006' } });
fireEvent.change(inputQuantity, { target: { value: '4' } });
expect(inputTitle.value).toBe('Laskar Pelangi 2');
expect(inputAuthor.value).toBe('Andrea Hirata 2');
expect(inputYear.value).toBe('2006');
expect(inputQuantity.value).toBe('4');
window.fetch.mockResolvedValueOnce({
json: () =>
Promise.resolve({
message: 'Buku berhasil diperbarui',
}),
});
fireEvent.click(buttonSubmit);
const lastCallUpdate = fetch.mock.lastCall;
expect(lastCallUpdate[0]).toBe('http://localhost:3333/books/1');
expect(lastCallUpdate[1].method).toBe('PUT');
const editedBookData = [...dataJSON.books];
editedBookData[0] = {
id: 1,
title: 'Laskar Pelangi 2',
author: 'Andrea Hirata 2',
year: 2006,
quantity: 4,
};
window.fetch.mockResolvedValueOnce({
json: () => Promise.resolve(editedBookData),
});
await waitFor(() => {
const bookListAfterEdit = windowJSDOM.document.querySelectorAll('.book-item');
expect(bookListAfterEdit.length).toBe(15);
const book1AfterEdit = bookListAfterEdit[0].children;
expect(book1AfterEdit[0].textContent).toBe('Laskar Pelangi 2');
expect(book1AfterEdit[1].textContent).toBe('Andrea Hirata 2');
expect(book1AfterEdit[2].textContent).toBe('2006');
expect(book1AfterEdit[3].textContent).toBe('4');
});
});
});
describe('Test Add Book Form', () => {
test('if tag a clicked, move to "tambah buku" page and create a new data', async () => {
const a = windowJSDOM.document.querySelector('a');
fireEvent.click(a);
await waitFor(() => {
const h2 = windowJSDOM.document.getElementsByTagName('h2')[0];
expect(h2.textContent).toBe('Tambah Buku');
});
const inputTitle = windowJSDOM.document.getElementById('title');
const inputAuthor = windowJSDOM.document.getElementById('author');
const inputYear = windowJSDOM.document.getElementById('year');
const inputQuantity = windowJSDOM.document.getElementById('quantity');
const buttonSubmit = windowJSDOM.document.querySelector('input[type="submit"]');
fireEvent.change(inputTitle, { target: { value: 'Laskar Pelangi 2' } });
fireEvent.change(inputAuthor, { target: { value: 'Andrea Hirata 2' } });
fireEvent.change(inputYear, { target: { value: '2006' } });
fireEvent.change(inputQuantity, { target: { value: '99' } });
expect(inputTitle.value).toBe('Laskar Pelangi 2');
expect(inputAuthor.value).toBe('Andrea Hirata 2');
expect(inputYear.value).toBe('2006');
expect(inputQuantity.value).toBe('99');
window.fetch.mockResolvedValueOnce({
json: () => Promise.resolve({ message: 'Buku berhasil ditambahkan' }),
});
fireEvent.click(buttonSubmit);
expect(fetch.mock.lastCall[0]).toBe('http://localhost:3333/books');
expect(fetch.mock.lastCall[1].method).toBe('POST');
window.fetch.mockResolvedValueOnce({
json: () =>
Promise.resolve([
...dataJSON.books,
{
id: 16,
title: 'Laskar Pelangi 2',
author: 'Andrea Hirata 2',
year: 2006,
quantity: 99,
},
]),
});
await waitFor(() => {
const bookList = windowJSDOM.document.querySelectorAll('.book-item');
expect(bookList.length).toBe(16);
const book1 = bookList[15].children;
expect(book1[0].textContent).toBe('Laskar Pelangi 2');
expect(book1[1].textContent).toBe('Andrea Hirata 2');
expect(book1[2].textContent).toBe('2006');
expect(book1[3].textContent).toBe('99');
});
});
});
});
describe('Test Deploy to Netlify', () => {
it('should have same student name and student id on deploy site', async () => {
const url = `http://18.142.251.73:3000/scrape?url=${NetlifyDeployUrl}`;
const response = await axios.get(url);
const html = await response.data;
const { window } = new JSDOM(html);
const { document } = window;
const studentNameText = document.querySelector('.studentName');
const studentIdText = document.querySelector('.studentId');
expect(studentNameText.textContent).toBe(studentName);
expect(studentIdText.textContent).toBe(studentId);
});
});