@@ -13,12 +13,13 @@ import {
1313
1414import {
1515 convertToSourceTC ,
16- propertyToGraphQLType ,
16+ propertyToSourceGraphQLType ,
1717 convertToAggregatableITC ,
1818 inputPropertiesToGraphQLTypes ,
1919 convertToSearchableITC ,
2020 convertToAnalyzedITC ,
21- } from '../PropertiesConverter' ;
21+ getSubFields ,
22+ } from '../mappingConverter' ;
2223
2324const mapping = {
2425 properties : {
@@ -84,54 +85,69 @@ describe('PropertiesConverter', () => {
8485 } ) ;
8586
8687 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' ) ;
8990 expect ( singular . type ) . toBe ( GraphQLString ) ;
9091
91- const plural : any = tc . getField ( 'nameA' ) ;
92+ const tc2 = convertToSourceTC ( mapping , 'TestMapping' , {
93+ pluralFields : [ 'name' ] ,
94+ } ) ;
95+ const plural : any = tc2 . getField ( 'name' ) ;
9296 expect ( plural . type ) . toBeInstanceOf ( GraphQLList ) ;
9397 expect ( plural . type . ofType ) . toBe ( GraphQLString ) ;
9498 } ) ;
9599 } ) ;
96100
97- describe ( 'propertyToGraphQLType ()' , ( ) => {
101+ describe ( 'propertyToSourceGraphQLType ()' , ( ) => {
98102 it ( 'should throw error on wrong property config' , ( ) => {
99103 expect ( ( ) => {
100104 // $FlowFixMe
101- propertyToGraphQLType ( ) ;
105+ propertyToSourceGraphQLType ( ) ;
102106 } ) . toThrowError ( 'incorrect Elastic property config' ) ;
103107 expect ( ( ) => {
104- propertyToGraphQLType ( { } ) ;
108+ propertyToSourceGraphQLType ( { } ) ;
105109 } ) . toThrowError ( 'incorrect Elastic property config' ) ;
106110 } ) ;
107111
108112 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+ ) ;
110116 } ) ;
111117
112118 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 ) ;
115123 } ) ;
116124
117125 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+ ) ;
120132 } ) ;
121133
122134 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+ ) ;
125141 } ) ;
126142
127143 it ( 'should return GraphQLBoolean for float types' , ( ) => {
128- expect ( propertyToGraphQLType ( { type : 'boolean' } ) ) . toEqual (
144+ expect ( propertyToSourceGraphQLType ( { type : 'boolean' } ) ) . toEqual (
129145 GraphQLBoolean
130146 ) ;
131147 } ) ;
132148
133149 it ( 'should return GraphQLObjectType for object with subfields' , ( ) => {
134- const type = propertyToGraphQLType (
150+ const type = propertyToSourceGraphQLType (
135151 {
136152 properties : {
137153 big : {
@@ -169,12 +185,13 @@ describe('PropertiesConverter', () => {
169185 ( ) => true ,
170186 'lastname'
171187 ) ;
172- expect ( fields . lastname ) . toEqual ( GraphQLString ) ;
188+ expect ( fields . _all . lastname ) . toEqual ( GraphQLString ) ;
189+ expect ( fields . text . lastname ) . toEqual ( GraphQLString ) ;
173190 } ) ;
174191
175192 it ( 'should accept mapping' , ( ) => {
176193 const fields = inputPropertiesToGraphQLTypes ( mapping , ( ) => true ) ;
177- expect ( Object . keys ( fields ) . length ) . toBeGreaterThan ( 2 ) ;
194+ expect ( Object . keys ( fields . _all ) . length ) . toBeGreaterThan ( 2 ) ;
178195 } ) ;
179196
180197 it ( 'should convert nested fields' , ( ) => {
@@ -194,10 +211,16 @@ describe('PropertiesConverter', () => {
194211 } ,
195212 ( ) => true
196213 ) ;
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 (
199216 expect . arrayContaining ( [ 'name' , 'name__keyword' ] )
200217 ) ;
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+ ) ;
201224 } ) ;
202225
203226 it ( 'should use filterFn' , ( ) => {
@@ -217,14 +240,18 @@ describe('PropertiesConverter', () => {
217240 } ,
218241 prop => prop . type !== 'text'
219242 ) ;
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 (
222249 expect . arrayContaining ( [ 'name__keyword' ] )
223250 ) ;
224251 } ) ;
225252
226253 it ( 'should not return index:false fields' , ( ) => {
227- const itc = convertToSearchableITC ( mapping , 'SerachInput ' ) ;
254+ const itc = convertToSearchableITC ( mapping , 'SearchInput ' ) ;
228255 expect ( itc . getFieldNames ( ) ) . not . toEqual (
229256 expect . arrayContaining ( [ 'noIndex' ] )
230257 ) ;
@@ -243,8 +270,12 @@ describe('PropertiesConverter', () => {
243270 } ,
244271 prop => prop . type !== 'text'
245272 ) ;
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 (
248279 expect . arrayContaining ( [ 'date' ] )
249280 ) ;
250281 } ) ;
@@ -305,14 +336,14 @@ describe('PropertiesConverter', () => {
305336 } ) ;
306337
307338 it ( 'should return InputTypeComposer' , ( ) => {
308- const itc = convertToSearchableITC ( mapping , 'SerachInput ' ) ;
339+ const itc = convertToSearchableITC ( mapping , 'SearchInput ' ) ;
309340 expect ( itc ) . toBeInstanceOf ( InputTypeComposer ) ;
310- expect ( itc . getTypeName ( ) ) . toBe ( 'SerachInput ' ) ;
341+ expect ( itc . getTypeName ( ) ) . toBe ( 'SearchInput ' ) ;
311342 expect ( itc . getFieldNames ( ) . length ) . toBeGreaterThan ( 1 ) ;
312343 } ) ;
313344
314345 it ( 'should return array of searchable fields' , ( ) => {
315- const itc = convertToSearchableITC ( mapping , 'SerachInput ' ) ;
346+ const itc = convertToSearchableITC ( mapping , 'SearchInput ' ) ;
316347 expect ( itc . getFieldNames ( ) ) . toEqual (
317348 expect . arrayContaining ( [
318349 'name__keyword' ,
@@ -327,7 +358,6 @@ describe('PropertiesConverter', () => {
327358 } ) ;
328359 } ) ;
329360
330-
331361 describe ( 'convertToAnalyzedITC()' , ( ) => {
332362 it ( 'should throw error on empty mapping' , ( ) => {
333363 // $FlowFixMe
@@ -360,4 +390,17 @@ describe('PropertiesConverter', () => {
360390 ) ;
361391 } ) ;
362392 } ) ;
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+ } ) ;
363406} ) ;
0 commit comments