summary refs log tree commit diff stats
path: root/test/app.test.js
blob: 681008960acf29b206dde467ec1a0b888d838b44 (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
"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("Content-Type", "text/html; charset=utf-8")
    .expect(/^<!DOCTYPE html>/)
    .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 <main> 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"]
            }
          ]);
        })
      ])
    );
});

test("post", t => {
  return request(app.listen())
    .get("/post/testfile")
    .expect(200)
    .expect("Content-Type", "text/html; charset=utf-8")
    .expect(/^<!DOCTYPE html>/)
    .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"
      );
    });
});

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("Content-Type", "text/html; charset=utf-8")
    .expect(/^<!DOCTYPE html>/)
    .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 not found", t =>
  request(app.listen())
    .get("/tag/non-existant")
    .expect(404)
    .expect(/tag non-existant not found/)
    .then(() => t.pass()));