Skip to content

Commit

Permalink
Add helpers.js and task 2
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinapowers committed Aug 1, 2018
1 parent 1ce6589 commit 303e51c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
31 changes: 31 additions & 0 deletions test/unit/mocha/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require("fs");
const path = require("path");
const parse5 = require("parse5");

const readFile = function(filePath) {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), filePath), "utf8");
} catch (e) {
assert(false, "The BookForm.vue file does not exist");
}

return file;
};

const parseFile = function(file) {
const doc = parse5.parseFragment(file.replace(/\n/g, ""), {
locationInfo: true
});
const nodes = doc.childNodes;

return nodes;
};

const getHtmlTag = function(tagName, nodes) {
return nodes.filter(node => node.nodeName === tagName);
};

module.exports.readFile = readFile;
module.exports.parseFile = parseFile;
module.exports.getHtmlTag = getHtmlTag;
33 changes: 33 additions & 0 deletions test/unit/mocha/part6/form-should-have-checkbox.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const assert = require("chai").assert;
const parse5 = require("parse5");
const cheerio = require("cheerio");
const helpers = require("../helpers");

describe("BookForm.vue", () => {
it("should contain a checkbox with a `v-model` directive @book-form-will-contain-checkbox", () => {
const file = helpers.readFile("src/components/BookForm.vue");
const nodes = helpers.parseFile(file);
const tagName = helpers.getHtmlTag("template", nodes);
const content = parse5.serialize(tagName[0].content);
const $ = cheerio.load(content);
const checkbox = $("form input[type=checkbox]");

assert(
checkbox.length > 0,
"The form doesn't have an `<input>` element with a `type` of `checkbox`"
);

assert.hasAnyKeys(
checkbox.attr(),
["v-model"],
"The BookForm checkbox does not have a `v-model` directive containing `bookData.finishedReading` as its value"
);

assert.propertyVal(
checkbox.attr(),
"v-model",
"bookData.finishedReading",
"The BookForm checkbox does not have a `v-model` directive containing `bookData.finishedReading` as its value"
);
});
});
27 changes: 7 additions & 20 deletions test/unit/mocha/part6/refactor-book-data.spec.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
const fs = require("fs");
const path = require("path");
const assert = require("chai").assert;
const parse5 = require("parse5");
const esquery = require("esquery");
const esprima = require("esprima");
const helpers = require("../helpers");

describe("BookForm.vue", () => {
it("should contain a data function that returns a bookData object @book-form-contains-data-object", () => {
let file;
try {
file = fs.readFileSync(
path.join(process.cwd(), "src/components/BookForm.vue"),
"utf8"
);
} catch (e) {
assert(false, "The BookForm component does not exist");
}
const document = parse5.parseFragment(file.replace(/\n/g, ""), {
locationInfo: true
});
const nodes = document.childNodes;
const script = nodes.filter(node => node.nodeName === "script");
const file = helpers.readFile("src/components/BookForm.vue");
const nodes = helpers.parseFile(file);
const script = helpers.getHtmlTag("script", nodes);

if (script.length == 0) {
assert(
Expand All @@ -42,7 +29,7 @@ describe("BookForm.vue", () => {
const bookData = esquery(ast, "Property[key.name=bookData]");
assert(
bookData.length > 0,
"The BookList's `bookData` object is not present in the return value from `data()`"
"The BookList's `bookData` object is not present"
);

let bookTitle = esquery(
Expand All @@ -66,12 +53,12 @@ describe("BookForm.vue", () => {

let finishedReading = esquery(
data[0],
"Property[key.name=finishedReading] > .value[value=true]"
"Property[key.name=finishedReading] > .value[value=false]"
);

assert(
finishedReading.length > 0,
"The `bookData` `finishedReading` property is not defined with value of `true`"
"The `bookData` `finishedReading` property is not defined with value of `false`"
);

let borrowed = esquery(
Expand Down

0 comments on commit 303e51c

Please sign in to comment.