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
/
ContentImpl.test.ts
116 lines (96 loc) · 3.96 KB
/
ContentImpl.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
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
// Copyright 2021 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 { RestoreConsole, default as mockConsole } from 'jest-mock-console';
import { clear as kvClear } from 'idb-keyval';
import mockFetch from 'jest-fetch-mock';
import { ContentImpl } from './ContentImpl';
import { DateTime } from './DateTime';
describe('ContentImpl', () => {
let restoreConsole: RestoreConsole | undefined = undefined;
afterEach(() => restoreConsole!());
beforeEach(async () => {
// (1) Install the moacked console, to catch console.error() messages.
restoreConsole = mockConsole();
// (2) Clear the cache, as this test suite depends on validating caching behaviour.
await kvClear();
});
it('should reflect the values of a valid content from the network', async () => {
const currentTime = DateTime.local().unix();
mockFetch.mockOnceIf('/api/content', async request => ({
body: JSON.stringify({
pages: [
{
pathname: '/foo',
content: 'Foo!',
modified: currentTime,
},
{
pathname: '/bar',
content: 'Bar?',
modified: currentTime + 30,
}
],
}),
status: 200,
}));
const content = new ContentImpl();
expect(await content.initialize()).toBeTruthy();
expect(content.has('/foo')).toBeTruthy();
expect(content.get('/foo')?.pathname).toEqual('/foo');
expect(content.get('/foo')?.content).toEqual('Foo!');
expect(content.get('/foo')?.modified).toEqual(DateTime.fromUnix(currentTime));
expect(content.has('/bar')).toBeTruthy();
expect(content.get('/bar')?.pathname).toEqual('/bar');
expect(content.get('/bar')?.content).toEqual('Bar?');
expect(content.get('/bar')?.modified).toEqual(DateTime.fromUnix(currentTime + 30));
expect(content.has('/baz')).toBeFalsy();
});
it('should fail when the API endpoint is unavailable', async () => {
mockFetch.mockOnceIf('/api/content', async request => ({
status: 404,
}));
const content = new ContentImpl();
expect(await content.initialize()).toBeFalsy();
expect(console.error).toHaveBeenCalledTimes(0);
expect(() => content.has('foo')).toThrowError();
expect(() => content.get('baz')).toThrowError();
});
it('should be able to return prefixed paths in length order', async () => {
const currentTime = DateTime.local().unix();
mockFetch.mockOnceIf('/api/content', async request => ({
body: JSON.stringify({
pages: [
{
pathname: '/foo/',
content: 'Foo Index',
modified: currentTime,
},
{
pathname: '/foo/bar.html',
content: 'Bar!',
modified: currentTime + 30,
}
]
}),
status: 200,
}));
const content = new ContentImpl();
expect(await content.initialize()).toBeTruthy();
expect(content.has('/foo/')).toBeTruthy();
expect(content.has('/foo/bar.html')).toBeTruthy();
expect(content.getPrefixed('/foo/')).toHaveLength(2);
expect(content.getPrefixed('/foo/')).toStrictEqual([
{
pathname: '/foo/bar.html',
content: 'Bar!',
modified: DateTime.fromUnix(currentTime + 30),
},
{
pathname: '/foo/',
content: 'Foo Index',
modified: DateTime.fromUnix(currentTime),
}
]);
});
});