-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
32 lines (22 loc) · 1.08 KB
/
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
const {addRequestToList, deleteRequests} = require('./static/assets/script');
test('addRequestToList adds a new item to the list', () => {
document.body.innerHTML = '<ul id="request-list"><li>list item</li></ul>';
addRequestToList('<li>another list item</li>');
const list = document.getElementById('request-list');
expect(list.children.length).toBe(2);
expect(list.firstChild.textContent).toBe('another list item');
expect(list.children[1].textContent).toBe('list item');
});
test('deleteRequests clears list and sends request to API', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({ok: true, json: () => Promise.resolve()})
);
document.body.innerHTML = '<ul id="request-list"><li>request 1</li></ul>';
await deleteRequests();
const list = document.getElementById('request-list');
expect(list.children.length).toBe(0);
expect(list.innerHTML).toBe('');
expect(global.fetch).toHaveBeenCalled();
expect(global.fetch).toHaveBeenCalledWith('/api/requests/delete', {method: 'POST'});
jest.restoreAllMocks();
});