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 2 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
244 changes: 244 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,246 @@ describe('Type System: NonNull must only accept non-nullable types', () => {
});
}
});

describe('Type System: Configs', () => {
function fields() {
const input = {
field: {
type: GraphQLString,
deprecationReason: 'deprecated field',
args: {
id: {
defaultValue: 'default',
description: 'String argument',
type: GraphQLString,
},
},
},
};
return {
getActualField() {
return input;
},
getExpectedField() {
return {
field: {
...input.field,
args: {
id: {
...input.field.args.id,
astNode: undefined,
},
},
astNode: undefined,
resolve: undefined,
description: undefined,
subscribe: undefined,
},
};
},
};
}

function convertToThunk(config) {
return mapValue(config, (value, key) => {
if (key === 'interfaces') {
return () => value.interfaces;
}
if (key === 'fields') {
return () => value.fields;
}
return value;
});
}

function configHelpers(type) {
return function(objectConfig, expectedConfig) {
return {
getType() {
return new type(objectConfig);
},
getExpectedConfig() {
return expectedConfig;
},
};
};
}

function configTests(type, config, expectedConfig) {
const helperMethods = configHelpers(type);
return {
...helperMethods(config, expectedConfig),
thunk: helperMethods(convertToThunk(config), expectedConfig),
};
}

function objectType() {
const { getActualField, getExpectedField } = fields();
const objectConfig = {
name: 'Object',
fields: getActualField(),
description: 'Some object type',
isTypeOf: () => true,
interfaces: [interfaceType().getType()],
astNode: null,
extensionASTNodes: [],
};
const expected = {
...objectConfig,
fields: getExpectedField(),
};
return configTests(GraphQLObjectType, objectConfig, expected);
}

function scalarType() {
const config = {
name: 'Scalar',
serialize() {},
parseValue() {},
parseLiteral() {},
};
return configTests(GraphQLScalarType, config, {
...config,
astNode: undefined,
description: undefined,
extensionASTNodes: [],
});
}

function interfaceType() {
const config = {
name: 'Interface',
fields: {},
};
return configTests(GraphQLInterfaceType, config, {
...config,
astNode: undefined,
description: undefined,
resolveType: undefined,
extensionASTNodes: [],
});
}

function unionType() {
const config = {
name: 'Union',
types: [objectType().getType()],
};
return configTests(GraphQLUnionType, config, {
...config,
astNode: undefined,
description: undefined,
resolveType: undefined,
extensionASTNodes: [],
});
}

function enumType() {
const config = { name: 'Enum', values: { foo: {} } };
return configTests(GraphQLEnumType, config, {
...config,
values: {
foo: {
astNode: undefined,
description: undefined,
deprecationReason: undefined,
value: 'foo',
},
},
astNode: undefined,
description: undefined,
extensionASTNodes: [],
});
}

function inputType() {
const config = {
name: 'InputObject',
fields: {},
};
return configTests(GraphQLUnionType, config, {
name: 'InputObject',
types: [],
resolveType: undefined,
astNode: undefined,
description: undefined,
extensionASTNodes: [],
});
}

describe('Configs values', () => {
it('GraphQLObjectType have correct configs', () => {
const { getType, getExpectedConfig } = objectType();

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLScalarType have correct configs', () => {
const { getType, getExpectedConfig } = scalarType();

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLInterfaceType have correct configs', () => {
const { getType, getExpectedConfig } = interfaceType();

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLUnionType have correct configs', () => {
const { getType, getExpectedConfig } = unionType();

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLEnumType have correct configs', () => {
const { getType, getExpectedConfig } = enumType();

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLInputObjectType have correct configs', () => {
const { getType, getExpectedConfig } = inputType();

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});
});

describe('Configs values with thunk', () => {
it('GraphQLObjectType have correct configs', () => {
const { getType, getExpectedConfig } = objectType();

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLScalarType have correct configs', () => {
const { getType, getExpectedConfig } = scalarType().thunk;

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLInterfaceType have correct configs', () => {
const { getType, getExpectedConfig } = interfaceType().thunk;

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLUnionType have correct configs', () => {
const { getType, getExpectedConfig } = unionType().thunk;

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLEnumType have correct configs', () => {
const { getType, getExpectedConfig } = enumType().thunk;

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});

it('GraphQLInputObjectType have correct configs', () => {
const { getType, getExpectedConfig } = inputType().thunk;

expect(getType().toConfig()).to.deep.equal(getExpectedConfig());
});
});
});