summary refs log tree commit diff stats
path: root/test/app.test.js
blob: b2f91d1dca6b193bfb9951e88f823ab1c4e7b91c (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
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(/^<!DOCTYPE html>/)
    .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(/^<!DOCTYPE html>/)
    .then(parseResponse)
    .then($ => {
      t.is($("head > title").text(), "This is a test · " + config.site.title);
      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(/This is a test/)
    .then(() => t.pass());
});

test("tags not found", t =>
  request(app.listen())
    .get("/tag/non-existant")
    .expect(404)
    .expect(/tag non-existant not found/)
    .then(() => t.pass()));