Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ElectronTheme.test.ts

52 lines1.4 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import { beforeEach, vi } from "vitest";
4​
5const { onMock, removeListenerMock, themeState } = vi.hoisted(() => ({
6 onMock: vi.fn(),
7 removeListenerMock: vi.fn(),
8 themeState: {
9 shouldUseDarkColors: true,
10 themeSource: "system",
11 },
12}));
13​
14vi.mock("electron", () => ({
15 nativeTheme: {
16 get shouldUseDarkColors() {
17 return themeState.shouldUseDarkColors;
18 },
19 set themeSource(value: string) {
20 themeState.themeSource = value;
21 },
22 on: onMock,
23 removeListener: removeListenerMock,
24 },
25}));
26​
27import * as ElectronTheme from "./ElectronTheme.ts";
28​
29describe("ElectronTheme", () => {
30 beforeEach(() => {
31 onMock.mockClear();
32 removeListenerMock.mockClear();
33 themeState.shouldUseDarkColors = true;
34 themeState.themeSource = "system";
35 });
36​
37 it.effect("scopes native theme update listeners", () =>
38 Effect.gen(function* () {
39 const listener = vi.fn();
40​
41 yield* Effect.scoped(
42 Effect.gen(function* () {
43 const electronTheme = yield* ElectronTheme.ElectronTheme;
44 yield* electronTheme.onUpdated(listener);
45 }),
46 );
47​
48 assert.deepEqual(onMock.mock.calls, [["updated", listener]]);
49 assert.deepEqual(removeListenerMock.mock.calls, [["updated", listener]]);
50 }).pipe(Effect.provide(ElectronTheme.layer)),
51 );
52});