UNPKG

tempmon

Version:

Monitor for a created folder and automatically generate custom files based on your templates - perfect for development

57 lines (56 loc) 3.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const index_1 = __importDefault(require("./index")); const files = ["index.ts", "${tempmon__fileName}.page.tsx", "${tempmon__fileName}.tsx"]; jest.mock(`fs-extra`, () => ({ copySync: jest.fn().mockReturnValue(true), readdir: jest.fn(() => files), readFileSync: jest.fn(() => "test, ${tempmon__fileName}, ${tempmon__fileName__lowercase}, ${tempmon__fileName__UPPERCASE}, ${tempmon__fileName__Firstcapital}"), writeFileSync: jest.fn(() => true), renameSync: jest.fn(() => true), })); console.log = jest.fn(); describe("index.js", () => { describe("watcher", () => { it("ready event", async () => { index_1.default.emit("ready"); expect(console.log).toHaveBeenCalledWith("[tempmon] ready for changes. template directory -> ./test/template, watch diretory -> ./test/src/**"); }); it("addDir event", async () => { // before expect(fs_extra_1.default.copySync).not.toHaveBeenCalled(); expect(fs_extra_1.default.readdir).not.toHaveBeenCalled(); expect(fs_extra_1.default.readFileSync).not.toHaveBeenCalled(); expect(fs_extra_1.default.writeFileSync).not.toHaveBeenCalled(); expect(fs_extra_1.default.renameSync).not.toHaveBeenCalled(); const directory = "targetDirectory"; index_1.default.emit("addDir", directory); await new Promise((r) => setTimeout(r, 500)); // after expect(fs_extra_1.default.copySync).toHaveBeenCalledTimes(1); expect(fs_extra_1.default.copySync).toHaveBeenCalledWith("./test/template", directory); expect(fs_extra_1.default.readdir).toHaveBeenCalledTimes(1); expect(fs_extra_1.default.readdir).toHaveBeenCalledWith(directory); expect(fs_extra_1.default.readFileSync).toHaveBeenCalledTimes(files.length); files.forEach((file, i) => { expect(fs_extra_1.default.readFileSync).toHaveBeenNthCalledWith(i + 1, `${directory}/${file}`, { encoding: "utf8" }); }); expect(fs_extra_1.default.writeFileSync).toHaveBeenCalledTimes(files.length); files.forEach((file, i) => { expect(fs_extra_1.default.writeFileSync).toHaveBeenNthCalledWith(i + 1, `${directory}/${file}`, "test, targetDirectory, targetdirectory, TARGETDIRECTORY, Targetdirectory"); }); expect(fs_extra_1.default.renameSync).toHaveBeenCalledTimes(2); [ ["targetDirectory/${tempmon__fileName}.page.tsx", "targetDirectory/targetDirectory.page.tsx"], ["targetDirectory/${tempmon__fileName}.tsx", "targetDirectory/targetDirectory.tsx"], ].forEach((arg, i) => { expect(fs_extra_1.default.renameSync).toHaveBeenNthCalledWith(i + 1, ...arg); }); expect(console.log).toHaveBeenCalledWith("[tempmon] custom files have been added in targetDirectory"); }); }); });