"use strict"; const test = require("ava"); const path = require("path"); const request = require("supertest"); const cheerio = require("cheerio"); const mf = require("microformat-node"); 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 }); const toMicroformatsOptions = node => ({ node, textFormat: "normalised" }); test("homepage", t => { return request(app.listen()) .get("/") .expect(200) .expect(/^/) .then(parseResponse) .then($ => { t.is($("head > title").text(), "John Doe"); t.is($("h1").text(), "John Doe"); t.is($("main").length, 1); t.is($("main .posts").length, 1); 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"] } ]); }) ]) ); }); test("post", t => { return request(app.listen()) .get("/post/testfile") .expect(200) .expect(/^/) .then(parseResponse) .then($ => { t.is($("head > title").text(), "This is a test · " + "John Doe"); 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 => { return request(app.listen()) .get("/post/non-existant") .expect(404) .expect(/Post not found/) .then(() => t.pass()); }); test("tags", t => { return request(app.listen()) .get("/tag/a") .expect(200) .expect(/^/) .then(parseResponse) .then($ => { t.is($("head > title").text(), "A · John Doe"); t.is($(".post a").text(), "This is a test"); }); }); test("tags not found", t => request(app.listen()) .get("/tag/non-existant") .expect(404) .expect(/tag non-existant not found/) .then(() => t.pass()));