diff options
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.ts | 114 |
1 files changed, 114 insertions, 0 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 new file mode 100644 index 0000000..29c0516 --- /dev/null +++ b/packages/main/src/infrastructure/electron/impl/__tests__/lockWebContentsToFile.spec.ts | |||
@@ -0,0 +1,114 @@ | |||
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 | |||
21 | import { URL } from 'node:url'; | ||
22 | |||
23 | import { jest } from '@jest/globals'; | ||
24 | import { fake } from '@sophie/test-utils'; | ||
25 | import type { Event, HandlerDetails, WebContents } from 'electron'; | ||
26 | import { mocked } from 'jest-mock'; | ||
27 | |||
28 | import { silenceLogger } from '../../../../utils/log'; | ||
29 | import type Resources from '../../../resources/Resources'; | ||
30 | import lockWebContentsToFile from '../lockWebContentsToFile'; | ||
31 | |||
32 | type WillNavigateHandler = (event: Event, url: string) => void; | ||
33 | |||
34 | let willNavigate: WillNavigateHandler | undefined; | ||
35 | |||
36 | let windowOpenHandler: | ||
37 | | ((details: HandlerDetails) => { action: 'allow' | 'deny' }) | ||
38 | | undefined; | ||
39 | |||
40 | const urlToLoad = | ||
41 | 'file:///opt/sophie/resources/app.asar/packages/renderer/dist/index.html'; | ||
42 | |||
43 | const 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 | |||
52 | const 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 | |||
65 | const event: Event = { | ||
66 | preventDefault: jest.fn(), | ||
67 | }; | ||
68 | |||
69 | beforeAll(() => { | ||
70 | silenceLogger(); | ||
71 | }); | ||
72 | |||
73 | beforeEach(async () => { | ||
74 | windowOpenHandler = undefined; | ||
75 | willNavigate = undefined; | ||
76 | mocked(fakeWebContents.loadURL).mockResolvedValueOnce(); | ||
77 | await lockWebContentsToFile(fakeResources, 'index.html', fakeWebContents); | ||
78 | }); | ||
79 | |||
80 | it('should load the specified file', () => { | ||
81 | expect(fakeWebContents.loadURL).toHaveBeenCalledWith(urlToLoad); | ||
82 | }); | ||
83 | |||
84 | it('should set up will navigate and window open listeners', () => { | ||
85 | expect(willNavigate).toBeDefined(); | ||
86 | expect(windowOpenHandler).toBeDefined(); | ||
87 | }); | ||
88 | |||
89 | it('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 | |||
103 | it('should allow navigation to the loaded URL', () => { | ||
104 | willNavigate!(event, urlToLoad); | ||
105 | expect(event.preventDefault).not.toHaveBeenCalled(); | ||
106 | }); | ||
107 | |||
108 | it('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 | }); | ||