-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
inputTypeComposer-test.js
269 lines (237 loc) · 8.65 KB
/
inputTypeComposer-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { expect } from 'chai';
import {
GraphQLInputObjectType,
GraphQLString,
GraphQLList,
GraphQLNonNull,
GraphQLInt,
GraphQLBoolean,
} from 'graphql';
import InputTypeComposer from '../inputTypeComposer';
describe('InputTypeComposer', () => {
let objectType;
beforeEach(() => {
objectType = new GraphQLInputObjectType({
name: 'InputType',
description: 'Mock type',
fields: {
input1: { type: GraphQLString },
input2: { type: GraphQLString },
},
});
});
describe('field manipulation methods', () => {
it('getFields()', () => {
const tc = new InputTypeComposer(objectType);
const fieldNames = Object.keys(tc.getFields());
expect(fieldNames).to.have.members(['input1', 'input2']);
});
it('getFieldNames()', () => {
const tc = new InputTypeComposer(objectType);
expect(tc.getFieldNames()).to.have.members(['input1', 'input2']);
});
it('hasField()', () => {
const tc = new InputTypeComposer(objectType);
expect(tc.hasField('input1')).to.equal(true);
expect(tc.hasField('missing')).to.equal(false);
});
it('setField()', () => {
const tc = new InputTypeComposer(objectType);
tc.setField('input3', { type: GraphQLString });
const fieldNames = Object.keys(objectType.getFields());
expect(fieldNames).to.include('input3');
});
describe('setFields()', () => {
it('accept regular fields definition', () => {
const tc = new InputTypeComposer(objectType);
tc.setFields({
input3: { type: GraphQLString },
input4: { type: GraphQLString },
});
expect(tc.getFieldNames()).to.not.have.members(['input1', 'input2']);
expect(tc.getFieldNames()).to.have.members(['input3', 'input4']);
expect(tc.getFieldType('input3')).to.equal(GraphQLString);
expect(tc.getFieldType('input4')).to.equal(GraphQLString);
});
it('accept shortand fields definition', () => {
const tc = new InputTypeComposer(objectType);
tc.setFields({
input3: GraphQLString,
input4: 'String',
});
expect(tc.getFieldType('input3')).to.equal(GraphQLString);
expect(tc.getFieldType('input4')).to.equal(GraphQLString);
});
it('accept types as function', () => {
const tc = new InputTypeComposer(objectType);
const typeAsFn = () => GraphQLString;
tc.setFields({
input3: { type: typeAsFn },
});
expect(tc.getFieldType('input3')).to.equal(typeAsFn);
// show provide unwrapped/unhoisted type for graphql
expect(tc.getType()._typeConfig.fields().input3.type)
.to.equal(GraphQLString);
});
});
it('addFields()', () => {
const tc = new InputTypeComposer(objectType);
tc.addFields({
input3: { type: GraphQLString },
input4: { type: GraphQLString },
});
expect(tc.getFieldNames())
.to.have.members(['input1', 'input2', 'input3', 'input4']);
});
it('removeField()', () => {
const tc = new InputTypeComposer(objectType);
tc.removeField('input1');
expect(tc.getFieldNames()).to.not.include('input1');
expect(tc.getFieldNames()).to.include('input2');
tc.removeField(['input2', 'input3']);
expect(tc.getFieldNames()).to.not.include('input2');
});
it('should extend field by name', () => {
const tc = new InputTypeComposer(objectType);
tc.setField('input3', {
type: GraphQLString,
});
tc.extendField('input3', {
description: 'this is input #3',
});
expect(tc.getField('input3')).property('type').to.be.equal(GraphQLString);
expect(tc.getField('input3')).property('description').to.equal('this is input #3');
tc.extendField('input3', {
type: 'Int',
});
expect(tc.getField('input3')).property('type').to.be.equal(GraphQLInt);
});
it('getFieldType()', () => {
const tc = new InputTypeComposer(objectType);
expect(tc.getFieldType('input1')).to.equal(GraphQLString);
});
it('isRequired()', () => {
const tc = new InputTypeComposer(objectType);
expect(tc.isRequired('input1')).to.equal(false);
});
it('makeRequired()', () => {
const tc = new InputTypeComposer(objectType);
tc.makeRequired('input1');
expect(tc.getField('input1').type).instanceof(GraphQLNonNull);
expect(tc.getField('input1').type.ofType).to.equal(GraphQLString);
expect(tc.isRequired('input1')).to.equal(true);
});
it('makeOptional()', () => {
const tc = new InputTypeComposer(objectType);
tc.makeRequired('input1');
expect(tc.isRequired('input1')).to.equal(true);
tc.makeOptional('input1');
expect(tc.isRequired('input1')).to.equal(false);
});
it('should add fields with converting types from string to object', () => {
const tc = new InputTypeComposer(objectType);
tc.setField('input3', { type: 'String' });
tc.addFields({
input4: { type: '[Int]' },
input5: { type: 'Boolean!' },
});
expect(tc.getField('input3').type).to.equal(GraphQLString);
expect(tc.getField('input4').type).instanceof(GraphQLList);
expect(tc.getField('input4').type.ofType).to.equal(GraphQLInt);
expect(tc.getField('input5').type).instanceof(GraphQLNonNull);
expect(tc.getField('input5').type.ofType).to.equal(GraphQLBoolean);
});
});
describe('type manipulation methods', () => {
it('getType()', () => {
const tc = new InputTypeComposer(objectType);
expect(tc.getType()).instanceof(GraphQLInputObjectType);
expect(tc.getType().name).to.equal('InputType');
});
it('getTypeAsRequired()', () => {
const tc = new InputTypeComposer(objectType);
expect(tc.getTypeAsRequired()).instanceof(GraphQLNonNull);
expect(tc.getTypeAsRequired().ofType.name).to.equal('InputType');
});
it('getTypeName()', () => {
const tc = new InputTypeComposer(objectType);
expect(tc.getTypeName()).to.equal('InputType');
});
it('setTypeName()', () => {
const tc = new InputTypeComposer(objectType);
tc.setTypeName('OtherInputType');
expect(tc.getTypeName()).to.equal('OtherInputType');
});
it('getDescription()', () => {
const tc = new InputTypeComposer(objectType);
expect(tc.getDescription()).to.equal('Mock type');
});
it('setDescription()', () => {
const tc = new InputTypeComposer(objectType);
tc.setDescription('Changed description');
expect(tc.getDescription()).to.equal('Changed description');
});
});
describe('static method create()', () => {
it('should create ITC by typeName as a string', () => {
const TC = InputTypeComposer.create('TypeStub');
expect(TC).instanceof(InputTypeComposer);
expect(TC.getType()).instanceof(GraphQLInputObjectType);
expect(TC.getFields()).deep.equal({});
});
it('should create ITC by type template string', () => {
const TC = InputTypeComposer.create(`
input TestTypeTplInput {
f1: String
# Description for some required Int field
f2: Int!
}
`);
expect(TC).instanceof(InputTypeComposer);
expect(TC.getTypeName()).equal('TestTypeTplInput');
expect(TC.getFieldType('f1')).equal(GraphQLString);
expect(TC.getFieldType('f2')).instanceof(GraphQLNonNull);
expect(TC.getFieldType('f2').ofType).equal(GraphQLInt);
});
it('should create ITC by GraphQLObjectTypeConfig', () => {
const TC = InputTypeComposer.create({
name: 'TestTypeInput',
fields: {
f1: {
type: 'String',
},
f2: 'Int!',
},
});
expect(TC).instanceof(InputTypeComposer);
expect(TC.getFieldType('f1')).equal(GraphQLString);
expect(TC.getFieldType('f2')).instanceof(GraphQLNonNull);
expect(TC.getFieldType('f2').ofType).equal(GraphQLInt);
});
it('should create ITC by GraphQLInputObjectType', () => {
const objType = new GraphQLInputObjectType({
name: 'TestTypeObj',
fields: {
f1: {
type: GraphQLString,
},
},
});
const TC = InputTypeComposer.create(objType);
expect(TC).instanceof(InputTypeComposer);
expect(TC.getType()).equal(objType);
expect(TC.getFieldType('f1')).equal(GraphQLString);
});
});
it('get() should return type by path', () => {
const tc = new InputTypeComposer(new GraphQLInputObjectType({
name: 'Writable',
fields: {
field1: {
type: GraphQLString,
},
},
}));
expect(tc.get('field1')).equal(GraphQLString);
});
});