-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unit tests for template extension (#95)
* feat: add unit tests for template extension * fix: add readable test names
- Loading branch information
1 parent
ead7245
commit d1af8ed
Showing
5 changed files
with
99 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |