Skip to content

Commit

Permalink
Simplify testing AST nodes in buildSchema/extendedSchema tests (#2998)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Mar 28, 2021
1 parent 1e46389 commit 33c069c
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 119 deletions.
143 changes: 94 additions & 49 deletions src/utilities/__tests__/buildASTSchema-test.js
Expand Up @@ -10,7 +10,6 @@ import { Kind } from '../../language/kinds';
import { parse } from '../../language/parser';
import { print } from '../../language/printer';

import type { GraphQLNamedType } from '../../type/definition';
import { GraphQLSchema } from '../../type/schema';
import { validateSchema } from '../../type/validate';
import { __Schema, __EnumValue } from '../../type/introspection';
Expand Down Expand Up @@ -51,18 +50,17 @@ function cycleSDL(sdl: string): string {
return printSchema(buildSchema(sdl));
}

function printASTNode(obj: ?{ +astNode: ?ASTNode, ... }): string {
function expectASTNode(obj: ?{ +astNode: ?ASTNode, ... }) {
// istanbul ignore next (FIXME)
invariant(obj?.astNode != null);
return print(obj.astNode);
return expect(print(obj.astNode));
}

function printAllASTNodes(obj: GraphQLNamedType): string {
invariant(obj.astNode != null);
return print({
kind: Kind.DOCUMENT,
definitions: [obj.astNode, ...obj.extensionASTNodes],
});
function expectExtensionASTNodes(obj: {
+extensionASTNodes: $ReadOnlyArray<ASTNode>,
...
}) {
return expect(obj.extensionASTNodes.map(print).join('\n\n'));
}

describe('Schema Builder', () => {
Expand Down Expand Up @@ -714,15 +712,11 @@ describe('Schema Builder', () => {
});

it('Correctly extend scalar type', () => {
const scalarSDL = dedent`
const schema = buildSchema(`
scalar SomeScalar
extend scalar SomeScalar @foo
extend scalar SomeScalar @bar
`;
const schema = buildSchema(`
${scalarSDL}
directive @foo on SCALAR
directive @bar on SCALAR
`);
Expand All @@ -732,11 +726,16 @@ describe('Schema Builder', () => {
scalar SomeScalar
`);

expect(printAllASTNodes(someScalar)).to.equal(scalarSDL);
expectASTNode(someScalar).to.equal('scalar SomeScalar');
expectExtensionASTNodes(someScalar).to.equal(dedent`
extend scalar SomeScalar @foo
extend scalar SomeScalar @bar
`);
});

it('Correctly extend object type', () => {
const objectSDL = dedent`
const schema = buildSchema(`
type SomeObject implements Foo {
first: String
}
Expand All @@ -748,9 +747,7 @@ describe('Schema Builder', () => {
extend type SomeObject implements Baz {
third: Float
}
`;
const schema = buildSchema(`
${objectSDL}
interface Foo
interface Bar
interface Baz
Expand All @@ -765,11 +762,24 @@ describe('Schema Builder', () => {
}
`);

expect(printAllASTNodes(someObject)).to.equal(objectSDL);
expectASTNode(someObject).to.equal(dedent`
type SomeObject implements Foo {
first: String
}
`);
expectExtensionASTNodes(someObject).to.equal(dedent`
extend type SomeObject implements Bar {
second: Int
}
extend type SomeObject implements Baz {
third: Float
}
`);
});

it('Correctly extend interface type', () => {
const interfaceSDL = dedent`
const schema = buildSchema(dedent`
interface SomeInterface {
first: String
}
Expand All @@ -781,8 +791,7 @@ describe('Schema Builder', () => {
extend interface SomeInterface {
third: Float
}
`;
const schema = buildSchema(interfaceSDL);
`);

const someInterface = assertInterfaceType(schema.getType('SomeInterface'));
expect(printType(someInterface)).to.equal(dedent`
Expand All @@ -793,19 +802,28 @@ describe('Schema Builder', () => {
}
`);

expect(printAllASTNodes(someInterface)).to.equal(interfaceSDL);
expectASTNode(someInterface).to.equal(dedent`
interface SomeInterface {
first: String
}
`);
expectExtensionASTNodes(someInterface).to.equal(dedent`
extend interface SomeInterface {
second: Int
}
extend interface SomeInterface {
third: Float
}
`);
});

it('Correctly extend union type', () => {
const unionSDL = dedent`
const schema = buildSchema(`
union SomeUnion = FirstType
extend union SomeUnion = SecondType
extend union SomeUnion = ThirdType
`;
const schema = buildSchema(`
${unionSDL}
type FirstType
type SecondType
type ThirdType
Expand All @@ -816,11 +834,16 @@ describe('Schema Builder', () => {
union SomeUnion = FirstType | SecondType | ThirdType
`);

expect(printAllASTNodes(someUnion)).to.equal(unionSDL);
expectASTNode(someUnion).to.equal('union SomeUnion = FirstType');
expectExtensionASTNodes(someUnion).to.equal(dedent`
extend union SomeUnion = SecondType
extend union SomeUnion = ThirdType
`);
});

it('Correctly extend enum type', () => {
const enumSDL = dedent`
const schema = buildSchema(dedent`
enum SomeEnum {
FIRST
}
Expand All @@ -832,8 +855,7 @@ describe('Schema Builder', () => {
extend enum SomeEnum {
THIRD
}
`;
const schema = buildSchema(enumSDL);
`);

const someEnum = assertEnumType(schema.getType('SomeEnum'));
expect(printType(someEnum)).to.equal(dedent`
Expand All @@ -844,11 +866,24 @@ describe('Schema Builder', () => {
}
`);

expect(printAllASTNodes(someEnum)).to.equal(enumSDL);
expectASTNode(someEnum).to.equal(dedent`
enum SomeEnum {
FIRST
}
`);
expectExtensionASTNodes(someEnum).to.equal(dedent`
extend enum SomeEnum {
SECOND
}
extend enum SomeEnum {
THIRD
}
`);
});

it('Correctly extend input object type', () => {
const inputSDL = dedent`
const schema = buildSchema(dedent`
input SomeInput {
first: String
}
Expand All @@ -860,8 +895,7 @@ describe('Schema Builder', () => {
extend input SomeInput {
third: Float
}
`;
const schema = buildSchema(inputSDL);
`);

const someInput = assertInputObjectType(schema.getType('SomeInput'));
expect(printType(someInput)).to.equal(dedent`
Expand All @@ -872,7 +906,20 @@ describe('Schema Builder', () => {
}
`);

expect(printAllASTNodes(someInput)).to.equal(inputSDL);
expectASTNode(someInput).to.equal(dedent`
input SomeInput {
first: String
}
`);
expectExtensionASTNodes(someInput).to.equal(dedent`
extend input SomeInput {
second: Int
}
extend input SomeInput {
third: Float
}
`);
});

it('Correctly assign AST nodes', () => {
Expand Down Expand Up @@ -932,25 +979,23 @@ describe('Schema Builder', () => {
]).to.be.deep.equal(ast.definitions);

const testField = query.getFields().testField;
expect(printASTNode(testField)).to.equal(
expectASTNode(testField).to.equal(
'testField(testArg: TestInput): TestUnion',
);
expect(printASTNode(testField.args[0])).to.equal('testArg: TestInput');
expect(printASTNode(testInput.getFields().testInputField)).to.equal(
expectASTNode(testField.args[0]).to.equal('testArg: TestInput');
expectASTNode(testInput.getFields().testInputField).to.equal(
'testInputField: TestEnum',
);

expect(printASTNode(testEnum.getValue('TEST_VALUE'))).to.equal(
'TEST_VALUE',
);
expectASTNode(testEnum.getValue('TEST_VALUE')).to.equal('TEST_VALUE');

expect(printASTNode(testInterface.getFields().interfaceField)).to.equal(
expectASTNode(testInterface.getFields().interfaceField).to.equal(
'interfaceField: String',
);
expect(printASTNode(testType.getFields().interfaceField)).to.equal(
expectASTNode(testType.getFields().interfaceField).to.equal(
'interfaceField: String',
);
expect(printASTNode(testDirective.args[0])).to.equal('arg: TestScalar');
expectASTNode(testDirective.args[0]).to.equal('arg: TestScalar');
});

it('Root operation types with custom names', () => {
Expand Down

0 comments on commit 33c069c

Please sign in to comment.