From 31f441ff32ab9f52fc80d0835bdd074554587ef6 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 30 Jun 2017 18:01:51 +0200 Subject: test: Use cheerio for DOM testing --- package.json | 1 + test/app.test.js | 29 ++++++++++++++++----- test/testsite/posts/testfile.md | 4 ++- yarn.lock | 57 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 81 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index e64409a..9fe5283 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "devDependencies": { "auto-install": "^1.7.4", "ava": "^0.19.1", + "cheerio": "^1.0.0-rc.1", "eslint": "^4.1.1", "eslint-config-prettier": "^2.2.0", "eslint-config-standard": "^10.2.1", diff --git a/test/app.test.js b/test/app.test.js index 9b0403c..8507c41 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -1,28 +1,45 @@ const test = require("ava"); const path = require("path"); const request = require("supertest"); +const cheerio = require("cheerio"); process.chdir(path.resolve(__dirname, "./testsite/")); const config = require(path.resolve(__dirname, "../src/modules/config.js")); const app = require("../src/app.js"); +const parseResponse = res => + cheerio.load(res.text, { + normalizeWhitespace: true, + useHtmlParser2: true + }); + test("homepage", t => { return request(app.listen()) .get("/") .expect(200) - .expect(/Test Site<\/title>/) - .expect(/<h1>Test Site<\/h1>/) - .expect(/This is a test/) - .then(() => t.pass()); + .then(parseResponse) + .then($ => { + t.is($("head > title").text(), config.site.title); + t.is($("h1").text(), config.site.title); + t.is($("main").length, 1); + }); }); test("post", t => { return request(app.listen()) .get("/post/testfile") .expect(200) - .expect(/<h1>Lorem ipsum<\/h1>/) - .then(() => t.pass()); + .then(parseResponse) + .then($ => { + t.is($("article h1").text(), "This is a test"); + t.is( + $("article p").text(), + `Ut enim blandit volutpat maecenas? Volutpat blandit aliquam etiam erat \ +velit, scelerisque in dictum non, consectetur a erat nam at lectus \ +urna duis convallis convallis tellus, id interdum velit laoreet!` + ); + }); }); test("post not found", t => { diff --git a/test/testsite/posts/testfile.md b/test/testsite/posts/testfile.md index bafc456..65f2122 100644 --- a/test/testsite/posts/testfile.md +++ b/test/testsite/posts/testfile.md @@ -3,4 +3,6 @@ Title = "This is a test" Description = "Test file" Tags = ["a", "b"] +++ -# Lorem ipsum +Ut enim blandit volutpat maecenas? Volutpat blandit aliquam etiam erat +velit, scelerisque in dictum non, consectetur a erat nam at lectus +urna duis convallis convallis tellus, id interdum velit laoreet! diff --git a/yarn.lock b/yarn.lock index c9165f1..9060a49 100644 --- a/yarn.lock +++ b/yarn.lock @@ -37,6 +37,10 @@ ansi-styles "^2.2.1" esutils "^2.0.2" +"@types/node@^6.0.46": + version "6.0.79" + resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.79.tgz#5efe7d4a6d8c453c7e9eaf55d931f4a22fac5169" + abbrev@1: version "1.1.0" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f" @@ -671,6 +675,10 @@ bluebird@^3.0.0: version "3.5.0" resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c" +boolbase@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e" + boom@2.x.x: version "2.10.1" resolved "https://registry.yarnpkg.com/boom/-/boom-2.10.1.tgz#39c8918ceff5799f83f9492a848f625add0c766f" @@ -800,6 +808,17 @@ chalk@^1.0.0, chalk@^1.1.0, chalk@^1.1.1, chalk@^1.1.3: strip-ansi "^3.0.0" supports-color "^2.0.0" +cheerio@^1.0.0-rc.1: + version "1.0.0-rc.1" + resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-1.0.0-rc.1.tgz#2af37339eab713ef6b72cde98cefa672b87641fe" + dependencies: + css-select "~1.2.0" + dom-serializer "~0.1.0" + entities "~1.1.1" + htmlparser2 "^3.9.1" + lodash "^4.15.0" + parse5 "^3.0.1" + chokidar@1.6.1, chokidar@^1.4.2: version "1.6.1" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.6.1.tgz#2f4447ab5e96e50fb3d789fd90d4c72e0e4c70c2" @@ -1106,6 +1125,19 @@ crypto-random-string@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e" +css-select@~1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/css-select/-/css-select-1.2.0.tgz#2b3a110539c5355f1cd8d314623e870b121ec858" + dependencies: + boolbase "~1.0.0" + css-what "2.1" + domutils "1.5.1" + nth-check "~1.0.1" + +css-what@2.1: + version "2.1.0" + resolved "https://registry.yarnpkg.com/css-what/-/css-what-2.1.0.tgz#9467d032c38cfaefb9f2d79501253062f87fa1bd" + cssauron@^1.2.0: version "1.4.0" resolved "https://registry.yarnpkg.com/cssauron/-/cssauron-1.4.0.tgz#a6602dff7e04a8306dc0db9a551e92e8b5662ad8" @@ -1300,7 +1332,7 @@ doctrine@^2.0.0: esutils "^2.0.2" isarray "^1.0.0" -dom-serializer@0: +dom-serializer@0, dom-serializer@~0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-0.1.0.tgz#073c697546ce0780ce23be4a28e293e40bc30c82" dependencies: @@ -1321,6 +1353,13 @@ domhandler@^2.3.0: dependencies: domelementtype "1" +domutils@1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.5.1.tgz#dcd8488a26f563d61079e48c9f7b7e32373682cf" + dependencies: + dom-serializer "0" + domelementtype "1" + domutils@^1.5.1: version "1.6.2" resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.6.2.tgz#1958cc0b4c9426e9ed367fb1c8e854891b0fa3ff" @@ -2292,7 +2331,7 @@ hsts@2.0.0: dependencies: core-util-is "1.0.2" -htmlparser2@^3.8.3: +htmlparser2@^3.8.3, htmlparser2@^3.9.1: version "3.9.2" resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.9.2.tgz#1bdf87acca0f3f9e53fa4fcceb0f4b4cbb00b338" dependencies: @@ -3082,7 +3121,7 @@ lodash.reduce@4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/lodash.reduce/-/lodash.reduce-4.6.0.tgz#f1ab6b839299ad48f784abbf476596f03b914d3b" -lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0: +lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0: version "4.17.4" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" @@ -3423,6 +3462,12 @@ npmlog@^4.0.2: gauge "~2.7.3" set-blocking "~2.0.0" +nth-check@~1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/nth-check/-/nth-check-1.0.1.tgz#9929acdf628fc2c41098deab82ac580cf149aae4" + dependencies: + boolbase "~1.0.0" + number-is-nan@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d" @@ -3617,6 +3662,12 @@ parse-ms@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/parse-ms/-/parse-ms-1.0.1.tgz#56346d4749d78f23430ca0c713850aef91aa361d" +parse5@^3.0.1: + version "3.0.2" + resolved "https://registry.yarnpkg.com/parse5/-/parse5-3.0.2.tgz#05eff57f0ef4577fb144a79f8b9a967a6cc44510" + dependencies: + "@types/node" "^6.0.46" + parseurl@^1.3.0, parseurl@~1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.1.tgz#c8ab8c9223ba34888aa64a297b28853bec18da56" -- cgit 1.4.1