-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.spec.ts
More file actions
89 lines (79 loc) · 3.42 KB
/
api.spec.ts
File metadata and controls
89 lines (79 loc) · 3.42 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
import { browser, expect } from '@wdio/globals';
import '@wdio/native-types';
describe('Tauri API', () => {
it('should execute basic commands', async () => {
// Test basic command execution using the execute API
const result = await browser.tauri.execute(({ core }) => core.invoke('get_platform_info'));
expect(result).toHaveProperty('os');
expect(result).toHaveProperty('arch');
expect(result).toHaveProperty('hostname');
expect(result).toHaveProperty('memory');
expect(result).toHaveProperty('cpu');
});
it('should handle command errors gracefully', async () => {
// Test error handling for invalid commands
await expect(browser.tauri.execute(({ core }) => core.invoke('invalid_command'))).rejects.toThrow();
});
it('should execute commands with parameters', async () => {
// Test command execution with parameters
const result = (await browser.tauri.execute(({ core }) => core.invoke('get_platform_info'))) as { os: string };
expect(result).toHaveProperty('os');
expect(typeof result.os).toBe('string');
});
describe('execute - different script types', () => {
it('should execute function with Tauri APIs and args (with-args branch)', async () => {
// This tests the with-args branch: function receives Tauri APIs as first param, user args after
const result = await browser.tauri.execute(
(tauri, arg1, arg2) => {
return { tauriHasCore: typeof tauri?.core?.invoke === 'function', arg1, arg2 };
},
'first',
'second',
);
expect(result.tauriHasCore).toBe(true);
expect(result.arg1).toBe('first');
expect(result.arg2).toBe('second');
});
it('should execute statement-style string (return statement)', async () => {
// This tests the no-args branch with statement-style script like "return document.title"
const result = await browser.tauri.execute('return 42');
expect(result).toBe(42);
});
it('should execute expression-style string', async () => {
// This tests the no-args branch with expression-style script
const result = await browser.tauri.execute('1 + 2 + 3');
expect(result).toBe(6);
});
it('should execute string with variable declaration', async () => {
// Statement-style: declare variables and return
const result = await browser.tauri.execute(`
const x = 10;
const y = 20;
return x + y;
`);
expect(result).toBe(30);
});
it('should execute function with Tauri APIs (no args)', async () => {
// Function without args should still receive Tauri APIs
const result = await browser.tauri.execute((tauri) => {
return { hasCore: typeof tauri?.core !== 'undefined' };
});
expect(result.hasCore).toBe(true);
});
it('should execute string that accesses Tauri APIs', async () => {
// String script that uses window.__TAURI__ directly
const result = await browser.tauri.execute(`
return typeof window.__TAURI__?.core;
`);
expect(result).toBe('object');
});
it('should execute async function with args', async () => {
const result = await browser.tauri.execute(async (tauri, value) => {
await new Promise((resolve) => setTimeout(resolve, 10));
return { received: value, hasTauri: !!tauri?.core };
}, 'async-test');
expect(result.received).toBe('async-test');
expect(result.hasTauri).toBe(true);
});
});
});