Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unit tests for template extension #95

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
});
});