This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontent.test.js
150 lines (137 loc) · 4.43 KB
/
content.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
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
const helpers = require('./support/built-site-helpers');
const { promisify } = require('util');
const fs = require('fs');
const path = require('path');
const readFile = promisify(fs.readFile);
const parseFrontMatter = require('front-matter');
const categoryDirectory = '_categories';
const agencyDirectory = '_agencies';
const contentDirectory = '_content';
let questionFiles;
let categoryFiles;
let categoryQuestions = [];
beforeAll(async () => {
const questionFilenames = await helpers.getAllFilesInDirectory(contentDirectory);
questionFiles = await Promise.all(questionFilenames.map(async (filename) => {
const content = await readFile(filename, 'utf8');
return {
filename,
content,
frontMatter: parseFrontMatter(content).attributes
};
}));
const categoryFilenames = await helpers.getAllFilesInDirectory(categoryDirectory);
categoryFiles = await Promise.all(categoryFilenames.map(async (filename) => {
const content = await readFile(filename, 'utf8');
return {
filename,
content,
frontMatter: parseFrontMatter(content).attributes
};
}));
categoryFiles.forEach(categoryFile => {
categoryFile.frontMatter.questions.forEach(questionBaseName => {
categoryQuestions.push([categoryFile.frontMatter.name, questionBaseName]);
})
});
});
test('every question is mapped to at least one category', () => {
questionFiles.forEach(questionFile => {
const baseName = path.basename(questionFile.filename, '.md');
let categoryCount = 0;
categoryFiles.forEach(async (categoryFile) => {
if (categoryFile.frontMatter.questions.includes(baseName)) {
categoryCount += 1;
}
});
expect(categoryCount, `${baseName} has at least one category`).toBeGreaterThan(0);
});
});
test('every category question refers to one question that exists', () => {
categoryQuestions.forEach(([category, question]) => {
let questionCount = 0;
questionFiles.forEach(async (questionFile) => {
const baseName = path.basename(questionFile.filename, '.md');
if (question === baseName) {
questionCount += 1;
}
});
expect(questionCount, `there is one question that matches ${category}:${question}`).toEqual(1);
});
});
test(`every question's source references an agency that exists in ${agencyDirectory}`, async () => {
const agencies = await helpers.getAllFilesInDirectory(agencyDirectory);
questionFiles.forEach(file => {
sources = file.frontMatter.sources.forEach(source => {
const expectedAgencyFile = `${path.join(agencyDirectory, source.agency)}.md`;
expect(agencies).toContain(expectedAgencyFile);
})
});
});
test('every category contains only expected frontmatter', async () => {
const expectedKeys = [
'banner',
'layout',
'name',
'owner',
'questions',
'redirect_from',
'title',
];
categoryFiles.forEach(categoryFile => {
Object.keys(categoryFile.frontMatter).forEach(property => {
expect(expectedKeys, `Unexpected property "${property}" in ${categoryFile.filename}"`).toContain(property);
})
});
});
test('every question contains only expected frontmatter', async () => {
const expectedKeys = [
'date',
'excerpt',
'hide_search_in_header',
'layout',
'redirect_from',
'sources',
'title',
];
questionFiles.forEach(questionFile => {
Object.keys(questionFile.frontMatter).forEach(property => {
expect(expectedKeys, `Unexpected property "${property}" in ${questionFile.filename}"`).toContain(property);
})
});
});
test('every question contains required frontmatter', async () => {
const requiredKeys = [
'date',
'excerpt',
'layout',
'sources',
'sources.0.agency',
'sources.0.url',
'title',
];
questionFiles.forEach(questionFile => {
requiredKeys.forEach(property => {
expect(questionFile.frontMatter, `Expected property "${property}" in ${questionFile.filename}" not found`).toHaveProperty(property);
});
});
});
test('every category contains required frontmatter', async () => {
const requiredKeys = [
'banner',
'banner.content',
'banner.display',
'banner.heading',
'layout',
'name',
'owner',
'questions',
'questions.0',
'title',
];
categoryFiles.forEach(categoryFile => {
requiredKeys.forEach(property => {
expect(categoryFile.frontMatter, `Expected property "${property}" in ${categoryFile.filename}" not found`).toHaveProperty(property);
});
});
});