Skip to content

Commit

Permalink
Add tests for generate.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
daohoangson committed Sep 25, 2023
1 parent 0f558b5 commit 366f37c
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 9 deletions.
156 changes: 156 additions & 0 deletions transformers/js-dvhcvn/bin/generate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { readFileSync } from "fs";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { main, processLevel1, processLevel2, processLevel3 } from "./generate";

let written = "";

const mocks = vi.hoisted(() => {
return {
readFileSync: vi.fn() satisfies typeof readFileSync,
stdout: {
write: (str: string) => (written = `${written}${str}`),
},
};
});

vi.mock("fs", () => ({
readFileSync: mocks.readFileSync,
}));

vi.mock("process", () => ({
stdout: mocks.stdout,
}));

describe("generate", () => {
beforeEach(() => {
written = "";
});

it("should process level 1", () => {
processLevel1(0, {
level1_id: "01",
name: "Thành phố Hà Nội",
type: "Thành phố Trung ương",
level2s: [],
});
expect(written.trim()).toEqual(
"new Level1('01', 'Thành phố Hà Nội', Type.tptw, [\n]),"
);
});

it("should process level 2", () => {
processLevel2(0, 0, {
level2_id: "001",
name: "Quận Ba Đình",
type: "Quận",
level3s: [],
});
expect(written.trim()).toEqual(
"new Level2(0, '001', 'Quận Ba Đình', Type.quan, [\n]),"
);
});

it("should process level 3", () => {
processLevel3(0, 0, {
level3_id: "00001",
name: "Phường Phúc Xá",
type: "Phường",
});
expect(written.trim()).toEqual(
"new Level3(0, 0, '00001', 'Phường Phúc Xá', Type.phuong),"
);
});

it("should process all levels", () => {
mocks.readFileSync.mockReturnValueOnce(
JSON.stringify({
data: [
{
level1_id: "01",
name: "Thành phố Hà Nội",
type: "Thành phố Trung ương",
level2s: [
{
level2_id: "001",
name: "Quận Ba Đình",
type: "Quận",
level3s: [
{
level3_id: "00001",
name: "Phường Phúc Xá",
type: "Phường",
},
],
},
],
},
],
})
);

main(["/path/to/file.json"]);

expect(mocks.readFileSync).toHaveBeenCalledWith(
"/path/to/file.json",
expect.anything()
);

expect(written).toEqual(
"import { Level1, Level2, Level3, Type } from './model';\n" +
"\nexport const level1s = [" +
"new Level1('01', 'Thành phố Hà Nội', Type.tptw, [\n" +
"new Level2(0, '001', 'Quận Ba Đình', Type.quan, [\n" +
"new Level3(0, 0, '00001', 'Phường Phúc Xá', Type.phuong),\n" +
"]),\n" +
"]),\n" +
"];"
);
});

it("should handle name with single quote", () => {
processLevel1(0, {
level1_id: "foo",
name: "Tỉnh Foo's",
type: "Tỉnh",
level2s: [],
});
expect(written.trim()).toEqual(
"new Level1('foo', \"Tỉnh Foo's\", Type.tinh, [\n]),"
);
});

it("should handle name with double quote", () => {
processLevel1(0, {
level1_id: "foo",
name: 'Tỉnh Foo"bar',
type: "Tỉnh",
level2s: [],
});
expect(written.trim()).toEqual(
"new Level1('foo', 'Tỉnh Foo\"bar', Type.tinh, [\n]),"
);
});

it("should handle name with single and double quotes", () => {
processLevel1(0, {
level1_id: "foo",
name: "Tỉnh 'Foo\"bar",
type: "Tỉnh",
level2s: [],
});
expect(written.trim()).toEqual(
"new Level1('foo', 'Tỉnh \\'Foo\"bar', Type.tinh, [\n]),"
);
});

it("should handle invalid type", () => {
const fn = () =>
processLevel1(0, {
level1_id: "foo",
name: "Bar Foo",
type: "Bar",
level2s: [],
});
expect(fn).toThrowError("Type not found: Bar");
});
});
19 changes: 10 additions & 9 deletions transformers/js-dvhcvn/bin/generate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { readFileSync } from "fs";
import { stdout as STDOUT } from "process";

const stdout = {
write: (msg: string) => process.stdout.write(msg),
writeln: (msg: string = "") => process.stdout.write(`${msg}\n`),
write: (msg: string) => STDOUT.write(msg),
writeln: (msg: string = "") => STDOUT.write(msg + "\n"),
};

const types: { [key: string]: string } = {
Expand All @@ -17,7 +18,7 @@ const types: { [key: string]: string } = {
"Thị xã": "thi_xa",
};

function main(args: string[]): void {
export function main(args: string[]): void {
stdout.writeln("import { Level1, Level2, Level3, Type } from './model';");
stdout.writeln();

Expand All @@ -27,7 +28,7 @@ function main(args: string[]): void {
const json = JSON.parse(txt);
const data = json.data as any[];
for (let i = 0; i < data.length; i++) {
_processLevel1(i, data[i]);
processLevel1(i, data[i]);
}

stdout.write("];");
Expand All @@ -44,21 +45,21 @@ function _getType(str: string): string {
throw new Error(`Type not found: ${str}`);
}

function _processLevel1(level1Index: number, level1: any): void {
export function processLevel1(level1Index: number, level1: any): void {
const id = _getString(level1.level1_id);
const name = _getString(level1.name);
const type = _getType(level1.type);
stdout.write(`new Level1(${id}, ${name}, ${type}, [\n`);

const level2s = level1.level2s as any[];
for (let i = 0; i < level2s.length; i++) {
_processLevel2(level1Index, i, level2s[i]);
processLevel2(level1Index, i, level2s[i]);
}

stdout.writeln("]),");
}

function _processLevel2(
export function processLevel2(
level1Index: number,
level2Index: number,
level2: any
Expand All @@ -70,13 +71,13 @@ function _processLevel2(

const level3s = level2.level3s as any[];
for (const level3 of level3s) {
_processLevel3(level1Index, level2Index, level3);
processLevel3(level1Index, level2Index, level3);
}

stdout.writeln("]),");
}

function _processLevel3(
export function processLevel3(
level1Index: number,
level2Index: number,
level3: any
Expand Down

0 comments on commit 366f37c

Please sign in to comment.