Skip to content

Commit cfe4854

Browse files
committed
Simplify testing AST nodes in buildSchema/extendedSchema tests
1 parent 1e46389 commit cfe4854

File tree

2 files changed

+151
-120
lines changed

2 files changed

+151
-120
lines changed

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 94 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Kind } from '../../language/kinds';
1010
import { parse } from '../../language/parser';
1111
import { print } from '../../language/printer';
1212

13-
import type { GraphQLNamedType } from '../../type/definition';
1413
import { GraphQLSchema } from '../../type/schema';
1514
import { validateSchema } from '../../type/validate';
1615
import { __Schema, __EnumValue } from '../../type/introspection';
@@ -51,18 +50,16 @@ function cycleSDL(sdl: string): string {
5150
return printSchema(buildSchema(sdl));
5251
}
5352

54-
function printASTNode(obj: ?{ +astNode: ?ASTNode, ... }): string {
55-
// istanbul ignore next (FIXME)
53+
function expectASTNode(obj: ?{ +astNode: ?ASTNode, ... }) {
5654
invariant(obj?.astNode != null);
57-
return print(obj.astNode);
55+
return expect(print(obj.astNode));
5856
}
5957

60-
function printAllASTNodes(obj: GraphQLNamedType): string {
61-
invariant(obj.astNode != null);
62-
return print({
63-
kind: Kind.DOCUMENT,
64-
definitions: [obj.astNode, ...obj.extensionASTNodes],
65-
});
58+
function expectExtensionASTNodes(obj: {
59+
+extensionASTNodes: $ReadOnlyArray<ASTNode>,
60+
...
61+
}) {
62+
return expect(obj.extensionASTNodes.map(print).join('\n\n'));
6663
}
6764

6865
describe('Schema Builder', () => {
@@ -714,15 +711,11 @@ describe('Schema Builder', () => {
714711
});
715712

716713
it('Correctly extend scalar type', () => {
717-
const scalarSDL = dedent`
714+
const schema = buildSchema(`
718715
scalar SomeScalar
719-
720716
extend scalar SomeScalar @foo
721-
722717
extend scalar SomeScalar @bar
723-
`;
724-
const schema = buildSchema(`
725-
${scalarSDL}
718+
726719
directive @foo on SCALAR
727720
directive @bar on SCALAR
728721
`);
@@ -732,11 +725,16 @@ describe('Schema Builder', () => {
732725
scalar SomeScalar
733726
`);
734727

735-
expect(printAllASTNodes(someScalar)).to.equal(scalarSDL);
728+
expectASTNode(someScalar).to.equal('scalar SomeScalar');
729+
expectExtensionASTNodes(someScalar).to.equal(dedent`
730+
extend scalar SomeScalar @foo
731+
732+
extend scalar SomeScalar @bar
733+
`);
736734
});
737735

738736
it('Correctly extend object type', () => {
739-
const objectSDL = dedent`
737+
const schema = buildSchema(`
740738
type SomeObject implements Foo {
741739
first: String
742740
}
@@ -748,9 +746,7 @@ describe('Schema Builder', () => {
748746
extend type SomeObject implements Baz {
749747
third: Float
750748
}
751-
`;
752-
const schema = buildSchema(`
753-
${objectSDL}
749+
754750
interface Foo
755751
interface Bar
756752
interface Baz
@@ -765,11 +761,24 @@ describe('Schema Builder', () => {
765761
}
766762
`);
767763

768-
expect(printAllASTNodes(someObject)).to.equal(objectSDL);
764+
expectASTNode(someObject).to.equal(dedent`
765+
type SomeObject implements Foo {
766+
first: String
767+
}
768+
`);
769+
expectExtensionASTNodes(someObject).to.equal(dedent`
770+
extend type SomeObject implements Bar {
771+
second: Int
772+
}
773+
774+
extend type SomeObject implements Baz {
775+
third: Float
776+
}
777+
`);
769778
});
770779

771780
it('Correctly extend interface type', () => {
772-
const interfaceSDL = dedent`
781+
const schema = buildSchema(dedent`
773782
interface SomeInterface {
774783
first: String
775784
}
@@ -781,8 +790,7 @@ describe('Schema Builder', () => {
781790
extend interface SomeInterface {
782791
third: Float
783792
}
784-
`;
785-
const schema = buildSchema(interfaceSDL);
793+
`);
786794

787795
const someInterface = assertInterfaceType(schema.getType('SomeInterface'));
788796
expect(printType(someInterface)).to.equal(dedent`
@@ -793,19 +801,28 @@ describe('Schema Builder', () => {
793801
}
794802
`);
795803

796-
expect(printAllASTNodes(someInterface)).to.equal(interfaceSDL);
804+
expectASTNode(someInterface).to.equal(dedent`
805+
interface SomeInterface {
806+
first: String
807+
}
808+
`);
809+
expectExtensionASTNodes(someInterface).to.equal(dedent`
810+
extend interface SomeInterface {
811+
second: Int
812+
}
813+
814+
extend interface SomeInterface {
815+
third: Float
816+
}
817+
`);
797818
});
798819

799820
it('Correctly extend union type', () => {
800-
const unionSDL = dedent`
821+
const schema = buildSchema(`
801822
union SomeUnion = FirstType
802-
803823
extend union SomeUnion = SecondType
804-
805824
extend union SomeUnion = ThirdType
806-
`;
807-
const schema = buildSchema(`
808-
${unionSDL}
825+
809826
type FirstType
810827
type SecondType
811828
type ThirdType
@@ -816,11 +833,16 @@ describe('Schema Builder', () => {
816833
union SomeUnion = FirstType | SecondType | ThirdType
817834
`);
818835

819-
expect(printAllASTNodes(someUnion)).to.equal(unionSDL);
836+
expectASTNode(someUnion).to.equal('union SomeUnion = FirstType');
837+
expectExtensionASTNodes(someUnion).to.equal(dedent`
838+
extend union SomeUnion = SecondType
839+
840+
extend union SomeUnion = ThirdType
841+
`);
820842
});
821843

822844
it('Correctly extend enum type', () => {
823-
const enumSDL = dedent`
845+
const schema = buildSchema(dedent`
824846
enum SomeEnum {
825847
FIRST
826848
}
@@ -832,8 +854,7 @@ describe('Schema Builder', () => {
832854
extend enum SomeEnum {
833855
THIRD
834856
}
835-
`;
836-
const schema = buildSchema(enumSDL);
857+
`);
837858

838859
const someEnum = assertEnumType(schema.getType('SomeEnum'));
839860
expect(printType(someEnum)).to.equal(dedent`
@@ -844,11 +865,24 @@ describe('Schema Builder', () => {
844865
}
845866
`);
846867

847-
expect(printAllASTNodes(someEnum)).to.equal(enumSDL);
868+
expectASTNode(someEnum).to.equal(dedent`
869+
enum SomeEnum {
870+
FIRST
871+
}
872+
`);
873+
expectExtensionASTNodes(someEnum).to.equal(dedent`
874+
extend enum SomeEnum {
875+
SECOND
876+
}
877+
878+
extend enum SomeEnum {
879+
THIRD
880+
}
881+
`);
848882
});
849883

850884
it('Correctly extend input object type', () => {
851-
const inputSDL = dedent`
885+
const schema = buildSchema(dedent`
852886
input SomeInput {
853887
first: String
854888
}
@@ -860,8 +894,7 @@ describe('Schema Builder', () => {
860894
extend input SomeInput {
861895
third: Float
862896
}
863-
`;
864-
const schema = buildSchema(inputSDL);
897+
`);
865898

866899
const someInput = assertInputObjectType(schema.getType('SomeInput'));
867900
expect(printType(someInput)).to.equal(dedent`
@@ -872,7 +905,20 @@ describe('Schema Builder', () => {
872905
}
873906
`);
874907

875-
expect(printAllASTNodes(someInput)).to.equal(inputSDL);
908+
expectASTNode(someInput).to.equal(dedent`
909+
input SomeInput {
910+
first: String
911+
}
912+
`);
913+
expectExtensionASTNodes(someInput).to.equal(dedent`
914+
extend input SomeInput {
915+
second: Int
916+
}
917+
918+
extend input SomeInput {
919+
third: Float
920+
}
921+
`);
876922
});
877923

878924
it('Correctly assign AST nodes', () => {
@@ -932,25 +978,23 @@ describe('Schema Builder', () => {
932978
]).to.be.deep.equal(ast.definitions);
933979

934980
const testField = query.getFields().testField;
935-
expect(printASTNode(testField)).to.equal(
981+
expectASTNode(testField).to.equal(
936982
'testField(testArg: TestInput): TestUnion',
937983
);
938-
expect(printASTNode(testField.args[0])).to.equal('testArg: TestInput');
939-
expect(printASTNode(testInput.getFields().testInputField)).to.equal(
984+
expectASTNode(testField.args[0]).to.equal('testArg: TestInput');
985+
expectASTNode(testInput.getFields().testInputField).to.equal(
940986
'testInputField: TestEnum',
941987
);
942988

943-
expect(printASTNode(testEnum.getValue('TEST_VALUE'))).to.equal(
944-
'TEST_VALUE',
945-
);
989+
expectASTNode(testEnum.getValue('TEST_VALUE')).to.equal('TEST_VALUE');
946990

947-
expect(printASTNode(testInterface.getFields().interfaceField)).to.equal(
991+
expectASTNode(testInterface.getFields().interfaceField).to.equal(
948992
'interfaceField: String',
949993
);
950-
expect(printASTNode(testType.getFields().interfaceField)).to.equal(
994+
expectASTNode(testType.getFields().interfaceField).to.equal(
951995
'interfaceField: String',
952996
);
953-
expect(printASTNode(testDirective.args[0])).to.equal('arg: TestScalar');
997+
expectASTNode(testDirective.args[0]).to.equal('arg: TestScalar');
954998
});
955999

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

0 commit comments

Comments
 (0)