-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-fixtures.mjs
234 lines (223 loc) · 7.05 KB
/
test-fixtures.mjs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import fetch from "node-fetch";
import fs from "fs";
import { diffChars } from "diff";
import { globSync } from "glob";
import { html_beautify } from "js-beautify/js/lib/beautify-html.js";
const pass = (message) => {
console.log("\x1b[42m%s\x1b[0m", " PASS ", "\x1b[0m", message);
};
const fail = (message) => {
console.error("\x1b[41m%s\x1b[0m", " FAIL ", "\x1b[0m", message);
};
console.log("Running tests...");
const testEndpoint = "http://127.0.0.1:5001/";
const standardiseHtml = (html) =>
html_beautify(
html
.replace(/(\n\s*){1,}/g, "")
.replace(/\s{2,}/g, " ")
.replace(/(\w+)="([^"]*)\s+"/g, '$1="$2"')
.replace(/(\w+)="\s+([^"]*)"/g, '$1="$2"'),
{
"wrap-attributes": "force",
"preserve-newlines": false,
},
);
const tnaFrontendDirectory = "node_modules/@nationalarchives/frontend";
const fixturesDirectory = `${tnaFrontendDirectory}/nationalarchives/components/`;
const utilitiesFixturesDirectory = `${tnaFrontendDirectory}/nationalarchives/utilities/`;
const components = globSync(`${fixturesDirectory}*/fixtures.json`)
.map((componentFixtureFile) => {
const name = componentFixtureFile
.replace(new RegExp(`^${fixturesDirectory}`), "")
.replace(new RegExp(/\/fixtures.json$/), "");
return {
name,
testUrl: `${testEndpoint}components/${name}`,
fixtures: [],
};
})
.map((component) => {
const { fixtures } = JSON.parse(
fs.readFileSync(
`${fixturesDirectory}${component.name}/fixtures.json`,
"utf8",
),
);
return {
...component,
fixtures,
};
})
.reverse();
for (let i = 0; i < components.length; i++) {
const component = components[i];
console.log(`\nComponent: ${component.name}`);
const { fixtures } = JSON.parse(
fs.readFileSync(
`${fixturesDirectory}${component.name}/fixtures.json`,
"utf8",
),
);
for (let j = 0; j < component.fixtures.length; j++) {
const fixture = component.fixtures[j];
const testUrl = `${component.testUrl}?params=${encodeURIComponent(
JSON.stringify(fixture.options),
)}`;
const response = await fetch(testUrl)
.then((response) => {
if (response.status >= 400 && response.status < 600) {
fail(`${fixture.name}\n`);
throw new Error("Bad response from server");
}
return response;
})
.catch((e) => {
fail(`${fixture.name}\n`);
console.error(e, testUrl);
});
const body = await response.text();
const bodyPretty = standardiseHtml(body);
const fixturePretty = standardiseHtml(fixture.html);
const mismatch = bodyPretty !== fixturePretty;
if (mismatch) {
fail(`${fixture.name}\n`);
console.error(testUrl);
const diff = diffChars(bodyPretty, fixturePretty)
.map(
(part) =>
`${
part.added ? "\x1b[32m" : part.removed ? "\x1b[31m" : "\x1b[0m"
}${part.value === " " ? "█" : part.value}`,
)
.join("");
console.log(diff);
console.log("\n");
console.log("GREEN text shows expected content that wasn't rendered");
console.log("RED text shows rendered content that wasn't expected");
process.exitCode = 1;
throw new Error("Fixtures tests failed");
} else {
pass(fixture.name);
}
}
}
const utilities = globSync(`${utilitiesFixturesDirectory}*/fixtures.json`)
.map((utilitiesFixtureFile) => {
const name = utilitiesFixtureFile
.replace(new RegExp(`^${utilitiesFixturesDirectory}`), "")
.replace(new RegExp(/\/fixtures.json$/), "");
return {
name,
testUrl: `${testEndpoint}utilities/${name}`,
fixtures: [],
};
})
.map((utility) => {
const { fixtures } = JSON.parse(
fs.readFileSync(
`${utilitiesFixturesDirectory}${utility.name}/fixtures.json`,
"utf8",
),
);
return {
...utility,
fixtures,
};
})
.reverse();
for (let i = 0; i < utilities.length; i++) {
const utility = utilities[i];
console.log(`\nUtility: ${utility.name}`);
const { fixtures } = JSON.parse(
fs.readFileSync(
`${utilitiesFixturesDirectory}${utility.name}/fixtures.json`,
"utf8",
),
);
for (let j = 0; j < utility.fixtures.length; j++) {
const fixture = utility.fixtures[j];
const testUrl = `${utility.testUrl}?params=${encodeURIComponent(
JSON.stringify(fixture.options),
)}`;
const response = await fetch(testUrl)
.then((response) => {
if (response.status >= 400 && response.status < 600) {
fail(`${fixture.name}\n`);
throw new Error("Bad response from server");
}
return response;
})
.catch((e) => {
fail(`${fixture.name}\n`);
console.error(e, testUrl);
});
const body = await response.text();
const bodyPretty = standardiseHtml(body);
const fixturePretty = standardiseHtml(fixture.html);
const mismatch = bodyPretty !== fixturePretty;
if (mismatch) {
fail(`${fixture.name}\n`);
console.error(testUrl);
const diff = diffChars(bodyPretty, fixturePretty)
.map(
(part) =>
`${
part.added ? "\x1b[32m" : part.removed ? "\x1b[31m" : "\x1b[0m"
}${part.value === " " ? "█" : part.value}`,
)
.join("");
console.log(diff);
console.log("\n");
console.log("GREEN text shows expected content that wasn't rendered");
console.log("RED text shows rendered content that wasn't expected");
process.exitCode = 1;
throw new Error("Fixtures tests failed");
} else {
pass(fixture.name);
}
}
}
const templatesDirectory = `${tnaFrontendDirectory}/nationalarchives/templates/`;
const { fixtures } = JSON.parse(
fs.readFileSync(`${templatesDirectory}fixtures.json`, "utf8"),
);
const genericFixture = fixtures.find((fixture) => fixture.name === "generic");
const testUrl = `${testEndpoint}templates/base`;
console.log("\nTemplates");
const response = await fetch(testUrl)
.then((response) => {
if (response.status >= 400 && response.status < 600) {
fail(`${genericFixture.name}\n`);
throw new Error("Bad response from server");
}
return response;
})
.catch((e) => {
fail(`${genericFixture.name}\n`);
console.error(e, testUrl);
});
const body = await response.text();
const bodyPretty = standardiseHtml(body);
const fixturePretty = standardiseHtml(genericFixture.html);
const mismatch = bodyPretty !== fixturePretty;
if (mismatch) {
fail(`${genericFixture.name}\n`);
console.error(testUrl);
const diff = diffChars(bodyPretty, fixturePretty)
.map(
(part) =>
`${
part.added ? "\x1b[32m" : part.removed ? "\x1b[31m" : "\x1b[0m"
}${part.value === " " ? "█" : part.value}`,
)
.join("");
console.log(diff);
console.log("\n");
console.log("GREEN text shows expected content that wasn't rendered");
console.log("RED text shows rendered content that wasn't expected");
process.exitCode = 1;
throw new Error("Fixtures tests failed");
} else {
pass(genericFixture.name);
}