summary refs log tree commit diff stats
path: root/test/app.test.js
blob: 33c89058c019924c21f04939571c21933d77b36d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"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");
let app;
test.before(async t => (app = await App()));

const parseResponse = res =>
  cheerio.load(res.text, {
    normalizeWhitespace: true,
    useHtmlParser2: true
  });

const toMicroformatsOptions = node => ({
  node,
  textFormat: "normalised"
});

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, /^<!DOCTYPE html>/);

  const $ = parseResponse(res);

  t.is($("head > title").text(), "John Doe", "head title is site author");
  t.is($("main").length, 1, "only one <main> tag");
  t.is($("nav a").first().text(), "Home", "nav link has text");
  t.is($("nav a").first().attr("href"), "/", "nav links to homepage");
  t.is(
    $("a[rel=me]").length,
    2,
    "renders author.contact config to rel=me links"
  );

  const options = toMicroformatsOptions($);
  const count = await mf.countAsync(options);

  t.deepEqual(count, {
    "h-card": 1,
    "h-feed": 1,
    "h-entry": 1,
    rels: 3
  });

  const data = await mf.getAsync(options);

  t.snapshot(data, "should contain relevant microformats data");
});

test("posts", async function(t) {
  const res = await request(app.listen()).get("/post/");

  t.is(res.statusCode, 200);
  t.is(res.type, "text/html");
  t.is(res.charset, "utf-8");
  t.regex(res.text, /^<!DOCTYPE html>/);

  const $ = parseResponse(res);

  t.is(
    $("head > title").text(),
    "Posts · " + "John Doe",
    "head title contains 'Posts' and site author"
  );

  const options = toMicroformatsOptions($);
  const count = await mf.countAsync(options);

  t.deepEqual(count, {
    "h-card": 1,
    "h-feed": 1,
    "h-entry": 1,
    rels: 3
  });

  const data = await mf.getAsync(options);

  t.snapshot(data, "should contain relevant microformats data");
});

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, /^<!DOCTYPE html>/);

  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,
    rels: 3
  });

  const data = await mf.getAsync(options);

  t.snapshot(data, "should contain relevant microformats data");
});

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, /^<!DOCTYPE html>/);

  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,
    rels: 3
  });

  const data = await mf.getAsync(options);

  t.snapshot(data, "should contain relevant microformats data");
});

test("highlight css", async function(t) {
  const res = await request(app.listen()).get("/css/code.css");

  t.is(res.statusCode, 200);
  t.is(res.type, "text/css");
  t.is(res.charset, "utf-8");
  t.regex(res.text, /^\.hljs/m);
});

test("feed", async function(t) {
  const res = await request(app.listen()).get("/index.xml");

  t.is(res.statusCode, 200);
  t.is(res.type, "application/atom+xml");
  t.regex(res.text, /^<\?xml/);

  const $ = parseResponse(res);

  t.is($("feed > title").text(), "John Doe");
  t.is($("feed > link").attr("href"), "http://localhost:3000/");
});

test(notFound, "/post/non-existent", /Post not found/);
test(notFound, "/tag/non-existent", /tag non-existent not found/);