forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.js
91 lines (83 loc) · 3.83 KB
/
graphql.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
const previewsJson = require('../../lib/graphql/static/previews')
const upcomingChangesJson = require('../../lib/graphql/static/upcoming-changes')
const prerenderedObjectsJson = require('../../lib/graphql/static/prerendered-objects')
const { schemaValidator, previewsValidator, upcomingChangesValidator } = require('../../lib/graphql/validator')
const revalidator = require('revalidator')
const allVersions = Object.values(require('../../lib/all-versions'))
const graphqlVersions = allVersions.map(v => v.miscVersionName)
const graphqlTypes = require('../../lib/graphql/types').map(t => t.kind)
describe('graphql json files', () => {
jest.setTimeout(3 * 60 * 1000)
test('static files have versions as top-level keys', () => {
graphqlVersions.forEach(version => {
expect(version in previewsJson).toBe(true)
expect(version in upcomingChangesJson).toBe(true)
expect(version in prerenderedObjectsJson).toBe(true)
})
})
test('schemas object validation', () => {
graphqlVersions.forEach(version => {
const schemaJsonPerVersion = require(`../../lib/graphql/static/schema-${version}`)
// all graphql types are arrays except for queries
graphqlTypes
.filter(type => type !== 'queries')
.forEach(type => {
schemaJsonPerVersion[type].forEach(typeObj => {
const { valid, errors } = revalidator.validate(typeObj, schemaValidator[type])
const errorMessage = JSON.stringify(errors, null, 2)
expect(valid, errorMessage).toBe(true)
})
})
// check query connections separately
schemaJsonPerVersion.queries.connections.forEach(connection => {
const { valid, errors } = revalidator.validate(connection, schemaValidator.queryConnections)
const errorMessage = JSON.stringify(errors, null, 2)
expect(valid, errorMessage).toBe(true)
})
// check query fields separately
schemaJsonPerVersion.queries.fields.forEach(field => {
const { valid, errors } = revalidator.validate(field, schemaValidator.queryFields)
const errorMessage = JSON.stringify(errors, null, 2)
expect(valid, errorMessage).toBe(true)
})
})
})
test('previews object validation', () => {
graphqlVersions.forEach(version => {
previewsJson[version].forEach(previewObj => {
const { valid, errors } = revalidator.validate(previewObj, previewsValidator)
const errorMessage = JSON.stringify(errors, null, 2)
expect(valid, errorMessage).toBe(true)
})
})
})
test('upcoming changes object validation', () => {
graphqlVersions.forEach(version => {
Object.values(upcomingChangesJson[version]).forEach(changes => {
// each object value is an array of changes
changes.forEach(changeObj => {
const { valid, errors } = revalidator.validate(changeObj, upcomingChangesValidator)
const errorMessage = JSON.stringify(errors, null, 2)
expect(valid, errorMessage).toBe(true)
})
})
})
})
test('prerendered objects validation', () => {
graphqlVersions.forEach(version => {
// shape of prerenderedObject: {
// html: <div>foo</div>,
// miniToc: {contents: '<a>bar</a>', headingLevel: N, indentationLevel: N}
// }
const prerenderedHtml = prerenderedObjectsJson[version].html
expect(typeof prerenderedHtml).toBe('string')
expect(prerenderedHtml.startsWith('<div>')).toBe(true)
const prerenderedMiniToc = prerenderedObjectsJson[version].miniToc
expect(Array.isArray(prerenderedMiniToc)).toBe(true)
expect(prerenderedMiniToc.length).toBeGreaterThan(0)
expect(typeof prerenderedMiniToc[0].contents).toBe('string')
expect(typeof prerenderedMiniToc[0].headingLevel).toBe('number')
expect(typeof prerenderedMiniToc[0].indentationLevel).toBe('number')
})
})
})