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
|
/*
* Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com>
*
* This file is part of Sophie.
*
* Sophie is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { URL } from 'node:url';
import { jest } from '@jest/globals';
import { fake } from '@sophie/test-utils';
import type { Event, HandlerDetails, WebContents } from 'electron';
import { mocked } from 'jest-mock';
import type Resources from '../../../resources/Resources';
import lockWebContentsToFile from '../lockWebContentsToFile';
type WillNavigateHandler = (event: Event, url: string) => void;
let willNavigate: WillNavigateHandler | undefined;
let windowOpenHandler:
| ((details: HandlerDetails) => { action: 'allow' | 'deny' })
| undefined;
const urlToLoad =
'file:///opt/sophie/resources/app.asar/packages/renderer/dist/index.html';
const fakeResources = fake<Resources>({
getRendererURL(path: string) {
return new URL(
path,
'file:///opt/sophie/resources/app.asar/packages/renderer/dist/',
).toString();
},
});
const fakeWebContents = fake<WebContents>({
setWindowOpenHandler(handler) {
windowOpenHandler = handler;
},
on(event, listener) {
if (event === 'will-navigate') {
willNavigate = listener as WillNavigateHandler;
}
return this as WebContents;
},
loadURL: jest.fn<WebContents['loadURL']>(),
});
const event: Event = {
preventDefault: jest.fn(),
};
beforeEach(async () => {
windowOpenHandler = undefined;
willNavigate = undefined;
mocked(fakeWebContents.loadURL).mockResolvedValueOnce();
await lockWebContentsToFile(fakeResources, 'index.html', fakeWebContents);
});
it('should load the specified file', () => {
expect(fakeWebContents.loadURL).toHaveBeenCalledWith(urlToLoad);
});
it('should set up will navigate and window open listeners', () => {
expect(willNavigate).toBeDefined();
expect(windowOpenHandler).toBeDefined();
});
it('should prevent opening a window', () => {
const { action } = windowOpenHandler!({
url: 'https://example.com',
frameName: 'newWindow',
features: '',
disposition: 'default',
referrer: {
url: urlToLoad,
policy: 'default',
},
});
expect(action).toBe('deny');
});
it('should allow navigation to the loaded URL', () => {
willNavigate!(event, urlToLoad);
expect(event.preventDefault).not.toHaveBeenCalled();
});
it('should not allow navigation to another URL', () => {
willNavigate!(
event,
'file:///opt/sophie/resources/app.asar/packages/renderer/not-allowed.html',
);
expect(event.preventDefault).toHaveBeenCalled();
});
|