Skip to content

Commit

Permalink
✅ cronjobs and tasks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Dec 19, 2024
1 parent 3929fd4 commit 3712801
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/tasks/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const filename = "dist/index.html";
const rgx = new RegExp('="/([^"]*)"', "g");
const rootURI = process.env.ROOT_URI || "/";

(async () => {
const transform = async () => {
const rootURI = process.env.ROOT_URI || "/";
const input = (await readFile(filename)).toString();
await writeFile(
filename,
input.replace(rgx, (_: string, c1: string) => `="${rootURI}${c1}"`),
);
})();
};

await transform();

export default transform;
25 changes: 25 additions & 0 deletions test/back-end/cronjobs/purge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, it, vi } from "vitest";

const { mockDelete, mockSchedule, mockWhere } = vi.hoisted(() => ({
mockDelete: vi.fn(),
mockSchedule: vi.fn(),
mockWhere: vi.fn(() => ({
delete: mockDelete,
select: () => [{ id: 1234 }],
})),
}));

vi.mock("knex", () => ({ default: () => () => ({ where: mockWhere }) }));
vi.mock("node-cron", () => ({ schedule: mockSchedule }));

describe("purge cronjob", async () => {
await import("../../../src/back-end/cronjobs/purge");

it("deletes expired stories and associated votes", async ({ expect }) => {
await mockSchedule.mock.lastCall![1](new Date());

expect(mockWhere).toHaveBeenCalledWith("storyId", 1234);
expect(mockWhere).toHaveBeenCalledWith("id", 1234);
expect(mockDelete).toHaveBeenCalledTimes(2);
});
});
45 changes: 45 additions & 0 deletions test/tasks/transform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { readFile, writeFile } from "fs";
import { describe, it, vi } from "vitest";
import transform from "../../../src/tasks/transform";

const { mockPromisify, mockWriteFile } = vi.hoisted(() => ({
mockPromisify: (arg: unknown) => {
switch (arg) {
case readFile:
return () => '<link href="/test" />';
case writeFile:
return mockWriteFile;
}
},
mockWriteFile: vi.fn(),
}));

vi.mock("util", () => ({
default: {
promisify: mockPromisify,
},
}));

describe("transform task", () => {
it("keeps links intact when rootURI is /", async ({ expect }) => {
vi.stubEnv("ROOT_URI", "/");

await transform();

expect(mockWriteFile).toHaveBeenCalledWith(
expect.anything(),
'<link href="/test" />',
);
});

it("reformats links when rootURI is custom", async ({ expect }) => {
vi.stubEnv("ROOT_URI", "/prefix/");

await transform();

expect(mockWriteFile).toHaveBeenCalledWith(
expect.anything(),
'<link href="/prefix/test" />',
);
});
});

0 comments on commit 3712801

Please sign in to comment.