diff --git a/test/unit/mocha/helpers.js b/test/unit/mocha/helpers.js new file mode 100644 index 00000000..37059cd8 --- /dev/null +++ b/test/unit/mocha/helpers.js @@ -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; diff --git a/test/unit/mocha/part6/form-should-have-checkbox.spec.js b/test/unit/mocha/part6/form-should-have-checkbox.spec.js new file mode 100644 index 00000000..bb5f7d87 --- /dev/null +++ b/test/unit/mocha/part6/form-should-have-checkbox.spec.js @@ -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 `` 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" + ); + }); +}); diff --git a/test/unit/mocha/part6/refactor-book-data.spec.js b/test/unit/mocha/part6/refactor-book-data.spec.js index df0e8bd5..909a8bd4 100644 --- a/test/unit/mocha/part6/refactor-book-data.spec.js +++ b/test/unit/mocha/part6/refactor-book-data.spec.js @@ -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( @@ -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( @@ -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(