@@ -13,12 +13,13 @@ import {
13
13
14
14
import {
15
15
convertToSourceTC ,
16
- propertyToGraphQLType ,
16
+ propertyToSourceGraphQLType ,
17
17
convertToAggregatableITC ,
18
18
inputPropertiesToGraphQLTypes ,
19
19
convertToSearchableITC ,
20
20
convertToAnalyzedITC ,
21
- } from '../PropertiesConverter' ;
21
+ getSubFields ,
22
+ } from '../mappingConverter' ;
22
23
23
24
const mapping = {
24
25
properties : {
@@ -84,54 +85,69 @@ describe('PropertiesConverter', () => {
84
85
} ) ;
85
86
86
87
it ( 'should make singular and plural fields' , ( ) => {
87
- const tc = convertToSourceTC ( mapping , 'TestMapping' ) ;
88
- const singular : any = tc . getField ( 'name' ) ;
88
+ const tc1 = convertToSourceTC ( mapping , 'TestMapping' ) ;
89
+ const singular : any = tc1 . getField ( 'name' ) ;
89
90
expect ( singular . type ) . toBe ( GraphQLString ) ;
90
91
91
- const plural : any = tc . getField ( 'nameA' ) ;
92
+ const tc2 = convertToSourceTC ( mapping , 'TestMapping' , {
93
+ pluralFields : [ 'name' ] ,
94
+ } ) ;
95
+ const plural : any = tc2 . getField ( 'name' ) ;
92
96
expect ( plural . type ) . toBeInstanceOf ( GraphQLList ) ;
93
97
expect ( plural . type . ofType ) . toBe ( GraphQLString ) ;
94
98
} ) ;
95
99
} ) ;
96
100
97
- describe ( 'propertyToGraphQLType ()' , ( ) => {
101
+ describe ( 'propertyToSourceGraphQLType ()' , ( ) => {
98
102
it ( 'should throw error on wrong property config' , ( ) => {
99
103
expect ( ( ) => {
100
104
// $FlowFixMe
101
- propertyToGraphQLType ( ) ;
105
+ propertyToSourceGraphQLType ( ) ;
102
106
} ) . toThrowError ( 'incorrect Elastic property config' ) ;
103
107
expect ( ( ) => {
104
- propertyToGraphQLType ( { } ) ;
108
+ propertyToSourceGraphQLType ( { } ) ;
105
109
} ) . toThrowError ( 'incorrect Elastic property config' ) ;
106
110
} ) ;
107
111
108
112
it ( 'should return GraphQLJSON as fallback for unknown Elastic type' , ( ) => {
109
- expect ( propertyToGraphQLType ( { type : 'strange' } ) ) . toEqual ( GraphQLJSON ) ;
113
+ expect ( propertyToSourceGraphQLType ( { type : 'strange' } ) ) . toEqual (
114
+ GraphQLJSON
115
+ ) ;
110
116
} ) ;
111
117
112
118
it ( 'should return GraphQLInt for int types' , ( ) => {
113
- expect ( propertyToGraphQLType ( { type : 'integer' } ) ) . toEqual ( GraphQLInt ) ;
114
- expect ( propertyToGraphQLType ( { type : 'long' } ) ) . toEqual ( GraphQLInt ) ;
119
+ expect ( propertyToSourceGraphQLType ( { type : 'integer' } ) ) . toEqual (
120
+ GraphQLInt
121
+ ) ;
122
+ expect ( propertyToSourceGraphQLType ( { type : 'long' } ) ) . toEqual ( GraphQLInt ) ;
115
123
} ) ;
116
124
117
125
it ( 'should return GraphQLString for string types' , ( ) => {
118
- expect ( propertyToGraphQLType ( { type : 'text' } ) ) . toEqual ( GraphQLString ) ;
119
- expect ( propertyToGraphQLType ( { type : 'keyword' } ) ) . toEqual ( GraphQLString ) ;
126
+ expect ( propertyToSourceGraphQLType ( { type : 'text' } ) ) . toEqual (
127
+ GraphQLString
128
+ ) ;
129
+ expect ( propertyToSourceGraphQLType ( { type : 'keyword' } ) ) . toEqual (
130
+ GraphQLString
131
+ ) ;
120
132
} ) ;
121
133
122
134
it ( 'should return GraphQLFloat for float types' , ( ) => {
123
- expect ( propertyToGraphQLType ( { type : 'float' } ) ) . toEqual ( GraphQLFloat ) ;
124
- expect ( propertyToGraphQLType ( { type : 'double' } ) ) . toEqual ( GraphQLFloat ) ;
135
+ expect ( propertyToSourceGraphQLType ( { type : 'float' } ) ) . toEqual (
136
+ GraphQLFloat
137
+ ) ;
138
+ expect ( propertyToSourceGraphQLType ( { type : 'double' } ) ) . toEqual (
139
+ GraphQLFloat
140
+ ) ;
125
141
} ) ;
126
142
127
143
it ( 'should return GraphQLBoolean for float types' , ( ) => {
128
- expect ( propertyToGraphQLType ( { type : 'boolean' } ) ) . toEqual (
144
+ expect ( propertyToSourceGraphQLType ( { type : 'boolean' } ) ) . toEqual (
129
145
GraphQLBoolean
130
146
) ;
131
147
} ) ;
132
148
133
149
it ( 'should return GraphQLObjectType for object with subfields' , ( ) => {
134
- const type = propertyToGraphQLType (
150
+ const type = propertyToSourceGraphQLType (
135
151
{
136
152
properties : {
137
153
big : {
@@ -169,12 +185,13 @@ describe('PropertiesConverter', () => {
169
185
( ) => true ,
170
186
'lastname'
171
187
) ;
172
- expect ( fields . lastname ) . toEqual ( GraphQLString ) ;
188
+ expect ( fields . _all . lastname ) . toEqual ( GraphQLString ) ;
189
+ expect ( fields . text . lastname ) . toEqual ( GraphQLString ) ;
173
190
} ) ;
174
191
175
192
it ( 'should accept mapping' , ( ) => {
176
193
const fields = inputPropertiesToGraphQLTypes ( mapping , ( ) => true ) ;
177
- expect ( Object . keys ( fields ) . length ) . toBeGreaterThan ( 2 ) ;
194
+ expect ( Object . keys ( fields . _all ) . length ) . toBeGreaterThan ( 2 ) ;
178
195
} ) ;
179
196
180
197
it ( 'should convert nested fields' , ( ) => {
@@ -194,10 +211,16 @@ describe('PropertiesConverter', () => {
194
211
} ,
195
212
( ) => true
196
213
) ;
197
- expect ( Object . keys ( fields ) . length ) . toEqual ( 2 ) ;
198
- expect ( Object . keys ( fields ) ) . toEqual (
214
+ expect ( Object . keys ( fields . _all ) . length ) . toEqual ( 2 ) ;
215
+ expect ( Object . keys ( fields . _all ) ) . toEqual (
199
216
expect . arrayContaining ( [ 'name' , 'name__keyword' ] )
200
217
) ;
218
+ expect ( Object . keys ( fields . keyword ) ) . toEqual (
219
+ expect . arrayContaining ( [ 'name__keyword' ] )
220
+ ) ;
221
+ expect ( Object . keys ( fields . text ) ) . toEqual (
222
+ expect . arrayContaining ( [ 'name' ] )
223
+ ) ;
201
224
} ) ;
202
225
203
226
it ( 'should use filterFn' , ( ) => {
@@ -217,14 +240,18 @@ describe('PropertiesConverter', () => {
217
240
} ,
218
241
prop => prop . type !== 'text'
219
242
) ;
220
- expect ( Object . keys ( fields ) . length ) . toEqual ( 1 ) ;
221
- expect ( Object . keys ( fields ) ) . toEqual (
243
+ expect ( Object . keys ( fields . _all ) . length ) . toEqual ( 1 ) ;
244
+ expect ( Object . keys ( fields . _all ) ) . toEqual (
245
+ expect . arrayContaining ( [ 'name__keyword' ] )
246
+ ) ;
247
+ expect ( Object . keys ( fields . keyword ) . length ) . toEqual ( 1 ) ;
248
+ expect ( Object . keys ( fields . keyword ) ) . toEqual (
222
249
expect . arrayContaining ( [ 'name__keyword' ] )
223
250
) ;
224
251
} ) ;
225
252
226
253
it ( 'should not return index:false fields' , ( ) => {
227
- const itc = convertToSearchableITC ( mapping , 'SerachInput ' ) ;
254
+ const itc = convertToSearchableITC ( mapping , 'SearchInput ' ) ;
228
255
expect ( itc . getFieldNames ( ) ) . not . toEqual (
229
256
expect . arrayContaining ( [ 'noIndex' ] )
230
257
) ;
@@ -243,8 +270,12 @@ describe('PropertiesConverter', () => {
243
270
} ,
244
271
prop => prop . type !== 'text'
245
272
) ;
246
- expect ( Object . keys ( fields ) . length ) . toEqual ( 1 ) ;
247
- expect ( Object . keys ( fields ) ) . toEqual (
273
+ expect ( Object . keys ( fields . _all ) . length ) . toEqual ( 1 ) ;
274
+ expect ( Object . keys ( fields . _all ) ) . toEqual (
275
+ expect . arrayContaining ( [ 'date' ] )
276
+ ) ;
277
+ expect ( Object . keys ( fields . date ) . length ) . toEqual ( 1 ) ;
278
+ expect ( Object . keys ( fields . date ) ) . toEqual (
248
279
expect . arrayContaining ( [ 'date' ] )
249
280
) ;
250
281
} ) ;
@@ -305,14 +336,14 @@ describe('PropertiesConverter', () => {
305
336
} ) ;
306
337
307
338
it ( 'should return InputTypeComposer' , ( ) => {
308
- const itc = convertToSearchableITC ( mapping , 'SerachInput ' ) ;
339
+ const itc = convertToSearchableITC ( mapping , 'SearchInput ' ) ;
309
340
expect ( itc ) . toBeInstanceOf ( InputTypeComposer ) ;
310
- expect ( itc . getTypeName ( ) ) . toBe ( 'SerachInput ' ) ;
341
+ expect ( itc . getTypeName ( ) ) . toBe ( 'SearchInput ' ) ;
311
342
expect ( itc . getFieldNames ( ) . length ) . toBeGreaterThan ( 1 ) ;
312
343
} ) ;
313
344
314
345
it ( 'should return array of searchable fields' , ( ) => {
315
- const itc = convertToSearchableITC ( mapping , 'SerachInput ' ) ;
346
+ const itc = convertToSearchableITC ( mapping , 'SearchInput ' ) ;
316
347
expect ( itc . getFieldNames ( ) ) . toEqual (
317
348
expect . arrayContaining ( [
318
349
'name__keyword' ,
@@ -327,7 +358,6 @@ describe('PropertiesConverter', () => {
327
358
} ) ;
328
359
} ) ;
329
360
330
-
331
361
describe ( 'convertToAnalyzedITC()' , ( ) => {
332
362
it ( 'should throw error on empty mapping' , ( ) => {
333
363
// $FlowFixMe
@@ -360,4 +390,17 @@ describe('PropertiesConverter', () => {
360
390
) ;
361
391
} ) ;
362
392
} ) ;
393
+
394
+ describe ( 'getSubFields()' , ( ) => {
395
+ it ( 'should return array of sub fields' , ( ) => {
396
+ expect (
397
+ getSubFields ( 'range' , [ 'ok' , 'range' , 'range.min' , 'range.max' ] )
398
+ ) . toEqual ( [ 'min' , 'max' ] ) ;
399
+ } ) ;
400
+
401
+ it ( 'should return array for empty/undefined list' , ( ) => {
402
+ expect ( getSubFields ( 'range' , null ) ) . toEqual ( [ ] ) ;
403
+ expect ( getSubFields ( 'range' ) ) . toEqual ( [ ] ) ;
404
+ } ) ;
405
+ } ) ;
363
406
} ) ;
0 commit comments