|
| 1 | +const { join } = require('path'); |
| 2 | +const { promises } = require('fs'); |
| 3 | +const { AjvOptions, rootDirectory, schemaPath } = require('./validation.js'); |
| 4 | +const ajv = new (require('ajv'))(AjvOptions); |
| 5 | + |
| 6 | +const examplePath = join(rootDirectory, 'examples/item.json'); |
| 7 | + |
| 8 | +let validate; |
| 9 | +beforeAll(async () => { |
| 10 | + const data = JSON.parse(await promises.readFile(schemaPath)); |
| 11 | + validate = await ajv.compileAsync(data); |
| 12 | +}); |
| 13 | + |
| 14 | +describe('Item example', () => { |
| 15 | + it('should pass validation', async () => { |
| 16 | + // given |
| 17 | + const example = JSON.parse(await promises.readFile(examplePath)); |
| 18 | + |
| 19 | + // when |
| 20 | + let valid = validate(example); |
| 21 | + |
| 22 | + // then |
| 23 | + expect(valid).toBeTruthy(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should fail validation with invalid properties processing:software value', async () => { |
| 27 | + // given |
| 28 | + const example = JSON.parse(await promises.readFile(examplePath)); |
| 29 | + example['properties']['processing:software'] = null; |
| 30 | + |
| 31 | + // when |
| 32 | + let valid = validate(example); |
| 33 | + |
| 34 | + // then |
| 35 | + expect(valid).toBeFalsy(); |
| 36 | + expect( |
| 37 | + validate.errors.some( |
| 38 | + (error) => |
| 39 | + error.instancePath === '/properties/processing:software' && |
| 40 | + error.message === 'must be object', |
| 41 | + ), |
| 42 | + ).toBeTruthy(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should fail validation with invalid asset processing:software value ', async () => { |
| 46 | + // given |
| 47 | + const example = JSON.parse(await promises.readFile(examplePath)); |
| 48 | + example['assets']['manifest']['processing:software'] = null; |
| 49 | + |
| 50 | + // when |
| 51 | + let valid = validate(example); |
| 52 | + |
| 53 | + // then |
| 54 | + expect(valid).toBeFalsy(); |
| 55 | + expect( |
| 56 | + validate.errors.some( |
| 57 | + (error) => |
| 58 | + error.instancePath === '/assets/manifest/processing:software' && |
| 59 | + error.message === 'must be object', |
| 60 | + ), |
| 61 | + ).toBeTruthy(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments