-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate.spec.ts
More file actions
54 lines (52 loc) · 2.19 KB
/
validate.spec.ts
File metadata and controls
54 lines (52 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Validation test file
* Description: This file contains tests which check if any modifications
* make the new code incompatible with the old one.
*/
// Importing the validation function
import { validate } from '../functions/validate';
// Importing mocha and chai for the tests
import { expect } from 'chai';
import 'mocha';
// Creating the test set
describe('Validation test', () => {
it('should return false when data object follows the JSON:API specification', () => {
const test = { data: [{ type: 'article', id: '1' }, { type: 'article', id: '2' }] };
expect(validate(test)).to.equal(false);
});
it('should return true when data object is empty', () => {
const test = { data: [] };
expect(validate(test)).to.equal(true);
});
// Creating throw tests
it('should throw that a data field does not exist', () => {
const test = { example: [] };
expect(() => {
validate(test);
}).to.throw('[Normalize] [JSON:API Syntax Error] A data field does not exist!');
});
it('should throw that data field must be an object or array', () => {
const test = { data: 'example' };
expect(() => {
validate(test);
}).to.throw('[Normalize] [JSON:API Syntax Error] The data field must be an object or array!');
});
it('should throw that the first data item does not contain type field', () => {
const test = { data: [{ id: '3' }] };
expect(() => {
validate(test);
}).to.throw('[Normalize] [JSON:API Syntax Error] A data item does not contain type field!');
});
it('should throw that a data item does not contain type field', () => {
const test = { data: [{ type: 'article' }, { id: '3' }] };
expect(() => {
validate(test);
}).to.throw('[Normalize] [JSON:API Syntax Error] A data item does not contain type field!');
});
it('should throw that not all data items are the same type', () => {
const test = { data: [{ type: 'article' }, { type: 'person' }] };
expect(() => {
validate(test);
}).to.throw('[Normalize] [JSON:API Syntax Error] Not all data items are the same type!');
});
});