-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathutility.ts
81 lines (72 loc) · 2.27 KB
/
utility.ts
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
import { Page } from "@playwright/test";
import { Brand } from "@/type/utility";
import { success } from "@/type/result";
type TestFileId = Brand<string, "TestFileId">;
/** ファイル書き出し選択ダイアログをモックにする */
// TODO: モックを戻せるようにする
export async function mockShowSaveFileDialog(page: Page): Promise<{
getFileIds: () => Promise<TestFileId[]>;
}> {
type _Window = Window & {
_mockShowSaveFileDialog: {
returnValues: TestFileId[];
};
};
// モックを差し込む
await page.evaluate(() => {
const _window = window as unknown as _Window;
_window._mockShowSaveFileDialog = {
returnValues: [],
};
_window.backend.showSaveFileDialog = async () => {
const id = `${Date.now()}` as TestFileId;
_window._mockShowSaveFileDialog.returnValues.push(id);
return id;
};
});
return {
getFileIds: async () => {
return page.evaluate(() => {
const _window = window as unknown as _Window;
return _window._mockShowSaveFileDialog.returnValues;
});
},
};
}
/** ファイル書き出しをモックにする */
// TODO: モックを戻せるようにする
export async function mockWriteFile(page: Page): Promise<{
getWrittenFileBuffers: () => Promise<Record<string | TestFileId, Buffer>>;
}> {
type _Window = Window & {
_mockWriteFile: Record<string | TestFileId, Uint8Array>;
};
// モックを差し込む
await page.evaluate(
({ sucecssResult }) => {
const _window = window as unknown as _Window;
_window._mockWriteFile = {};
_window.backend.writeFile = async ({ filePath, buffer }) => {
_window._mockWriteFile[filePath] = new Uint8Array(buffer);
return sucecssResult;
};
},
{ sucecssResult: success(undefined) },
);
return {
getWrittenFileBuffers: async () => {
const arrays = await page.evaluate(() => {
const _window = window as unknown as _Window;
return Object.fromEntries(
Object.entries(_window._mockWriteFile).map(([key, value]) => [
key,
Array.from(value),
]),
);
});
return Object.fromEntries(
Object.entries(arrays).map(([key, value]) => [key, Buffer.from(value)]),
);
},
};
}