-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fcdc0b
commit 8ab2da6
Showing
13 changed files
with
12,535 additions
and
486 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}), | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] | ||
}" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.