Skip to content

Commit d1af8ed

Browse files
authored
feat: add unit tests for template extension (#95)
* feat: add unit tests for template extension * fix: add readable test names
1 parent ead7245 commit d1af8ed

File tree

5 files changed

+99
-96
lines changed

5 files changed

+99
-96
lines changed

extensions/template/examples/collection.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
"assets": {
2828
"example": {
2929
"href": "https://example.com/examples/file.xyz",
30-
"template:new_field": "test"
30+
"template:new_field": "test",
31+
"template:xyz": {
32+
"x": 1,
33+
"y": 2,
34+
"z": 3
35+
}
3136
}
3237
},
3338
"item_assets": {

extensions/template/non-examples/collection.json

Lines changed: 0 additions & 55 deletions
This file was deleted.

extensions/template/non-examples/item.json

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import o from 'ospec';
2+
import Ajv from 'ajv';
3+
import { dirname, join } from 'path';
4+
import { fileURLToPath } from 'url';
5+
import { promises as fs } from 'fs';
6+
import { AjvOptions } from '../../validation.js';
7+
8+
const __dirname = dirname(fileURLToPath(import.meta.url));
9+
const schemaPath = join(__dirname, '..', 'schema.json');
10+
const exampleCollectionPath = join(__dirname, '..', 'examples/collection.json');
11+
12+
o.spec('template-collection', () => {
13+
o.specTimeout(20000);
14+
let validate;
15+
const ajv = new Ajv(AjvOptions);
16+
17+
o.before(async () => {
18+
const data = JSON.parse(await fs.readFile(schemaPath));
19+
validate = await ajv.compileAsync(data);
20+
});
21+
22+
o('Collection should pass validation', async () => {
23+
// given
24+
const templateCollectionExample = JSON.parse(await fs.readFile(exampleCollectionPath));
25+
26+
// when
27+
let valid = validate(templateCollectionExample);
28+
29+
// then
30+
o(valid).equals(true)(JSON.stringify(validate.errors, null, 2));
31+
});
32+
33+
o("Collection without mandatory 'y' field in the 'template:xyz object should fail validation", async () => {
34+
// given
35+
const templateCollectionExample = JSON.parse(await fs.readFile(exampleCollectionPath));
36+
delete templateCollectionExample['assets']['example']['template:xyz']['y'];
37+
38+
// when
39+
let valid = validate(templateCollectionExample);
40+
41+
// then
42+
o(valid).equals(false);
43+
o(validate.errors.some((error) => error.message === "should have required property 'y'")).equals(true)(
44+
JSON.stringify(validate.errors),
45+
);
46+
});
47+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import o from 'ospec';
2+
import Ajv from 'ajv';
3+
import { dirname, join } from 'path';
4+
import { fileURLToPath } from 'url';
5+
import { promises as fs } from 'fs';
6+
import { AjvOptions } from '../../validation.js';
7+
8+
const __dirname = dirname(fileURLToPath(import.meta.url));
9+
const schemaPath = join(__dirname, '..', 'schema.json');
10+
const exampleItemPath = join(__dirname, '..', 'examples/item.json');
11+
12+
o.spec('template-item', () => {
13+
o.specTimeout(20000);
14+
let validate;
15+
const ajv = new Ajv(AjvOptions);
16+
17+
o.before(async () => {
18+
const data = JSON.parse(await fs.readFile(schemaPath));
19+
validate = await ajv.compileAsync(data);
20+
});
21+
22+
o('Item should pass validation', async () => {
23+
// given
24+
const templateItemExample = JSON.parse(await fs.readFile(exampleItemPath));
25+
26+
// when
27+
let valid = validate(templateItemExample);
28+
29+
// then
30+
o(valid).equals(true)(JSON.stringify(validate.errors, null, 2));
31+
});
32+
33+
o("Item without mandatory 'template:new_field' property should fail validation", async () => {
34+
// given
35+
const templateItemExample = JSON.parse(await fs.readFile(exampleItemPath));
36+
delete templateItemExample.properties['template:new_field'];
37+
// when
38+
let valid = validate(templateItemExample);
39+
40+
// then
41+
o(valid).equals(false);
42+
o(
43+
validate.errors.some((error) => error.message === "should have required property '['template:new_field']'"),
44+
).equals(true)(JSON.stringify(validate.errors));
45+
});
46+
});

0 commit comments

Comments
 (0)