1
1
import { faker } from '@faker-js/faker' ;
2
2
import { isDefined } from 'class-validator' ;
3
- import { FieldMetadataComplexOption , FieldMetadataDefaultOption } from 'src/engine/metadata-modules/field-metadata/dtos/options.input' ;
3
+ import {
4
+ FieldMetadataComplexOption ,
5
+ FieldMetadataDefaultOption ,
6
+ } from 'src/engine/metadata-modules/field-metadata/dtos/options.input' ;
4
7
import { createOneOperation } from 'test/integration/graphql/utils/create-one-operation.util' ;
5
8
import { findOneOperation } from 'test/integration/graphql/utils/find-one-operation.util' ;
6
9
import { createOneFieldMetadata } from 'test/integration/metadata/suites/field-metadata/utils/create-one-field-metadata.util' ;
@@ -26,12 +29,18 @@ const generateOption = (index: number): Option => ({
26
29
color : 'green' ,
27
30
position : index ,
28
31
} ) ;
29
- const generateOptions = ( length : number ) =>
30
- Array . from ( { length } , ( _value , index ) => generateOption ( index ) ) ;
32
+ const generateOptionWithId = ( index : number ) : Option & { id : string } => ( {
33
+ ...generateOption ( index ) ,
34
+ id : faker . string . uuid ( ) ,
35
+ } ) ;
36
+ const generateOptions = ( length : number , withId : boolean = false ) =>
37
+ Array . from ( { length } , ( _value , index ) =>
38
+ withId ? generateOptionWithId ( index ) : generateOption ( index ) ,
39
+ ) ;
31
40
const updateOption = ( { value, label, ...option } : Option ) => ( {
32
41
...option ,
33
- value : `${ value } _ ${ faker . lorem . word ( ) . toLocaleUpperCase ( ) } ` ,
34
- label : `${ label } ${ faker . lorem . word ( ) } ` ,
42
+ value : `${ value } _UPDATED ` ,
43
+ label : `${ label } updated ` ,
35
44
} ) ;
36
45
37
46
const ALL_OPTIONS = generateOptions ( 10 ) ;
@@ -105,41 +114,49 @@ describe('updateOne', () => {
105
114
}
106
115
} ) ;
107
116
117
+ type ViewFilterUpdate = {
118
+ displayValue : string ;
119
+ value : string ;
120
+ } ;
108
121
const testCases : EachTestingContext < {
109
- initial : Option [ ] ;
110
- updateOptions : ( options : FieldMetadataDefaultOption [ ] | FieldMetadataComplexOption [ ] ) => Option [ ] ;
122
+ fieldMetadataOptions : Option [ ] ;
123
+ createViewFilter : ViewFilterUpdate ;
124
+ updateOptions : (
125
+ options : FieldMetadataDefaultOption [ ] | FieldMetadataComplexOption [ ] ,
126
+ ) => Option [ ] ;
111
127
expected ?: null ;
112
128
} > [ ] = [
113
129
// {
114
130
// title:
115
131
// 'should delete related view filter if all select field options got deleted',
116
132
// context: {
117
133
// initial: ALL_OPTIONS,
118
- // updated: generateOptions(3),
134
+ // updateOptions: () => {
135
+ // const withId = true;
136
+ // return generateOptions(3, withId);
137
+ // },
119
138
// expected: null,
120
139
// },
121
140
// },
122
141
// {
123
142
// title: 'should update related view filter label',
124
143
// context: {
125
144
// initial: ALL_OPTIONS,
126
- // updated: ALL_OPTIONS.map((option, index) =>
127
- // isEven(option, index) ? updateOption(option) : option,
128
- // ),
129
- // },
130
- // },
131
- // {
132
- // title: 'should update related view filter with added options',
133
- // context: {
134
- // initial: ALL_OPTIONS.slice(0, 2),
135
- // updated: ALL_OPTIONS,
145
+ // updateOptions: (options) =>
146
+ // options.map((option, index) =>
147
+ // isEven(option, index) ? updateOption(option) : option,
148
+ // ),
136
149
// },
137
150
// },
138
151
{
139
- title : 'should update related view filter updated option' ,
152
+ title : 'should update related view filter with updated option' ,
140
153
context : {
141
- initial : ALL_OPTIONS ,
142
- updateOptions : ( options : Option [ ] ) =>
154
+ fieldMetadataOptions : ALL_OPTIONS ,
155
+ createViewFilter : {
156
+ displayValue : `${ ALL_OPTIONS . length } options` ,
157
+ value : ALL_OPTIONS . map ( ( option ) => option . value ) . join ( ', ' ) ,
158
+ } ,
159
+ updateOptions : ( options ) =>
143
160
options . map ( ( option , index ) =>
144
161
index === 5 ? updateOption ( option ) : option ,
145
162
) ,
@@ -149,9 +166,16 @@ describe('updateOne', () => {
149
166
150
167
test . each ( testCases ) (
151
168
'$title' ,
152
- async ( { context : { expected, initial, updateOptions } } ) => {
169
+ async ( {
170
+ context : {
171
+ expected,
172
+ createViewFilter,
173
+ fieldMetadataOptions,
174
+ updateOptions,
175
+ } ,
176
+ } ) => {
153
177
const { createOneField, createOneView } =
154
- await createObjectSelectFieldAndView ( ALL_OPTIONS ) ;
178
+ await createObjectSelectFieldAndView ( fieldMetadataOptions ) ;
155
179
const {
156
180
data : { createOneResponse : createOneViewFilter } ,
157
181
} = await createOneOperation < {
@@ -168,8 +192,8 @@ describe('updateOne', () => {
168
192
viewId : createOneView . id ,
169
193
fieldMetadataId : createOneField . id ,
170
194
operand : 'is' ,
171
- value : JSON . stringify ( initial . map ( ( { label } ) => label ) ) ,
172
- displayValue : ` ${ initial . length } options` ,
195
+ value : createViewFilter . value ,
196
+ displayValue : createViewFilter . displayValue ,
173
197
} ,
174
198
} ) ;
175
199
@@ -203,15 +227,14 @@ describe('updateOne', () => {
203
227
} ,
204
228
} ) ;
205
229
206
- expect ( errors ) . toMatchSnapshot ( ) ;
207
-
208
230
if ( expected !== undefined ) {
231
+ expect ( errors ) . toMatchSnapshot ( ) ;
209
232
expect ( findResponse ) . toBe ( expected ) ;
210
233
return ;
211
234
}
212
235
213
236
expect ( findResponse ) . toMatchSnapshot ( {
214
- id : createOneViewFilter . id ,
237
+ id : expect . any ( String ) ,
215
238
} ) ;
216
239
} ,
217
240
) ;
0 commit comments