-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist_users.test.js
57 lines (44 loc) · 1.5 KB
/
list_users.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
const $ = require('jquery');
global.$ = $;
const module = require('planttracer');
const list_users = module.list_users;
const list_users_data = jest.fn();
global.fetch = jest.fn();
document.body.innerHTML = '<div id="message"></div>';
describe('list_users', () => {
beforeEach(() => {
global.api_key = 'abcdefghijklmnopqrstuvwxyz'
global.API_BASE = 'https://planttracer.com'
});
test('should fetch user data and call list_users_data on success', async () => {
const mockResponse = {
error: false,
users: [
{ user_id: 1, name: 'User 1', email: '[email protected]' },
{ user_id: 2, name: 'User 2', email: '[email protected]' }
],
courses: [
{ course_id: 1, course_name: 'Course 1' },
{ course_id: 2, course_name: 'Course 2' }
]
};
global.fetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValueOnce(mockResponse)
});
module.list_users();
await Promise.resolve();
expect(fetch).toHaveBeenCalledWith(`${API_BASE}api/list-users`, expect.any(Object));
});
test('should display an error message if API returns an error', async () => {
const mockErrorResponse = {
error: true,
message: 'An error occurred while fetching users'
};
global.fetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValueOnce(mockErrorResponse)
});
module.list_users();
await Promise.resolve();
expect(fetch).toHaveBeenCalledWith(`${API_BASE}api/list-users`, expect.any(Object));
});
});