Skip to content

Commit

Permalink
test(e2e): fix runtime window tests
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler committed Dec 8, 2024
1 parent 825dc0e commit df9fc98
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 54 deletions.
6 changes: 6 additions & 0 deletions apps/example-reducers/electron.vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { resolve } from 'node:path';

import { defineConfig, externalizeDepsPlugin } from 'electron-vite';
import react from '@vitejs/plugin-react';

Expand Down Expand Up @@ -29,6 +31,10 @@ export default defineConfig({
output: {
format: 'es',
},
input: {
mainWindow: resolve(__dirname, 'src/renderer/index.html'),
runtimeWindow: resolve(__dirname, 'src/renderer/runtimeWindow.html'),
},
},
},
},
Expand Down
7 changes: 3 additions & 4 deletions apps/example-reducers/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const icon = path.join(__dirname, '..', '..', 'resources', 'images', 'icon.png')
const windowOptions: BrowserWindowConstructorOptions = {
show: false,
icon,
title: 'zutron main window',
width: 256,
height: 256,
webPreferences: {
Expand Down Expand Up @@ -85,10 +84,10 @@ app
mainWindow.focus();

setTimeout(() => {
const runtimeWindow = new BrowserWindow({ ...windowOptions, title: 'zutron runtime window', show: true });
subscribe([runtimeWindow]);
const runtimeWindow = new BrowserWindow({ ...windowOptions, show: true });

runtimeWindow.loadFile(path.join(__dirname, '..', 'renderer', 'index.html'));
runtimeWindow.loadFile(path.join(__dirname, '..', 'renderer', 'runtimeWindow.html'));
subscribe([runtimeWindow]);

runtimeWindow.on('close', () => {
runtimeWindow.destroy();
Expand Down
2 changes: 1 addition & 1 deletion apps/example-reducers/src/renderer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8" />
<title>zutron example-reducers</title>
<title>zutron main window</title>
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta
http-equiv="Content-Security-Policy"
Expand Down
17 changes: 17 additions & 0 deletions apps/example-reducers/src/renderer/runtimeWindow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>zutron runtime window</title>
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:"
/>
</head>

<body>
<div id="root"></div>
<script type="module" src="/main.tsx"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions apps/example-separate-handlers/electron.vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { resolve } from 'node:path';

import { defineConfig, externalizeDepsPlugin } from 'electron-vite';
import react from '@vitejs/plugin-react';

Expand Down Expand Up @@ -29,6 +31,10 @@ export default defineConfig({
output: {
format: 'es',
},
input: {
mainWindow: resolve(__dirname, 'src/renderer/index.html'),
runtimeWindow: resolve(__dirname, 'src/renderer/runtimeWindow.html'),
},
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions apps/example-separate-handlers/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ app
mainWindow.focus();

setTimeout(() => {
const runtimeWindow = new BrowserWindow({ ...windowOptions, title: 'zutron runtime window', show: true });
const runtimeWindow = new BrowserWindow({ ...windowOptions, show: true });

runtimeWindow.loadFile(path.join(__dirname, '..', 'renderer', 'runtimeWindow.html'));
subscribe([runtimeWindow]);
runtimeWindow.loadFile(path.join(__dirname, '..', 'renderer', 'index.html'));

runtimeWindow.on('close', () => {
runtimeWindow.destroy();
});
}, 3000);
}, 1000);
})
.catch(console.error);
2 changes: 1 addition & 1 deletion apps/example-separate-handlers/src/renderer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8" />
<title>zutron example-separate-handlers</title>
<title>zutron main window</title>
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta
http-equiv="Content-Security-Policy"
Expand Down
17 changes: 17 additions & 0 deletions apps/example-separate-handlers/src/renderer/runtimeWindow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>zutron runtime window</title>
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:"
/>
</head>

<body>
<div id="root"></div>
<script type="module" src="/main.tsx"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions apps/example-store-handlers/electron.vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { resolve } from 'node:path';

import { defineConfig, externalizeDepsPlugin } from 'electron-vite';
import react from '@vitejs/plugin-react';

Expand Down Expand Up @@ -32,6 +34,10 @@ export default defineConfig({
output: {
format: 'es',
},
input: {
mainWindow: resolve(__dirname, 'src/renderer/index.html'),
runtimeWindow: resolve(__dirname, 'src/renderer/runtimeWindow.html'),
},
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions apps/example-store-handlers/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ app
mainWindow.focus();

setTimeout(() => {
const runtimeWindow = new BrowserWindow({ ...windowOptions, title: 'zutron runtime window', show: true });
const runtimeWindow = new BrowserWindow({ ...windowOptions, show: true });

runtimeWindow.loadFile(path.join(__dirname, '..', 'renderer', 'runtimeWindow.html'));
subscribe([runtimeWindow]);
runtimeWindow.loadFile(path.join(__dirname, '..', 'renderer', 'index.html'));

runtimeWindow.on('close', () => {
runtimeWindow.destroy();
});
}, 3000);
}, 1000);
})
.catch(console.error);
2 changes: 1 addition & 1 deletion apps/example-store-handlers/src/renderer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8" />
<title>zutron example-store-handlers</title>
<title>zutron main window</title>
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta
http-equiv="Content-Security-Policy"
Expand Down
17 changes: 17 additions & 0 deletions apps/example-store-handlers/src/renderer/runtimeWindow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>zutron runtime window</title>
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:"
/>
</head>

<body>
<div id="root"></div>
<script type="module" src="/main.tsx"></script>
</body>
</html>
93 changes: 52 additions & 41 deletions e2e/test/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ import { expect } from '@wdio/globals';
import { browser } from 'wdio-electron-service';
import { setupBrowser, type WebdriverIOQueries } from '@testing-library/webdriverio';

const windowHandles = new Map<string, string>();

const waitUntilWindowsAvailable = async (desiredWindows: number) =>
await browser.waitUntil(async () => {
const numWindows = (await browser.getWindowHandles()).length;
return numWindows > desiredWindows;
const handles = await browser.getWindowHandles();
for (const handle of handles) {
await browser.switchToWindow(handle);
const title = await browser.getTitle();
windowHandles.set(title, handle);
}
return handles.length === desiredWindows;
});

const switchToWindow = async (windowName: string) => {
await browser.switchToWindow(windowHandles.get(windowName) as string);
};

let screen: WebdriverIOQueries;

before(() => {
Expand Down Expand Up @@ -37,45 +48,6 @@ describe('click events', () => {
expect(await screen.getByText('0')).toBeDefined();
});

describe('in an app with a second window created at runtime', () => {
beforeEach(async () => {
await waitUntilWindowsAvailable(2);
await browser.switchToWindow('zutron main window');
});

it('should increment the counter in the other window when the main window increment button is clicked', async () => {
const incrementButton = await screen.getByText('increment');

await incrementButton.click();
await browser.switchToWindow('zutron runtime window');
expect(await screen.getByText('1')).toBeDefined();
await browser.switchToWindow('zutron main window');
await incrementButton.click();
await browser.switchToWindow('zutron runtime window');
expect(await screen.getByText('2')).toBeDefined();
await browser.switchToWindow('zutron main window');
await incrementButton.click();
await browser.switchToWindow('zutron runtime window');
expect(await screen.getByText('3')).toBeDefined();
});

it('should decrement the counter in the other window when the main window decrement button is clickede', async () => {
const decrementButton = await screen.getByText('decrement');

await decrementButton.click();
await browser.switchToWindow('zutron runtime window');
expect(await screen.getByText('2')).toBeDefined();
await browser.switchToWindow('zutron main window');
await decrementButton.click();
await browser.switchToWindow('zutron runtime window');
expect(await screen.getByText('1')).toBeDefined();
await browser.switchToWindow('zutron main window');
await decrementButton.click();
await browser.switchToWindow('zutron runtime window');
expect(await screen.getByText('0')).toBeDefined();
});
});

// Setting badge count is supported on macOS and Linux
// However, Linux support is limited to Unity, which is not the default desktop environment for Ubuntu
if (process.platform === 'darwin') {
Expand Down Expand Up @@ -131,4 +103,43 @@ describe('click events', () => {
expect(badgeCount).toBe(0);
});
}

describe('in an app with a second window created at runtime', () => {
beforeEach(async () => {
await waitUntilWindowsAvailable(2);
await switchToWindow('zutron main window');
});

it('should increment the counter in the other window when the main window increment button is clicked', async () => {
const incrementButton = await screen.getByText('increment');

await incrementButton.click();
await switchToWindow('zutron runtime window');
expect(await screen.getByText('1')).toBeDefined();
await switchToWindow('zutron main window');
await incrementButton.click();
await switchToWindow('zutron runtime window');
expect(await screen.getByText('2')).toBeDefined();
await switchToWindow('zutron main window');
await incrementButton.click();
await switchToWindow('zutron runtime window');
expect(await screen.getByText('3')).toBeDefined();
});

it('should decrement the counter in the other window when the main window decrement button is clickede', async () => {
const decrementButton = await screen.getByText('decrement');

await decrementButton.click();
await switchToWindow('zutron runtime window');
expect(await screen.getByText('2')).toBeDefined();
await switchToWindow('zutron main window');
await decrementButton.click();
await switchToWindow('zutron runtime window');
expect(await screen.getByText('1')).toBeDefined();
await switchToWindow('zutron main window');
await decrementButton.click();
await switchToWindow('zutron runtime window');
expect(await screen.getByText('0')).toBeDefined();
});
});
});

0 comments on commit df9fc98

Please sign in to comment.