Skip to content

Commit 4e0a86e

Browse files
committed
add unit test for nonNull()
1 parent 7472377 commit 4e0a86e

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/node/__tests__/plural.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ import {
1515
GraphQLObjectType,
1616
GraphQLSchema,
1717
GraphQLString,
18+
GraphQLInputObjectType,
19+
GraphQLScalarType,
20+
GraphQLEnumType,
21+
GraphQLList,
22+
GraphQLNonNull,
1823
graphql
1924
} from 'graphql';
2025

2126
import {
22-
pluralIdentifyingRootField
27+
pluralIdentifyingRootField,
28+
nonNull
2329
} from '../plural';
2430

2531
var userType = new GraphQLObjectType({
@@ -42,6 +48,7 @@ var queryType = new GraphQLObjectType({
4248
description: 'Map from a username to the user',
4349
inputType: GraphQLString,
4450
outputType: userType,
51+
// $FlowFixMe : rootValue Graphql(mixed) -> relay(object)
4552
resolveSingleInput: (username, context, {rootValue: {lang}}) => ({
4653
username: username,
4754
url: 'www.facebook.com/' + username + '?lang=' + lang
@@ -157,3 +164,60 @@ describe('pluralIdentifyingRootField()', () => {
157164
return expect(graphql(schema, query)).to.become({data: expected});
158165
});
159166
});
167+
168+
describe('nonNull()', () => {
169+
function qlScalar() {
170+
return new GraphQLScalarType({
171+
name: 'scalar',
172+
serialize: String,
173+
description: 'test'
174+
});
175+
}
176+
177+
it('covert GraphQLInputObjectType to NonNull type', () => {
178+
const inputType = new GraphQLInputObjectType({
179+
name: 'input',
180+
fields: {
181+
test: {
182+
type: qlScalar()
183+
}
184+
}
185+
});
186+
const result = nonNull(inputType);
187+
expect(result).to.be.an.instanceof(GraphQLNonNull);
188+
expect(result.ofType).to.deep.equal(inputType);
189+
});
190+
191+
it('covert GraphQLScalarType to NonNull type', () => {
192+
const scalarType = qlScalar();
193+
const result = nonNull(scalarType);
194+
expect(result).to.be.an.instanceof(GraphQLNonNull);
195+
expect(result.ofType).to.deep.equal(scalarType);
196+
});
197+
198+
it('covert GraphQLEnumType to NonNull type', () => {
199+
const enumType = new GraphQLEnumType({
200+
name: 'EM',
201+
values: {
202+
E: { value: 0},
203+
M: { value: 1}
204+
}
205+
});
206+
const result = nonNull(enumType);
207+
expect(result).to.be.an.instanceof(GraphQLNonNull);
208+
expect(result.ofType).to.deep.equal(enumType);
209+
});
210+
211+
it('covert GraphQLList to NonNull type', () => {
212+
const listType = new GraphQLList(GraphQLString);
213+
const result = nonNull(listType);
214+
expect(result).to.be.an.instanceof(GraphQLNonNull);
215+
expect(result.ofType).to.deep.equal(listType);
216+
});
217+
218+
it('does nothing to GraphQLNonNull Type', () => {
219+
const nonNullType = new GraphQLNonNull(GraphQLString);
220+
const result = nonNull(nonNullType);
221+
expect(result).to.deep.equal(nonNullType);
222+
});
223+
});

src/node/plural.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type NonNullInputType = GraphQLNonNull<
6464
GraphQLInputObjectType |
6565
GraphQLList<GraphQLInputType> >;
6666

67-
function nonNull(type:GraphQLInputType):NonNullInputType {
67+
export function nonNull(type:GraphQLInputType):NonNullInputType {
6868
if ( type instanceof GraphQLNonNull) {
6969
return type;
7070
} else {

0 commit comments

Comments
 (0)