forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme-settings.spec.js
77 lines (65 loc) · 2.46 KB
/
theme-settings.spec.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
let fetchThemeSettings;
let getSetting;
let themeSettingsState;
const themeSettings = {
theme: 'foo',
};
const jsonMock = jest.fn().mockResolvedValue(themeSettings);
const fetchMock = jest.fn().mockResolvedValue({
json: jsonMock,
});
const resolveAbsoluteUrlMock = jest.fn();
const mockUrlHelper = {
resolveAbsoluteUrl: resolveAbsoluteUrlMock,
};
jest.mock('docc-render/utils/url-helper', () => (mockUrlHelper));
function importDeps() {
jest.resetModules();
// eslint-disable-next-line global-require
({ fetchThemeSettings, getSetting, themeSettingsState } = require('@/utils/theme-settings'));
}
window.fetch = fetchMock;
describe('theme-settings', () => {
beforeEach(() => {
importDeps();
jest.clearAllMocks();
});
it('fetches the theme settings from a remote path', async () => {
window.baseUrl = '/';
resolveAbsoluteUrlMock.mockReturnValue('http://localhost/theme-settings.json');
importDeps();
await fetchThemeSettings();
expect(resolveAbsoluteUrlMock).toHaveBeenCalledWith('/theme-settings.json');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith('http://localhost/theme-settings.json');
expect(jsonMock).toHaveBeenCalledTimes(1);
});
it('uses the window.baseUrl for the json path', async () => {
window.baseUrl = '/bar/foo/';
resolveAbsoluteUrlMock.mockReturnValue(`http://localhost${window.baseUrl}theme-settings.json`);
importDeps();
await fetchThemeSettings();
expect(resolveAbsoluteUrlMock).toHaveBeenCalledWith('/theme-settings.json');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith('http://localhost/bar/foo/theme-settings.json');
expect(jsonMock).toHaveBeenCalledTimes(1);
});
it('silences errors while fetching theme settings', async () => {
fetchMock.mockRejectedValueOnce('Foo is not JSON');
expect(await fetchThemeSettings()).toEqual({});
});
it('retrieves already stored data', async () => {
expect(getSetting(['theme'])).toEqual({});
Object.assign(themeSettingsState, themeSettings);
expect(getSetting(['theme'])).toEqual(themeSettings.theme);
});
});