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 --- test/app.test.js | 29 +++++++++++++++++++++++------ test/testsite/posts/testfile.md | 4 +++- 2 files changed, 26 insertions(+), 7 deletions(-) (limited to 'test') 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! -- cgit 1.4.1