forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject-metadata.test.ts
202 lines (186 loc) · 4.74 KB
/
project-metadata.test.ts
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
import fs from 'fs';
import { join } from 'path';
import {
getMetaData,
getProjectMetaPath,
validateMetaData
} from './project-metadata';
const basePath = join(
process.cwd(),
'__fixtures__' + process.env.JEST_WORKER_ID
);
const commonPath = join(basePath, 'curriculum', 'challenges');
const block = 'project-project-metadata';
const metaPath = join(commonPath, '_meta', block);
const superBlockPath = join(
commonPath,
'english',
'superblock-project-metadata'
);
const projectPath = join(superBlockPath, block);
describe('project-metadata helper', () => {
beforeEach(() => {
fs.mkdirSync(superBlockPath, { recursive: true });
fs.mkdirSync(projectPath, { recursive: true });
fs.mkdirSync(metaPath, { recursive: true });
});
describe('getProjectMetaPath helper', () => {
it('should return the meta path', () => {
const expected = join(metaPath, 'meta.json');
process.env.CALLING_DIR = projectPath;
expect(getProjectMetaPath()).toEqual(expected);
});
});
describe('getMetaData helper', () => {
beforeEach(() => {
fs.writeFileSync(
join(projectPath, 'step-001.md'),
'Lorem ipsum...',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-002.md'),
'Lorem ipsum...',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-003.md'),
'Lorem ipsum...',
'utf-8'
);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{
"id": "mock-id",
"challengeOrder": [{"id": "1", "title": "Step 1"}, {"id": "2", "title": "Step 2"}, {"id": "1", "title": "Step 3"}]}`,
'utf-8'
);
});
it('should process requested file', () => {
const expected = {
id: 'mock-id',
challengeOrder: [
{ id: '1', title: 'Step 1' },
{ id: '2', title: 'Step 2' },
{ id: '1', title: 'Step 3' }
]
};
process.env.CALLING_DIR = projectPath;
expect(getMetaData()).toEqual(expected);
});
it('should throw if file is not found', () => {
process.env.CALLING_DIR =
'curriculum/challenges/english/superblock/mick-priject';
const errorPath = join(
'curriculum',
'challenges',
'_meta',
'mick-priject',
'meta.json'
);
expect(() => {
getMetaData();
}).toThrowError(
new Error(`ENOENT: no such file or directory, open '${errorPath}'`)
);
});
});
describe('validateMetaData helper', () => {
it('should throw if a stepfile is missing', () => {
fs.writeFileSync(
join(projectPath, 'step-001.md'),
`---
id: id-1
title: Step 2
challengeType: a
dashedName: step-2
---
`,
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-003.md'),
`---
id: id-3
title: Step 3
challengeType: c
dashedName: step-3
---
`,
'utf-8'
);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{
"id": "mock-id",
"challengeOrder": [{"id": "1", "title": "Step 1"}, {"id": "2", "title": "Step 2"}, {"id": "1", "title": "Step 3"}]}`,
'utf-8'
);
process.env.CALLING_DIR = projectPath;
expect(() => validateMetaData()).toThrow(
`The file
${projectPath}/1.md
does not exist, but is required by the challengeOrder of
${metaPath}/meta.json
To fix this, you can rename the file containing id: 1 to 1.md
If there is no file for this id, then either the challengeOrder needs to be updated, or the file needs to be created.
`
);
});
it('should throw if a step is present in the project, but not the meta', () => {
fs.writeFileSync(
join(projectPath, '1.md'),
`---
id: id-1
title: Step 2
challengeType: a
dashedName: step-2
---
`,
'utf-8'
);
fs.writeFileSync(
join(projectPath, '2.md'),
`---
id: id-2
title: Step 1
challengeType: b
dashedName: step-1
---
`,
'utf-8'
);
fs.writeFileSync(
join(projectPath, '3.md'),
`---
id: id-3
title: Step 3
challengeType: c
dashedName: step-3
---
`,
'utf-8'
);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{
"id": "mock-id",
"challengeOrder": [{"id": "1", "title": "Step 1"}, {"id": "2", "title": "Step 2"}, {"id": "1", "title": "Step 3"}]}`,
'utf-8'
);
process.env.CALLING_DIR = projectPath;
expect(() => validateMetaData()).toThrow(
`File ${projectPath}/3.md should be in the meta.json's challengeOrder`
);
});
});
afterEach(() => {
delete process.env.CALLING_DIR;
try {
fs.rmSync(basePath, { recursive: true });
} catch (err) {
console.log(err);
console.log('Could not remove fixtures folder.');
}
});
});