This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Notes.test.ts
73 lines (57 loc) · 2.37 KB
/
Notes.test.ts
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
// Copyright 2022 Peter Beverloo & AnimeCon. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.
import mockFetch from 'jest-fetch-mock';
import { UserImpl } from './UserImpl';
import { uploadNotes } from './Notes';
describe('Notes', () => {
beforeEach(() => {
mockFetch.mockOnceIf('/api/auth', async request => ({
body: JSON.stringify({ authToken: 'my-token' }),
status: 200,
}));
mockFetch.mockOnceIf('/api/user?authToken=my-token', async request => ({
body: JSON.stringify({
administrator: false,
events: { '2022-regular': 'Volunteer' },
name: 'Volunteer Joe',
}),
status: 200,
}));
});
it('should be able to issue a valid API request to the server', async () => {
const user = new UserImpl();
expect(await user.authenticate({
emailAddress: '[email protected]',
accessCode: '1234'
})).toBeTruthy();
expect(user.authenticated).toBeTruthy();
mockFetch.mockOnceIf('/api/notes?authToken=my-token&event=2022', async request => ({
body: JSON.stringify({ notes: await request.text() }),
status: 200,
}));
const result = await uploadNotes(user, '2022', 'event', 'my-event-id', 'hello, world!');
// No, this isn't what's being uploaded. This is a change detector test to identify when we
// can actually test this, depending on NodeJS implementing and supporting FormData.
expect(result.error).toBeUndefined();
expect(result.notes).toEqual('[object FormData]');
});
it('should be able to throw an exception on server failure', async () => {
const user = new UserImpl();
expect(await user.authenticate({
emailAddress: '[email protected]',
accessCode: '1234'
})).toBeTruthy();
expect(user.authenticated).toBeTruthy();
mockFetch.mockOnceIf('/api/notes?authToken=my-token&event=2022', async request => ({
status: 404,
}));
let thrown = false;
try {
const result = await uploadNotes(user, '2022', 'event', 'my-event-id', 'hello, world!');
} catch (error) {
thrown = true;
}
expect(thrown).toBeTruthy();
});
});