aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/infrastructure/electron/impl/__tests__/lockWebContentsToFile.spec.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/infrastructure/electron/impl/__tests__/lockWebContentsToFile.spec.ts')
-rw-r--r--packages/main/src/infrastructure/electron/impl/__tests__/lockWebContentsToFile.spec.ts114
1 files changed, 0 insertions, 114 deletions
diff --git a/packages/main/src/infrastructure/electron/impl/__tests__/lockWebContentsToFile.spec.ts b/packages/main/src/infrastructure/electron/impl/__tests__/lockWebContentsToFile.spec.ts
deleted file mode 100644
index 29c0516..0000000
--- a/packages/main/src/infrastructure/electron/impl/__tests__/lockWebContentsToFile.spec.ts
+++ /dev/null
@@ -1,114 +0,0 @@
1/*
2 * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import { URL } from 'node:url';
22
23import { jest } from '@jest/globals';
24import { fake } from '@sophie/test-utils';
25import type { Event, HandlerDetails, WebContents } from 'electron';
26import { mocked } from 'jest-mock';
27
28import { silenceLogger } from '../../../../utils/log';
29import type Resources from '../../../resources/Resources';
30import lockWebContentsToFile from '../lockWebContentsToFile';
31
32type WillNavigateHandler = (event: Event, url: string) => void;
33
34let willNavigate: WillNavigateHandler | undefined;
35
36let windowOpenHandler:
37 | ((details: HandlerDetails) => { action: 'allow' | 'deny' })
38 | undefined;
39
40const urlToLoad =
41 'file:///opt/sophie/resources/app.asar/packages/renderer/dist/index.html';
42
43const fakeResources = fake<Resources>({
44 getRendererURL(path: string) {
45 return new URL(
46 path,
47 'file:///opt/sophie/resources/app.asar/packages/renderer/dist/',
48 ).toString();
49 },
50});
51
52const fakeWebContents = fake<WebContents>({
53 setWindowOpenHandler(handler) {
54 windowOpenHandler = handler;
55 },
56 on(event, listener) {
57 if (event === 'will-navigate') {
58 willNavigate = listener as WillNavigateHandler;
59 }
60 return this as WebContents;
61 },
62 loadURL: jest.fn(),
63});
64
65const event: Event = {
66 preventDefault: jest.fn(),
67};
68
69beforeAll(() => {
70 silenceLogger();
71});
72
73beforeEach(async () => {
74 windowOpenHandler = undefined;
75 willNavigate = undefined;
76 mocked(fakeWebContents.loadURL).mockResolvedValueOnce();
77 await lockWebContentsToFile(fakeResources, 'index.html', fakeWebContents);
78});
79
80it('should load the specified file', () => {
81 expect(fakeWebContents.loadURL).toHaveBeenCalledWith(urlToLoad);
82});
83
84it('should set up will navigate and window open listeners', () => {
85 expect(willNavigate).toBeDefined();
86 expect(windowOpenHandler).toBeDefined();
87});
88
89it('should prevent opening a window', () => {
90 const { action } = windowOpenHandler!({
91 url: 'https://example.com',
92 frameName: 'newWindow',
93 features: '',
94 disposition: 'default',
95 referrer: {
96 url: urlToLoad,
97 policy: 'default',
98 },
99 });
100 expect(action).toBe('deny');
101});
102
103it('should allow navigation to the loaded URL', () => {
104 willNavigate!(event, urlToLoad);
105 expect(event.preventDefault).not.toHaveBeenCalled();
106});
107
108it('should not allow navigation to another URL', () => {
109 willNavigate!(
110 event,
111 'file:///opt/sophie/resources/app.asar/packages/renderer/not-allowed.html',
112 );
113 expect(event.preventDefault).toHaveBeenCalled();
114});