Skip to content

Test for toConfig #1683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions src/type/__tests__/definition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { expect } from 'chai';

import inspect from '../../jsutils/inspect';
import { isObjectType } from '../definition';
import mapValue from '../../jsutils/mapValue';

const BlogImage = new GraphQLObjectType({
name: 'Image',
Expand Down Expand Up @@ -1180,3 +1181,199 @@ describe('Type System: NonNull must only accept non-nullable types', () => {
});
}
});

describe('Type System: Configs', () => {
function convertToThunk(config) {
return mapValue(config, (value, key) => {
if (['interfaces', 'fields'].includes(key)) {
return () => value;
}
return value;
});
}

const fields = {
field: {
type: GraphQLString,
deprecationReason: 'deprecated field',
args: {
id: {
defaultValue: 'default',
description: 'String argument',
type: GraphQLString,
},
},
},
};
const expectedFields = {
field: {
...fields.field,
args: {
id: {
...fields.field.args.id,
astNode: undefined,
},
},
astNode: undefined,
resolve: undefined,
description: undefined,
subscribe: undefined,
},
};

const scalarConfig = {
name: 'Scalar',
serialize() {},
parseValue() {},
parseLiteral() {},
};
const expectedScalarConfig = {
...scalarConfig,
astNode: undefined,
description: undefined,
extensionASTNodes: [],
};

const interfaceConfig = {
name: 'Interface',
fields: {},
};
const expectedInterfaceConfig = {
...interfaceConfig,
astNode: undefined,
description: undefined,
resolveType: undefined,
extensionASTNodes: [],
};

const enumConfig = { name: 'Enum', values: { foo: {} } };
const expectedEnumConfig = {
...enumConfig,
values: {
foo: {
astNode: undefined,
description: undefined,
deprecationReason: undefined,
value: 'foo',
},
},
astNode: undefined,
description: undefined,
extensionASTNodes: [],
};

const inputConfig = {
name: 'InputObject',
fields: {},
};
const expectedInputConfig = {
name: 'InputObject',
types: [],
resolveType: undefined,
astNode: undefined,
description: undefined,
extensionASTNodes: [],
};

const objectConfig = {
name: 'Object',
fields,
description: 'Some object type',
isTypeOf: () => true,
interfaces: [new GraphQLInterfaceType(interfaceConfig)],
astNode: null,
extensionASTNodes: [],
};
const expectedObjectConfig = {
...objectConfig,
fields: expectedFields,
};

const unionConfig = {
name: 'Union',
types: [new GraphQLObjectType(objectConfig)],
};
const expectedUnionConfig = {
...unionConfig,
astNode: undefined,
description: undefined,
resolveType: undefined,
extensionASTNodes: [],
};

describe('Configs values', () => {
it('GraphQLObjectType have correct configs', () => {
expect(new GraphQLObjectType(objectConfig).toConfig()).to.deep.equal(
expectedObjectConfig,
);
});

it('GraphQLScalarType have correct configs', () => {
expect(new GraphQLScalarType(scalarConfig).toConfig()).to.deep.equal(
expectedScalarConfig,
);
});

it('GraphQLInterfaceType have correct configs', () => {
expect(
new GraphQLInterfaceType(interfaceConfig).toConfig(),
).to.deep.equal(expectedInterfaceConfig);
});

it('GraphQLUnionType have correct configs', () => {
expect(new GraphQLUnionType(unionConfig).toConfig()).to.deep.equal(
expectedUnionConfig,
);
});

it('GraphQLEnumType have correct configs', () => {
expect(new GraphQLEnumType(enumConfig).toConfig()).to.deep.equal(
expectedEnumConfig,
);
});

it('GraphQLInputObjectType have correct configs', () => {
expect(new GraphQLUnionType(inputConfig).toConfig()).to.deep.equal(
expectedInputConfig,
);
});
});

describe('Configs values with thunk', () => {
it('GraphQLObjectType have correct configs', () => {
expect(
new GraphQLObjectType(convertToThunk(objectConfig)).toConfig(),
).to.deep.equal(expectedObjectConfig);
});

it('GraphQLScalarType have correct configs', () => {
expect(
new GraphQLScalarType(convertToThunk(scalarConfig)).toConfig(),
).to.deep.equal(expectedScalarConfig);
});

it('GraphQLInterfaceType have correct configs', () => {
expect(
new GraphQLInterfaceType(convertToThunk(interfaceConfig)).toConfig(),
).to.deep.equal(expectedInterfaceConfig);
});

it('GraphQLUnionType have correct configs', () => {
expect(
new GraphQLUnionType(convertToThunk(unionConfig)).toConfig(),
).to.deep.equal(expectedUnionConfig);
});

it('GraphQLEnumType have correct configs', () => {
expect(
new GraphQLEnumType(convertToThunk(enumConfig)).toConfig(),
).to.deep.equal(expectedEnumConfig);
});

it('GraphQLInputObjectType have correct configs', () => {
expect(
new GraphQLUnionType(convertToThunk(inputConfig)).toConfig(),
).to.deep.equal(expectedInputConfig);
});
});
});