-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.spec.ts
More file actions
187 lines (161 loc) · 6.6 KB
/
api.spec.ts
File metadata and controls
187 lines (161 loc) · 6.6 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { browser } from '@wdio/electron-service';
import { expect } from '@wdio/globals';
// Check if we're running in script mode
const isBinary = process.env.BINARY !== 'false';
// Helper function to get the expected app name from globalThis.packageJson
const getExpectedAppName = (): string => {
// If running in binary mode, use the package name from globalThis
if (isBinary && globalThis.packageJson?.name) {
return globalThis.packageJson.name;
}
// In script mode, the app name will always be "Electron"
return 'Electron';
};
const getExpectedAppVersion = async (): Promise<string> => {
// If running in binary mode, use the package name from globalThis
if (isBinary && globalThis.packageJson?.version) {
return globalThis.packageJson.version;
}
// In script mode, the version should match the Electron version
const electronVersion = await browser.electron.execute((_electron) => process.versions.electron);
// Handle multiremote mode - take first result since all instances should have same version
if (Array.isArray(electronVersion)) {
return electronVersion[0];
}
return electronVersion;
};
describe('Electron APIs', () => {
beforeEach(async () => {
// Reset app name to original value to ensure test isolation
const expectedName = getExpectedAppName();
await browser.electron.execute((electron, appName) => electron.app.setName(appName), expectedName);
});
it('should retrieve the app name through the electron API', async () => {
const appName = await browser.electron.execute((electron) => electron.app.getName());
const expectedName = getExpectedAppName();
expect(appName).toBe(expectedName);
});
it('should retrieve the app version through the electron API', async () => {
const appVersion = await browser.electron.execute((electron) => electron.app.getVersion());
const expectedVersion = await getExpectedAppVersion();
expect(appVersion).toBe(expectedVersion);
});
describe('execute', () => {
it('should execute a function', async () => {
expect(await browser.electron.execute(() => 1 + 2 + 3)).toEqual(6);
});
it('should execute a function in the electron main process', async () => {
const result = await browser.electron.execute(
(electron, a, b, c) => {
const version = electron.app.getVersion();
return [version, a + b + c];
},
1,
2,
3,
);
// Check that we get a valid version (don't compare exact version)
expect(result[0]).toMatch(/^\d+\.\d+\.\d+/);
expect(result[1]).toEqual(6);
});
it('should execute a stringified function', async () => {
await expect(browser.electron.execute('() => 1 + 2 + 3')).resolves.toEqual(6);
});
it('should execute a stringified function in the electron main process', async () => {
// Don't check for specific version, just verify it returns a valid semver string
await expect(browser.electron.execute('(electron) => electron.app.getVersion()')).resolves.toMatch(
/^\d+\.\d+\.\d+/,
);
});
describe('execute - different script types', () => {
it('should execute function with args (with-args branch)', async () => {
const result = await browser.electron.execute(
(electron, arg1, arg2) => {
return { appName: electron.app.getName(), arg1, arg2 };
},
'first',
'second',
);
expect(result.appName).toBeDefined();
expect(result.arg1).toBe('first');
expect(result.arg2).toBe('second');
});
it('should execute statement-style string (return statement)', async () => {
const result = await browser.electron.execute('return 42');
expect(result).toBe(42);
});
it('should execute expression-style string', async () => {
const result = await browser.electron.execute('1 + 2 + 3');
expect(result).toBe(6);
});
it('should execute string with variable declaration', async () => {
const result = await browser.electron.execute(`
const x = 10;
const y = 20;
return x + y;
`);
expect(result).toBe(30);
});
it('should execute function without args', async () => {
const result = await browser.electron.execute((electron) => {
return { name: electron.app.getName() };
});
expect(result.name).toBeDefined();
});
it('should execute async function with args', async () => {
const result = await browser.electron.execute(async (electron, value) => {
await new Promise((resolve) => setTimeout(resolve, 10));
return { received: value, appName: electron.app.getName() };
}, 'async-test');
expect(result.received).toBe('async-test');
expect(result.appName).toBeDefined();
});
});
describe('workaround for TSX issue', () => {
// Tests for the following issue - can be removed when the TSX issue is resolved
// https://github.com/webdriverio-community/wdio-electron-service/issues/756
// https://github.com/privatenumber/tsx/issues/113
it('should handle executing a function which declares a function', async () => {
expect(
await browser.electron.execute(() => {
function innerFunc() {
return 'executed inner function';
}
return innerFunc();
}),
).toEqual('executed inner function');
});
it('should handle executing a function which declares an arrow function', async () => {
expect(
await browser.electron.execute(() => {
const innerFunc = () => 'executed inner function';
return innerFunc();
}),
).toEqual('executed inner function');
});
});
});
});
describe('browser.execute - workaround for TSX issue', () => {
// Tests for the following issue - can be removed when the TSX issue is resolved
// https://github.com/webdriverio-community/wdio-electron-service/issues/756
// https://github.com/privatenumber/tsx/issues/113
it('should handle executing a function which declares a function', async () => {
expect(
await browser.execute(() => {
function innerFunc() {
return 'executed inner function';
}
return innerFunc();
}),
).toEqual('executed inner function');
});
it('should handle executing a function which declares an arrow function', async () => {
expect(
await browser.execute(() => {
const innerFunc = () => 'executed inner function';
return innerFunc();
}),
).toEqual('executed inner function');
});
});