From 81322ab8ed19c4c192d765dc33f0c2edb9bb3ce8 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Tue, 13 Aug 2019 12:40:39 +0300 Subject: [PATCH] starWarsIntrospection-test: cleanup + add explanation comment --- src/__tests__/starWarsIntrospection-test.js | 218 ++++++++------------ 1 file changed, 84 insertions(+), 134 deletions(-) diff --git a/src/__tests__/starWarsIntrospection-test.js b/src/__tests__/starWarsIntrospection-test.js index cbd9749188..7bcfcf9b81 100644 --- a/src/__tests__/starWarsIntrospection-test.js +++ b/src/__tests__/starWarsIntrospection-test.js @@ -7,152 +7,126 @@ import { graphqlSync } from '../graphql'; import { StarWarsSchema } from './starWarsSchema'; +function queryStarWars(source) { + const result = graphqlSync({ schema: StarWarsSchema, source }); + expect(Object.keys(result)).to.deep.equal(['data']); + return result.data; +} + describe('Star Wars Introspection Tests', () => { describe('Basic Introspection', () => { it('Allows querying the schema for types', () => { - const query = ` - query IntrospectionTypeQuery { + const data = queryStarWars(` + { __schema { types { name } } } - `; - const expected = { + `); + + // Include all types used by StarWars schema, introspection types and + // standard directives. For example, `Boolean` is used in `@skip`, + // `@include` and also inside introspection types. + expect(data).to.deep.equal({ __schema: { types: [ - { - name: 'Query', - }, - { - name: 'Episode', - }, - { - name: 'Character', - }, - { - name: 'String', - }, - { - name: 'Human', - }, - { - name: 'Droid', - }, - { - name: '__Schema', - }, - { - name: '__Type', - }, - { - name: '__TypeKind', - }, - { - name: 'Boolean', - }, - { - name: '__Field', - }, - { - name: '__InputValue', - }, - { - name: '__EnumValue', - }, - { - name: '__Directive', - }, - { - name: '__DirectiveLocation', - }, + { name: 'Query' }, + { name: 'Episode' }, + { name: 'Character' }, + { name: 'String' }, + { name: 'Human' }, + { name: 'Droid' }, + { name: '__Schema' }, + { name: '__Type' }, + { name: '__TypeKind' }, + { name: 'Boolean' }, + { name: '__Field' }, + { name: '__InputValue' }, + { name: '__EnumValue' }, + { name: '__Directive' }, + { name: '__DirectiveLocation' }, ], }, - }; - const result = graphqlSync(StarWarsSchema, query); - expect(result).to.deep.equal({ data: expected }); + }); }); it('Allows querying the schema for query type', () => { - const query = ` - query IntrospectionQueryTypeQuery { + const data = queryStarWars(` + { __schema { queryType { name } } } - `; - const expected = { + `); + + expect(data).to.deep.equal({ __schema: { queryType: { name: 'Query', }, }, - }; - const result = graphqlSync(StarWarsSchema, query); - expect(result).to.deep.equal({ data: expected }); + }); }); it('Allows querying the schema for a specific type', () => { - const query = ` - query IntrospectionDroidTypeQuery { + const data = queryStarWars(` + { __type(name: "Droid") { name } } - `; - const expected = { + `); + + expect(data).to.deep.equal({ __type: { name: 'Droid', }, - }; - const result = graphqlSync(StarWarsSchema, query); - expect(result).to.deep.equal({ data: expected }); + }); }); it('Allows querying the schema for an object kind', () => { - const query = ` - query IntrospectionDroidKindQuery { + const data = queryStarWars(` + { __type(name: "Droid") { name kind } } - `; - const expected = { + `); + + expect(data).to.deep.equal({ __type: { name: 'Droid', kind: 'OBJECT', }, - }; - const result = graphqlSync(StarWarsSchema, query); - expect(result).to.deep.equal({ data: expected }); + }); }); it('Allows querying the schema for an interface kind', () => { - const query = ` - query IntrospectionCharacterKindQuery { + const data = queryStarWars(` + { __type(name: "Character") { name kind } } - `; - const expected = { + `); + + expect(data).to.deep.equal({ __type: { name: 'Character', kind: 'INTERFACE', }, - }; - const result = graphqlSync(StarWarsSchema, query); - expect(result).to.deep.equal({ data: expected }); + }); }); it('Allows querying the schema for object fields', () => { - const query = ` - query IntrospectionDroidFieldsQuery { + const data = queryStarWars(` + { __type(name: "Droid") { name fields { @@ -164,64 +138,44 @@ describe('Star Wars Introspection Tests', () => { } } } - `; - const expected = { + `); + + expect(data).to.deep.equal({ __type: { name: 'Droid', fields: [ { name: 'id', - type: { - name: null, - kind: 'NON_NULL', - }, + type: { name: null, kind: 'NON_NULL' }, }, { name: 'name', - type: { - name: 'String', - kind: 'SCALAR', - }, + type: { name: 'String', kind: 'SCALAR' }, }, { name: 'friends', - type: { - name: null, - kind: 'LIST', - }, + type: { name: null, kind: 'LIST' }, }, { name: 'appearsIn', - type: { - name: null, - kind: 'LIST', - }, + type: { name: null, kind: 'LIST' }, }, { name: 'secretBackstory', - type: { - name: 'String', - kind: 'SCALAR', - }, + type: { name: 'String', kind: 'SCALAR' }, }, { name: 'primaryFunction', - type: { - name: 'String', - kind: 'SCALAR', - }, + type: { name: 'String', kind: 'SCALAR' }, }, ], }, - }; - - const result = graphqlSync(StarWarsSchema, query); - expect(result).to.deep.equal({ data: expected }); + }); }); it('Allows querying the schema for nested object fields', () => { - const query = ` - query IntrospectionDroidNestedFieldsQuery { + const data = queryStarWars(` + { __type(name: "Droid") { name fields { @@ -237,8 +191,9 @@ describe('Star Wars Introspection Tests', () => { } } } - `; - const expected = { + `); + + expect(data).to.deep.equal({ __type: { name: 'Droid', fields: [ @@ -301,14 +256,12 @@ describe('Star Wars Introspection Tests', () => { }, ], }, - }; - const result = graphqlSync(StarWarsSchema, query); - expect(result).to.deep.equal({ data: expected }); + }); }); it('Allows querying the schema for field args', () => { - const query = ` - query IntrospectionQueryTypeQuery { + const data = queryStarWars(` + { __schema { queryType { fields { @@ -330,8 +283,9 @@ describe('Star Wars Introspection Tests', () => { } } } - `; - const expected = { + `); + + expect(data).to.deep.equal({ __schema: { queryType: { fields: [ @@ -390,29 +344,25 @@ describe('Star Wars Introspection Tests', () => { ], }, }, - }; - - const result = graphqlSync(StarWarsSchema, query); - expect(result).to.deep.equal({ data: expected }); + }); }); it('Allows querying the schema for documentation', () => { - const query = ` - query IntrospectionDroidDescriptionQuery { + const data = queryStarWars(` + { __type(name: "Droid") { name description } } - `; - const expected = { + `); + + expect(data).to.deep.equal({ __type: { name: 'Droid', description: 'A mechanical creature in the Star Wars universe.', }, - }; - const result = graphqlSync(StarWarsSchema, query); - expect(result).to.deep.equal({ data: expected }); + }); }); }); });