Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ittechhunter committed Jun 4, 2024
1 parent b5f8902 commit 950197d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
20 changes: 11 additions & 9 deletions lib/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,23 @@ export async function addApps(srcs: string[], dists: string[]) {
io.emit("fileChange", appDevJson);
}

fileWatcher.add(srcs.map((src) => [
path.join(src, "widget/**/*"),
path.join(src, "module/**/*"),
path.join(src, "ipfs/**/*"),
path.join(src, "bos.config.json"),
path.join(src, "aliases.json")
]).flat()
);
if (fileWatcher) {
fileWatcher.add(srcs.map((src) => [
path.join(src, "widget/**/*"),
path.join(src, "module/**/*"),
path.join(src, "ipfs/**/*"),
path.join(src, "bos.config.json"),
path.join(src, "aliases.json")
]).flat()
);
}
}

async function fileWatcherCallback(action: string, file: string) {
let appDevJson = await readJson(appDevJsonPath, { throws: false });

// find which app this file belongs to
const index = appSrcs.findIndex((src) => file.includes(src));
const index = appSrcs.findIndex((src) => file.includes(path.resolve(src)));
if (index == -1) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Gaze } from "gaze";

export function startFileWatcher(watchPaths: string[], callback: Function): Gaze {
let gaze = new Gaze(watchPaths, { debounceDelay: 100 });
const gaze = new Gaze(watchPaths, { debounceDelay: 100 });

// @ts-ignore
gaze.on("all", callback);

Expand Down
50 changes: 40 additions & 10 deletions tests/unit/dev.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { buildApp } from "@/lib/build";
import { DEFAULT_CONFIG, loadConfig } from "@/lib/config";
import { dev, DevOptions } from "@/lib/dev";
import { dev, DevOptions, addApps } from "@/lib/dev";
import { Logger, LogLevel } from "@/lib/logger";
import { startDevServer } from "@/lib/server";
import { startSocket } from "@/lib/socket";
import { startFileWatcher } from "@/lib/watcher";
import http from "http";
import path from "path";
import { Server as IoServer } from "socket.io";
import { Gaze } from "gaze";

import { vol } from 'memfs';
jest.mock('fs', () => require('memfs').fs);
Expand All @@ -18,11 +20,15 @@ const app_example_1 = {
"./build/bos-loader.json": JSON.stringify({ "components": { "test.testnet/widget/home": { "code": "return <p>hello world</p>" } }, "data": {} }),
};

const app_example_2 = {
"./build/bos-loader.json": JSON.stringify({ "components": { "test.testnet/widget/home": { "code": "return <p>goodbye nothing</p>" } }, "data": {} }),
};

jest.mock("@/lib/config");
jest.mock("@/lib/server");
jest.mock("@/lib/socket");
jest.mock("@/lib/watcher");
jest.mock("@/lib/build")
jest.mock("@/lib/build");

describe("dev", () => {
const mockSrc = "/app_example_1";
Expand All @@ -36,7 +42,8 @@ describe("dev", () => {

(loadConfig as jest.MockedFunction<typeof loadConfig>).mockResolvedValue(mockConfig);
(startDevServer as jest.MockedFunction<typeof startDevServer>).mockReturnValue({} as http.Server);
(startSocket as jest.MockedFunction<typeof startSocket>).mockReturnValue({} as IoServer);
(startSocket as jest.MockedFunction<typeof startSocket>).mockReturnValue(new IoServer());
(startFileWatcher as jest.MockedFunction<typeof startFileWatcher>).mockReturnValue(new Gaze(mockSrc));
(buildApp as jest.MockedFunction<typeof buildApp>).mockReturnValue({} as Promise<any>);
});

Expand All @@ -52,8 +59,9 @@ describe("dev", () => {

it("should call generateApp with src, dist, config, opts, and devJsonPath", async () => {
await dev(mockSrc, mockOpts);
const mockDevJsonPath = `${mockSrc}/build/bos-loader.json`;
expect(startDevServer).toHaveBeenCalledWith(mockDevJsonPath, mockOpts);
const mockDist = path.join(mockSrc, 'build');
const mockDevJsonPath = path.join(mockSrc, 'build', 'bos-loader.json');
expect(startDevServer).toHaveBeenCalledWith([mockSrc], [mockDist], mockDevJsonPath, mockOpts);
});

it("should start the socket server if hot reload is enabled", async () => {
Expand All @@ -71,12 +79,34 @@ describe("dev", () => {
it("should call startFileWatcher with correct watch paths", async () => {
await dev(mockSrc, mockOpts);
const expectedWatchPaths = [
`${mockSrc}/widget/**/*`,
`${mockSrc}/module/**/*`,
`${mockSrc}/ipfs/**/*`,
`${mockSrc}/bos.config.json`,
`${mockSrc}/aliases.json`,
path.join(mockSrc, 'widget', '**', '*'),
path.join(mockSrc, 'module', '**', '*'),
path.join(mockSrc, 'ipfs', '**', '*'),
path.join(mockSrc, 'bos.config.json'),
path.join(mockSrc, 'aliases.json'),
];
expect(startFileWatcher).toHaveBeenCalledWith(expectedWatchPaths, expect.any(Function));
});

it("should add correct watch paths after adding apps", async () => {
const mockedGazeAdd = jest.spyOn(Gaze.prototype, 'add');

const mockHotOpts: DevOptions = { NoHot: true };
console.log("dev", dev);
await dev(mockSrc, mockHotOpts);

const mockSrc2 = "/app_example_2";
vol.fromJSON(app_example_2, mockSrc2);
const mockDist2 = path.join(mockSrc2, 'build');
await addApps([mockSrc2], [mockDist2]);

const expectedWatchPaths = [
path.join(mockSrc2, 'widget', '**', '*'),
path.join(mockSrc2, 'module', '**', '*'),
path.join(mockSrc2, 'ipfs', '**', '*'),
path.join(mockSrc2, 'bos.config.json'),
path.join(mockSrc2, 'aliases.json'),
];
expect(mockedGazeAdd).toHaveBeenCalledWith(expectedWatchPaths);
});
});

0 comments on commit 950197d

Please sign in to comment.