Skip to content

Commit

Permalink
add tests for classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanthanh2067 committed Nov 12, 2021
1 parent 3fcdc0b commit 8ab2da6
Show file tree
Hide file tree
Showing 13 changed files with 12,535 additions and 486 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
browser: true,
commonjs: true,
es2021: true,
jest: true,
},
extends: "eslint:recommended",
parserOptions: {
Expand Down
24 changes: 24 additions & 0 deletions bin/helpers/__mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = jest.createMockFromModule("fs");

// allow adding a mock file with data to our fake file system

const mockFiles = {};
function __setMockFileData(filename, data) {
mockFiles[filename] = data;
}

function readFile(filepath) {
const data = mockFiles[filepath];
if (data) {
return Promise.resolve(data);
} else {
return Promise.reject(new Error("unknown filepath"));
}
}

fs.promises = {
__setMockFileData,
readFile,
};

module.exports = fs;
52 changes: 52 additions & 0 deletions bin/helpers/handleFile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const HandleFile = require("./handleFile");

describe("handleFile class tests", () => {
describe("handleFile method should work", () => {
test("should return empty for both attributes", async () => {
const emptyArrayPromise = new Promise(({ resolve }) => {
resolve([]);
});
const test = new HandleFile();
expect(test.handleFile("", emptyArrayPromise)).toStrictEqual(
new Promise(({ resolve }) => {
resolve({
results: [],
metaData: "",
});
})
);
});

test("should return correct values for both attributes txt extension", async () => {
const arrayPromise = new Promise(({ resolve }) => {
resolve(["Hello World"]);
});
const test = new HandleFile();
expect(test.handleFile(".txt", arrayPromise)).toStrictEqual(
new Promise(({ resolve }) => {
resolve({
results: ["Hello World"],
metaData: "",
});
})
);
});

test("should return correct values for both attributes txt extension", async () => {
const arrayPromise = new Promise(({ resolve }) => {
resolve(["Hello World"]);
});
const test = new HandleFile();
expect(test.handleFile(".txt", arrayPromise)).toStrictEqual(
new Promise(({ resolve }) => {
resolve({
results: ["Hello World"],
metaData: "",
});
})
);
});

// test for md extension
});
});
3 changes: 2 additions & 1 deletion bin/helpers/produceFile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global process */
const fs = require("fs");
const chalk = require("chalk");
const pretty = require("pretty");

module.exports = class ProduceFile {
constructor(results, metaData = null, path, ext, styleSheetLink = "") {
Expand Down Expand Up @@ -70,7 +71,7 @@ module.exports = class ProduceFile {
</html>
`;

return result;
return pretty(result, { ocd: true });
}

produce(folder) {
Expand Down
150 changes: 150 additions & 0 deletions bin/helpers/produceFile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
const ProduceFile = require("./produceFile");
const pretty = require("pretty");

describe("produceFile class tests", () => {
let results = null;
let metaData = null;
let path = "";
let ext = "";
let styleSheetLink = "";

test("should createFile return null if results not available", () => {
const test = new ProduceFile(results, metaData, path, ext, styleSheetLink);

expect(test.createFile()).toBe(undefined);
});

describe("createHtmlFile should work", () => {
test("should give correct html file without data, meta and stylesheet", () => {
let results = [];
const test = new ProduceFile(
results,
metaData,
path,
ext,
styleSheetLink
);

expect(test.createHtmlFile(results)).toBe(
pretty(
`
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Filename</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
</body>
</html>
`,
{ ocd: true }
)
);
});

test("should give html file without meta and stylesheet", () => {
let results = ["Hello World"];

const test = new ProduceFile(
results,
metaData,
path,
ext,
styleSheetLink
);

expect(test.createHtmlFile(results)).toBe(
pretty(
`
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Filename</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>Hello World</p>
</body>
</html>
`,
{ ocd: true }
)
);
});

test("should give correct html file with meta and no stylesheet", () => {
let results = ["Hello World"];

let metaData = {
author: "Author",
};
const test = new ProduceFile(
results,
metaData,
path,
ext,
styleSheetLink
);

expect(test.createHtmlFile(results)).toBe(
pretty(
`
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Filename</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Author">
</head>
<body>
<p>Hello World</p>
</body>
</html>
`,
{ ocd: true }
)
);
});

test("should give correct html file with meta and stylesheet", () => {
let results = ["Hello World"];

let metaData = {
author: "Author",
};
styleSheetLink = "test.com";
const test = new ProduceFile(
results,
metaData,
path,
ext,
styleSheetLink
);

expect(test.createHtmlFile(results)).toBe(
pretty(
`
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Filename</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Author">
<link rel='stylesheet' href='test.com'>
</head>
<body>
<p>Hello World</p>
</body>
</html>
`,
{ ocd: true }
)
);
});
});
});
9 changes: 9 additions & 0 deletions bin/helpers/produceFolder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* global process */
const ProduceFolder = require("./produceFolder");

describe("produceFolder class tests", () => {
test("get path should return correct path", () => {
const test = new ProduceFolder();
expect(test.getPath()).toBe(`${process.cwd()}/dist`);
});
});
63 changes: 63 additions & 0 deletions bin/helpers/readPath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const ReadPath = require("./readPath");

jest.mock("fs");
const fs = require("fs").promises;

describe("readPath class tests", () => {
test("should throw error if constructor receive invalid link", () => {
[null, undefined, ""].forEach((option) => {
expect(() => {
new ReadPath(option);
}).toThrow("The path should be a string");
});
});

describe("readFile tests", () => {
const txtFileData = "Hello World";

test("Reading txt file should work", async () => {
const path = "/sample-test.txt";
fs.__setMockFileData(path, txtFileData);
const test = new ReadPath(path);
expect(await test.readFile()).toEqual(txtFileData);
});
});

describe("getter tests", () => {
const path = "/sample-test.txt";
fs.__setMockFileData(path, {
path: "/sample-test.txt",
ext: ".txt",
results: new Promise(({ resolve }) => {
resolve("Hello World");
}),
});
const readPath = new ReadPath(path);

beforeAll(async () => {
await readPath.init();
});

test("Should return correct extension", () => {
expect(readPath.getExt()).toBe(".txt");
});

test("Should return correct path", () => {
expect(readPath.getPath()).toBe("/sample-test.txt");
});

test("Should return correct results", async () => {
expect(await readPath.getResults()).toBe("Hello World");
});

test("Should return correct details", async () => {
expect(readPath.getDetails()).toStrictEqual({
path: "/sample-test.txt",
ext: ".txt",
results: new Promise(({ resolve }) => {
resolve("Hello World");
}),
});
});
});
});
31 changes: 31 additions & 0 deletions bin/test/__snapshots__/e2e.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`end-to-end integration prints error and help message when no arguments given 1`] = `
"internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/Users/tuanthanh2067/Desktop/Study/osd600/cv-ssg.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}"
`;

exports[`end-to-end integration prints help message when --help given 1`] = `
"internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/Users/tuanthanh2067/Desktop/Study/osd600/cv-ssg.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}"
`;
11 changes: 11 additions & 0 deletions bin/test/e2e.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const run = require("./run");

describe("end-to-end integration", () => {
test("prints error and help message when no arguments given", async () => {
const { stderr, stdout, exitCode } = await run();

expect(exitCode).toBe(1);
expect(stderr).toMatchSnapshot();
expect(stdout).toEqual("");
});
});
12 changes: 12 additions & 0 deletions bin/test/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const execa = require("execa");

async function run(...args) {
try {
const results = await execa.node("../cv-ssg.js", args);
return results;
} catch (err) {
return err;
}
}

module.exports = run;
Loading

0 comments on commit 8ab2da6

Please sign in to comment.