From b7bf162e6c3bc834097e65936704e3eac59eb4bd Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 2 Jul 2017 15:30:20 +0200 Subject: feat: Use microformats classes Add snapshot-based tests to ensure microformats data can be extracted correctly. --- test/app.test.js | 227 ++++++++++++++++++---------------------- test/domain/posts.test.js | 18 ++-- test/snapshots/app.test.js.md | 163 +++++++++++++++++++++++++++++ test/snapshots/app.test.js.snap | Bin 0 -> 1236 bytes 4 files changed, 274 insertions(+), 134 deletions(-) create mode 100644 test/snapshots/app.test.js.md create mode 100644 test/snapshots/app.test.js.snap (limited to 'test') diff --git a/test/app.test.js b/test/app.test.js index 6810089..69cea86 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -22,137 +22,110 @@ const toMicroformatsOptions = node => ({ textFormat: "normalised" }); -test("homepage", t => { - return request(app.listen()) - .get("/") - .expect(200) - .expect("Content-Type", "text/html; charset=utf-8") - .expect(/^/) - .then(parseResponse) - .then($ => { - t.is($("head > title").text(), "John Doe", "head title is site author"); - t.is($("h1").text(), "John Doe", "h1 is site author"); - t.is($("main").length, 1, "only one
tag"); - t.is($("main .posts").length, 1, "contains one posts listing"); - t.is( - $(".post:first-of-type a").attr("href"), - "/post/testfile", - "first post url" - ); - t.is( - $(".post:first-of-type time").text(), - "Sunday, January 1, 2017", - "first post date" - ); - t.is( - $(".post:first-of-type time").attr("datetime"), - new Date("2017-01-01").toISOString() - ); - return $; - }) - .then(toMicroformatsOptions) - .then(options => - Promise.all([ - mf.countAsync(options).then(count => - t.deepEqual(count, { - "h-card": 1 - }) - ), - mf.getAsync(options).then(data => { - t.deepEqual(data.items, [ - { - properties: { - name: ["John Doe"], - url: ["/"], - photo: ["/static/johndoe.jpg"] - }, - type: ["h-card"] - } - ]); - }) - ]) - ); -}); +async function notFound(t, url, bodyRegex) { + const res = await request(app.listen()).get(url); + const body = res.text; + t.is(res.statusCode, 404); + t.regex(body, bodyRegex); +} +notFound.title = (providedTitle, input, expected) => + `${input} = 404 ${expected.toString()}`; + +test("homepage", async function(t) { + const res = await request(app.listen()).get("/"); + + t.is(res.statusCode, 200); + t.is(res.type, "text/html"); + t.is(res.charset, "utf-8"); + t.regex(res.text, /^/); + + const $ = parseResponse(res); -test("post", t => { - return request(app.listen()) - .get("/post/testfile") - .expect(200) - .expect("Content-Type", "text/html; charset=utf-8") - .expect(/^/) - .then(parseResponse) - .then($ => { - t.is( - $("head > title").text(), - "This is a test · " + "John Doe", - "head title contains post and site author" - ); - t.is( - $("article h1").text(), - "This is a test", - "article header is post title" - ); - t.is( - $("article time").text(), - "Sunday, January 1, 2017", - "first post date" - ); - t.is( - $("article time").attr("datetime"), - new Date("2017-01-01").toISOString() - ); - 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!`, - "article has text" - ); - }); + t.is($("head > title").text(), "John Doe", "head title is site author"); + t.is($("h1").text(), "John Doe", "h1 is site author"); + t.is($("main").length, 1, "only one
tag"); + t.is( + $(".h-entry:first-of-type time").text(), + "Sunday, January 1, 2017", + "first post date" + ); + t.is( + $(".h-entry:first-of-type time").attr("datetime"), + new Date("2017-01-01").toISOString() + ); + + const options = toMicroformatsOptions($); + const count = await mf.countAsync(options); + + t.deepEqual(count, { + "h-card": 1, + "h-feed": 1, + "h-entry": 1 + }); + + const data = await mf.getAsync(options); + + t.snapshot(data, "should contain relevant microformats data"); }); -test("post not found", t => { - return request(app.listen()) - .get("/post/non-existant") - .expect(404) - .expect(/Post not found/) - .then(() => t.pass()); +test("post", async function(t) { + const res = await request(app.listen()).get("/post/testfile"); + + t.is(res.statusCode, 200); + t.is(res.type, "text/html"); + t.is(res.charset, "utf-8"); + t.regex(res.text, /^/); + + const $ = parseResponse(res); + + t.is( + $("head > title").text(), + "This is a test · " + "John Doe", + "head title contains post and site author" + ); + + const options = toMicroformatsOptions($); + + const count = await mf.countAsync(options); + + t.deepEqual(count, { + "h-card": 1, + "h-entry": 1 + }); + + const data = await mf.getAsync(options); + + t.snapshot(data, "should contain relevant microformats data"); }); -test("tags", t => { - return request(app.listen()) - .get("/tag/a") - .expect(200) - .expect("Content-Type", "text/html; charset=utf-8") - .expect(/^/) - .then(parseResponse) - .then($ => { - t.is( - $("head > title").text(), - "A · John Doe", - "head title contains title-cased tag and site name" - ); - t.is( - $(".post a").text(), - "This is a test", - "post link text is post title" - ); - t.is( - $(".post:first-of-type a").attr("href"), - "/post/testfile", - "post url" - ); - t.is( - $(".post:first-of-type time").text(), - "Sunday, January 1, 2017", - "first post date" - ); - }); +test("tags", async function(t) { + const res = await request(app.listen()).get("/tag/a"); + + t.is(res.statusCode, 200); + t.is(res.type, "text/html"); + t.is(res.charset, "utf-8"); + t.regex(res.text, /^/); + + const $ = parseResponse(res); + + t.is( + $("head > title").text(), + "A · John Doe", + "head title contains title-cased tag and site name" + ); + const options = toMicroformatsOptions($); + const count = await mf.countAsync(options); + + t.deepEqual(count, { + "h-card": 1, + "h-feed": 1, + "h-entry": 1 + }); + + const data = await mf.getAsync(options); + + t.snapshot(data, "should contain relevant microformats data"); }); -test("tags not found", t => - request(app.listen()) - .get("/tag/non-existant") - .expect(404) - .expect(/tag non-existant not found/) - .then(() => t.pass())); +test(notFound, "/post/non-existent", /Post not found/); +test(notFound, "/tag/non-existent", /tag non-existent not found/); diff --git a/test/domain/posts.test.js b/test/domain/posts.test.js index cc236c7..8b27698 100644 --- a/test/domain/posts.test.js +++ b/test/domain/posts.test.js @@ -1,13 +1,16 @@ const test = require("ava"); const path = require("path"); -const Posts = require("../../src/domain/posts.js")({ - folder: path.resolve(__dirname, "../testsite/posts/"), - taxonomies: { - tag: "tags", - category: "categories" - } -}); +const Posts = require("../../src/domain/posts.js")( + { + folder: path.resolve(__dirname, "../testsite/posts/"), + taxonomies: { + tag: "tags", + category: "categories" + } + }, + basename => basename +); test("get", t => { const expected = new Map( @@ -19,6 +22,7 @@ test("get", t => { }) ); const post = Posts.get( + basename => basename, path.resolve(__dirname, "../testsite/posts/testfile.md") ); t.deepEqual(post.data, expected); diff --git a/test/snapshots/app.test.js.md b/test/snapshots/app.test.js.md new file mode 100644 index 0000000..99811f9 --- /dev/null +++ b/test/snapshots/app.test.js.md @@ -0,0 +1,163 @@ +# Snapshot report for `test/app.test.js` + +The actual snapshot is saved in `app.test.js.snap`. + +Generated by [AVA](https://ava.li). + +## homepage + +> should contain relevant microformats data + + { + items: [ + { + properties: { + name: [ + 'John Doe', + ], + photo: [ + '/static/johndoe.jpg', + ], + url: [ + '/', + ], + }, + type: [ + 'h-card', + ], + }, + { + children: [ + { + properties: { + name: [ + 'This is a test', + ], + published: [ + '2017-01-01T00:00:00.000Z', + ], + url: [ + '/post/testfile', + ], + }, + type: [ + 'h-entry', + ], + value: 'This is a test Sunday, January 1, 2017', + }, + ], + properties: { + name: [ + 'John Doe', + ], + }, + type: [ + 'h-feed', + ], + }, + ], + 'rel-urls': {}, + rels: {}, + } + +## post + +> should contain relevant microformats data + + { + items: [ + { + properties: { + name: [ + 'John Doe', + ], + photo: [ + '/static/johndoe.jpg', + ], + url: [ + '/', + ], + }, + type: [ + 'h-card', + ], + }, + { + properties: { + content: [ + { + html: '

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!

', + value: '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!', + }, + ], + name: [ + 'This is a test', + ], + published: [ + '2017-01-01T00:00:00.000Z', + ], + }, + type: [ + 'h-entry', + ], + }, + ], + 'rel-urls': {}, + rels: {}, + } + +## tags + +> should contain relevant microformats data + + { + items: [ + { + properties: { + name: [ + 'John Doe', + ], + photo: [ + '/static/johndoe.jpg', + ], + url: [ + '/', + ], + }, + type: [ + 'h-card', + ], + }, + { + children: [ + { + properties: { + name: [ + 'This is a test', + ], + published: [ + '2017-01-01T00:00:00.000Z', + ], + url: [ + '/post/testfile', + ], + }, + type: [ + 'h-entry', + ], + value: 'This is a test Sunday, January 1, 2017', + }, + ], + properties: { + name: [ + 'A · John Doe', + ], + }, + type: [ + 'h-feed', + ], + }, + ], + 'rel-urls': {}, + rels: {}, + } diff --git a/test/snapshots/app.test.js.snap b/test/snapshots/app.test.js.snap new file mode 100644 index 0000000..bbc5c61 Binary files /dev/null and b/test/snapshots/app.test.js.snap differ -- cgit 1.4.1