-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvice.html.test.js
101 lines (85 loc) · 3.11 KB
/
advice.html.test.js
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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2021 S. K. Medlock, E. K. Herman, K. M. Shaw
// vim: set sts=4 shiftwidth=4 expandtab :
"use strict";
// Libraries used to Mock the DOM and other browser APIs
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
require('jest-fetch-mock').enableMocks();
beforeEach(() => {
// reset the mocking behavior before each new test
fetchMock.resetMocks();
});
let process = require('process');
const advice_filename = 'valportaal-static/advice.html';
async function loadAdvicePage(urlparams='', serverPatientAdvice='') {
fetch.mockResponse(serverPatientAdvice);
let file_url = 'file://' + process.cwd() + '/' + advice_filename;
if (urlparams) {
file_url += '?' + urlparams;
}
let dom = await JSDOM.fromFile(advice_filename,
{
runScripts: "dangerously", // allow scripts (run as user)
includeNodeLocations: true, // track line numbers for debugging
resources: "usable", // allow loading of scripts, stylesheets, etc.
url: file_url
});
dom.window.fetch = fetch;
// advicePageLoad() is typically called by page load,
// but we need to set the fetch first
while (dom.window.advicePageLoad === undefined) {
await new Promise((result) => setTimeout(result, 1));
}
await dom.window.advicePageLoad();
return dom;
}
test("Advice page should contain 'loading' before the javascript runs",
async () => {
// NB: this is how to load the page without running the page's javascript
let dom = await JSDOM.fromFile(advice_filename)
expect(dom.window.document.body.textContent).toEqual(
expect.stringMatching(/laden.../i));
});
test("Advice page should not contain 'loading' after it has finished loading",
async () => {
let dom = await loadAdvicePage("", "");
expect(dom.window.document.body.textContent).toEqual(
expect.not.stringMatching(/laden.../i));
});
test("If id==null, display login button",
async () => {
let dom = await loadAdvicePage("", "");
expect(dom.window.document.body.textContent).toEqual(
expect.stringMatching(/in te loggen/i)
);
});
if (0) {
test("Advice page without patient data should not contain a login",
async () => {
let dom = await loadAdvicePage("id=3", `{
"patient_id": 3,
"patient_advice": []
}`);
expect(dom.window.document.body.textContent).toEqual(
expect.not.stringMatching(/in te loggen/i)
);
});
test("Advice page without patient data should contain 'geen persoon'",
async () => {
let dom = await loadAdvicePage("id=3", `{
"patient_id": 3,
"patient_advice": []
}`);
expect(dom.window.document.body.textContent).toEqual(
expect.stringMatching(/geen persoon/i)
);
});
}
// TODO add some tests for logged-in-with-advice state
test("format advice", async () => {
let dom = await loadAdvicePage();
expect(dom.window.formatAdvice('foo', '')).toBe('<p>foo</p>');
expect(dom.window.formatAdvice('foo: {{free text stuff }}', 'bar'))
.toBe('<p>foo: bar</p>');
});