summaryrefslogtreecommitdiffhomepage
path: root/gui/test/e2e/utils.ts
blob: c8d75f61191192a91dc9ad0bc3e19f464a6396d1 (plain)
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
import { Locator, Page } from 'playwright';
import { _electron as electron, ElectronApplication } from 'playwright-core';

interface StartAppResponse {
  electronApp: ElectronApplication;
  appWindow: Page;
}

let electronApp: ElectronApplication;

export const startApp = async (): Promise<StartAppResponse> => {
  process.env.CI = 'e2e';

  electronApp = await electron.launch({
    args: ['build/test/e2e/setup/main.js'],
  });

  const appWindow = await electronApp.firstWindow();

  appWindow.on('pageerror', (error) => {
    console.log(error);
  });

  appWindow.on('console', (msg) => {
    console.log(msg.text());
  });

  return { electronApp, appWindow };
};

type MockIpcHandleProps<T> = {
  channel: string;
  response: T;
};

export const mockIpcHandle = async <T>({ channel, response }: MockIpcHandleProps<T>) => {
  await electronApp.evaluate(
    ({ ipcMain }, { channel, response }) => {
      ipcMain.removeHandler(channel);
      ipcMain.handle(channel, () => {
        return Promise.resolve({
          type: 'success',
          value: response,
        });
      });
    },
    { channel, response },
  );
};

type SendMockIpcResponseProps<T> = {
  channel: string;
  response: T;
};

export const sendMockIpcResponse = async <T>({ channel, response }: SendMockIpcResponseProps<T>) => {
  await electronApp.evaluate(
    ({ webContents }, { channel, response }) => {
      webContents.getAllWebContents()[0].send(channel, response);
    },
    { channel, response },
  );
};

const getStyleProperty = async (locator: Locator, property: string) => {
  return locator.evaluate(
    (el, { property }) => {
      return window.getComputedStyle(el).getPropertyValue(property);
    },
    { property },
  );
};

export const getColor = async (locator: Locator) => {
  return getStyleProperty(locator, 'color');
};

export const getBackgroundColor = async (locator: Locator) => {
  return getStyleProperty(locator, 'background-color');
};