-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathtest.js
105 lines (86 loc) · 3.1 KB
/
test.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
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
const Application = require("spectron").Application;
const electronPath = require("electron");
const {getActiveWindow, getWindows} = require("@nut-tree/nut-js");
const {POS_X, POS_Y, WIDTH, HEIGTH, TITLE} = require("./constants");
const {join} = require("path");
const sleep = async (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
};
let app;
const APP_TIMEOUT = 10000;
jest.setTimeout(3 * APP_TIMEOUT)
beforeEach(async () => {
app = new Application({
path: electronPath,
args: [join(__dirname, 'main.js')],
startTimeout: APP_TIMEOUT,
waitTimeout: APP_TIMEOUT,
});
await app.start();
await app.client.waitUntilWindowLoaded();
await app.browserWindow.minimize();
await app.browserWindow.restore();
await app.browserWindow.focus();
});
describe("getWindows", () => {
it("should list our started application window", async () => {
// GIVEN
const openWindows = await getWindows();
// WHEN
const windowNames = await Promise.all(openWindows.map((wnd) => wnd.title));
// THEN
expect(windowNames).toContain(TITLE);
});
});
describe("getActiveWindow", () => {
it("should return our started application window", async () => {
// GIVEN
// WHEN
const foregroundWindow = await getActiveWindow();
const windowTitle = await foregroundWindow.title;
// THEN
expect(windowTitle).toBe(TITLE);
});
it("should determine correct coordinates for our application", async () => {
// GIVEN
// WHEN
const foregroundWindow = await getActiveWindow();
const activeWindowRegion = await foregroundWindow.region;
// THEN
expect(activeWindowRegion.left).toBe(POS_X);
expect(activeWindowRegion.top).toBe(POS_Y);
expect(activeWindowRegion.width).toBe(WIDTH);
expect(activeWindowRegion.height).toBe(HEIGTH);
});
it("should determine correct coordinates for our application after moving the window", async () => {
// GIVEN
const xPosition = 42;
const yPosition = 23;
await app.browserWindow.setPosition(xPosition, yPosition);
await sleep(1000);
// WHEN
const foregroundWindow = await getActiveWindow();
const activeWindowRegion = await foregroundWindow.region;
// THEN
expect(activeWindowRegion.left).toBe(xPosition);
expect(activeWindowRegion.top).toBe(yPosition);
});
it("should determine correct window size for our application after resizing the window", async () => {
// GIVEN
const newWidth = 400;
const newHeight = 350;
await app.browserWindow.setSize(newWidth, newHeight);
await sleep(1000);
// WHEN
const foregroundWindow = await getActiveWindow();
const activeWindowRegion = await foregroundWindow.region;
// THEN
expect(activeWindowRegion.width).toBe(newWidth);
expect(activeWindowRegion.height).toBe(newHeight);
});
});
afterEach(async () => {
if (app && app.isRunning()) {
await app.stop();
}
});