Skip to content

Commit

Permalink
feat: add unit tests for template extension (#95)
Browse files Browse the repository at this point in the history
* feat: add unit tests for template extension

* fix: add readable test names
  • Loading branch information
MitchellPaff authored Oct 7, 2021
1 parent ead7245 commit d1af8ed
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 96 deletions.
7 changes: 6 additions & 1 deletion extensions/template/examples/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
"assets": {
"example": {
"href": "https://example.com/examples/file.xyz",
"template:new_field": "test"
"template:new_field": "test",
"template:xyz": {
"x": 1,
"y": 2,
"z": 3
}
}
},
"item_assets": {
Expand Down
55 changes: 0 additions & 55 deletions extensions/template/non-examples/collection.json

This file was deleted.

40 changes: 0 additions & 40 deletions extensions/template/non-examples/item.json

This file was deleted.

47 changes: 47 additions & 0 deletions extensions/template/tests/template_collection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import o from 'ospec';
import Ajv from 'ajv';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { promises as fs } from 'fs';
import { AjvOptions } from '../../validation.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
const schemaPath = join(__dirname, '..', 'schema.json');
const exampleCollectionPath = join(__dirname, '..', 'examples/collection.json');

o.spec('template-collection', () => {
o.specTimeout(20000);
let validate;
const ajv = new Ajv(AjvOptions);

o.before(async () => {
const data = JSON.parse(await fs.readFile(schemaPath));
validate = await ajv.compileAsync(data);
});

o('Collection should pass validation', async () => {
// given
const templateCollectionExample = JSON.parse(await fs.readFile(exampleCollectionPath));

// when
let valid = validate(templateCollectionExample);

// then
o(valid).equals(true)(JSON.stringify(validate.errors, null, 2));
});

o("Collection without mandatory 'y' field in the 'template:xyz object should fail validation", async () => {
// given
const templateCollectionExample = JSON.parse(await fs.readFile(exampleCollectionPath));
delete templateCollectionExample['assets']['example']['template:xyz']['y'];

// when
let valid = validate(templateCollectionExample);

// then
o(valid).equals(false);
o(validate.errors.some((error) => error.message === "should have required property 'y'")).equals(true)(
JSON.stringify(validate.errors),
);
});
});
46 changes: 46 additions & 0 deletions extensions/template/tests/template_item.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import o from 'ospec';
import Ajv from 'ajv';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { promises as fs } from 'fs';
import { AjvOptions } from '../../validation.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
const schemaPath = join(__dirname, '..', 'schema.json');
const exampleItemPath = join(__dirname, '..', 'examples/item.json');

o.spec('template-item', () => {
o.specTimeout(20000);
let validate;
const ajv = new Ajv(AjvOptions);

o.before(async () => {
const data = JSON.parse(await fs.readFile(schemaPath));
validate = await ajv.compileAsync(data);
});

o('Item should pass validation', async () => {
// given
const templateItemExample = JSON.parse(await fs.readFile(exampleItemPath));

// when
let valid = validate(templateItemExample);

// then
o(valid).equals(true)(JSON.stringify(validate.errors, null, 2));
});

o("Item without mandatory 'template:new_field' property should fail validation", async () => {
// given
const templateItemExample = JSON.parse(await fs.readFile(exampleItemPath));
delete templateItemExample.properties['template:new_field'];
// when
let valid = validate(templateItemExample);

// then
o(valid).equals(false);
o(
validate.errors.some((error) => error.message === "should have required property '['template:new_field']'"),
).equals(true)(JSON.stringify(validate.errors));
});
});

0 comments on commit d1af8ed

Please sign in to comment.