Skip to content

Commit

Permalink
feat: added tests for createWindow and unpack, moved jest.setup.ts to…
Browse files Browse the repository at this point in the history
… the root, installed jest-mock-extended (#51)

## Changes

* Added tests for `unpack` and `create-window`
* Added `jest-mock-extended` to improve type safety
* Added missing `archive.mjs` that was lost in one other PR and is
needed for the future build
  • Loading branch information
TimPietrusky authored Feb 21, 2024
1 parent 382b8ce commit 760f3ee
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 40 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const jestConfig = {
},
transformIgnorePatterns: ["/node_modules/"],
extensionsToTreatAsEsm: [".ts", ".tsx"],
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
};

export default jestConfig;
19 changes: 0 additions & 19 deletions jest.config.unit.mjs

This file was deleted.

8 changes: 0 additions & 8 deletions tests/jest.setup.js → jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
global.console = {
log: console.log,
error: console.error,
warn: console.warn,
info: console.info,
debug: console.debug,
};

jest.mock("electron", () => {
const mockBrowserWindow = jest.fn().mockImplementation(() => ({
loadURL: jest.fn(),
Expand Down
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"toc": "npx markdown-toc README.md -i",
"tsc:noEmit": "tsc --noEmit",
"caption:test": "ts-node-esm main/captions/misc.ts",
"test:unit": "jest --runInBand --config jest.config.unit.mjs --verbose",
"test:unit": "jest --runInBand --config jest.config.ts --verbose",
"pretest:e2e": "nextron build --no-pack",
"test:e2e": "npx playwright test"
},
Expand Down Expand Up @@ -100,6 +100,7 @@
"jest": "^29.7.0",
"jest-config": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-mock-extended": "^3.0.5",
"jest-ts-webcompat-resolver": "^1.0.0",
"jotai": "2.6.3",
"json5": "^2.2.3",
Expand Down
30 changes: 30 additions & 0 deletions scripts/archive.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import fsp from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

import Seven from "node-7z";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const my7z = path.join(__dirname, "..", "resources", "7zip", "win", "7za.exe");

const sourceFolder = path.join(__dirname, "..", "resources", "python-embedded");
const outputArchive = path.join(__dirname, "..", "resources", "python-embedded.7z");

const files = await fsp.readdir(sourceFolder);

const archive = Seven.add(
outputArchive,
files.map(file => path.join(sourceFolder, file)),
{
$bin: my7z,
recursive: true,
}
);

archive.on("end", () => {
console.log("Compression finished");
});

archive.on("error", error => {
console.error("Compression error:", error);
});
60 changes: 60 additions & 0 deletions src/electron/future/utils/__test__/create-window.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { BrowserWindow } from "electron";
import Store from "electron-store";
import type { DeepMockProxy } from "jest-mock-extended";
import { mockDeep } from "jest-mock-extended";

import { createWindow } from "@/utils/create-window";

jest.mock("electron", () => ({
BrowserWindow: jest.fn().mockImplementation(() => ({
webContents: {
setWindowOpenHandler: jest.fn(),
},
on: jest.fn(),
isMinimized: jest.fn(),
isMaximized: jest.fn(),
})),
shell: {
openExternal: jest.fn(),
},
screen: {
getAllDisplays: jest.fn().mockReturnValue([
{
bounds: { x: 0, y: 0, width: 1024, height: 768 },
},
]),
getPrimaryDisplay: jest.fn().mockReturnValue({
bounds: { x: 0, y: 0, width: 1920, height: 1080 },
workArea: { x: 0, y: 0, width: 1920, height: 1040 },
}),
},
}));

jest.mock("electron-store", () => ({
__esModule: true,
default: jest.fn(),
}));

describe("createWindow", () => {
const defaultState = { x: 100, y: 100, width: 1024, height: 768 };
let storeMock: DeepMockProxy<Store>;

beforeEach(() => {
storeMock = mockDeep<Store>();
(Store as unknown as jest.Mock).mockImplementation(() => storeMock);
storeMock.get.mockReturnValue(defaultState);
});

it("should create a BrowserWindow with default size when no state is saved", async () => {
await createWindow("testWindow", {});

expect(BrowserWindow).toHaveBeenCalledWith(
expect.objectContaining({
webPreferences: expect.objectContaining({
contextIsolation: true,
nodeIntegration: false,
}),
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import Seven from "node-7z";
import { unpack } from "@/utils/unpack";

jest.mock("node-7z", () => ({
extractFull: jest.fn().mockReturnValue({
extractFull: jest.fn().mockImplementation(() => ({
on: jest.fn().mockImplementation((event, handler) => {
if (event === "end") {
process.nextTick(handler);
}

return this;
}),
}),
})),
}));

describe("electron/utils/unpack.ts", () => {
Expand Down
8 changes: 0 additions & 8 deletions tests/unit/main.helpers.caption.test.ts

This file was deleted.

0 comments on commit 760f3ee

Please sign in to comment.