/
validation-test.js
102 lines (87 loc) · 2.5 KB
/
validation-test.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// @flow strict
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parse } from '../../language/parser';
import { TypeInfo } from '../../utilities/TypeInfo';
import { validate } from '../validate';
import { specifiedRules } from '../specifiedRules';
import { testSchema } from './harness';
describe('Validate: Supports full validation', () => {
it('validates queries', () => {
const doc = parse(`
query {
catOrDog {
... on Cat {
furColor
}
... on Dog {
isHousetrained
}
}
}
`);
const errors = validate(testSchema, doc);
expect(errors).to.deep.equal([]);
});
it('detects bad scalar parse', () => {
const doc = parse(`
query {
invalidArg(arg: "bad value")
}
`);
const errors = validate(testSchema, doc);
expect(errors).to.deep.equal([
{
locations: [{ line: 3, column: 25 }],
message:
'Expected type Invalid, found "bad value"; Invalid scalar is always invalid: "bad value"',
},
]);
});
// NOTE: experimental
it('validates using a custom TypeInfo', () => {
// This TypeInfo will never return a valid field.
const typeInfo = new TypeInfo(testSchema, () => null);
const doc = parse(`
query {
catOrDog {
... on Cat {
furColor
}
... on Dog {
isHousetrained
}
}
}
`);
const errors = validate(testSchema, doc, specifiedRules, typeInfo);
const errorMessages = errors.map(err => err.message);
expect(errorMessages).to.deep.equal([
'Cannot query field "catOrDog" on type "QueryRoot". Did you mean "catOrDog"?',
'Cannot query field "furColor" on type "Cat". Did you mean "furColor"?',
'Cannot query field "isHousetrained" on type "Dog". Did you mean "isHousetrained"?',
]);
});
it('properly calls onError callback when passed', () => {
const doc = parse(`
query {
cat {
name
someNonExistentField
}
dog {
name
anotherNonExistentField
}
}
`);
const expectedNumberOfErrors = 2;
let errorCount = 0;
validate(testSchema, doc, specifiedRules, undefined, (err, ctx) => {
expect(err).to.not.be.a('null');
expect(ctx).to.not.be.a('null');
expect(ctx.getErrors()).to.be.length(++errorCount);
});
expect(errorCount).to.be.equal(expectedNumberOfErrors);
});
});