@@ -13,28 +13,31 @@ const baseSettings = {
13
13
14
14
interface ICustom {
15
15
token : 'first' | 'last' | 'middle' ;
16
- indexToReplace ? : number ;
17
- indexesToReplace ? : number [ ] ;
16
+ indexesToReplace : number [ ] ; // indexes to remove
17
+ amount : number ; // how many items to insert
18
18
increase ?: boolean ;
19
19
}
20
20
21
21
const configList : TestBedConfig [ ] = [ {
22
22
datasourceSettings : { ...baseSettings } ,
23
23
custom : {
24
- indexToReplace : baseSettings . minIndex + 1 ,
25
- token : 'middle'
26
- }
24
+ indexesToReplace : [ baseSettings . minIndex + 1 ] ,
25
+ token : 'middle' ,
26
+ amount : 1
27
+ } as ICustom
27
28
} , {
28
29
datasourceSettings : { ...baseSettings } ,
29
30
custom : {
30
- indexToReplace : baseSettings . minIndex ,
31
- token : 'first'
31
+ indexesToReplace : [ baseSettings . minIndex ] ,
32
+ token : 'first' ,
33
+ amount : 1
32
34
} as ICustom
33
35
} , {
34
36
datasourceSettings : { ...baseSettings , startIndex : baseSettings . maxIndex } ,
35
37
custom : {
36
- indexToReplace : baseSettings . maxIndex ,
37
- token : 'last'
38
+ indexesToReplace : [ baseSettings . maxIndex ] ,
39
+ token : 'last' ,
40
+ amount : 1
38
41
} as ICustom
39
42
} ] . map ( config => ( {
40
43
...config ,
@@ -49,7 +52,8 @@ const manyToOneConfigList: TestBedConfig[] = [{
49
52
baseSettings . minIndex + 2 ,
50
53
baseSettings . minIndex + 3
51
54
] ,
52
- token : 'middle'
55
+ token : 'middle' ,
56
+ amount : 1
53
57
} as ICustom
54
58
} , {
55
59
datasourceSettings : { ...baseSettings } ,
@@ -59,7 +63,8 @@ const manyToOneConfigList: TestBedConfig[] = [{
59
63
baseSettings . minIndex + 1 ,
60
64
baseSettings . minIndex + 2
61
65
] ,
62
- token : 'first'
66
+ token : 'first' ,
67
+ amount : 1
63
68
} as ICustom
64
69
} , {
65
70
datasourceSettings : { ...baseSettings , startIndex : baseSettings . maxIndex } ,
@@ -69,7 +74,8 @@ const manyToOneConfigList: TestBedConfig[] = [{
69
74
baseSettings . maxIndex - 1 ,
70
75
baseSettings . maxIndex
71
76
] ,
72
- token : 'last'
77
+ token : 'last' ,
78
+ amount : 1
73
79
} as ICustom
74
80
} ] . map ( config => ( {
75
81
...config ,
@@ -81,38 +87,53 @@ const manyToOneIncreaseConfigList = manyToOneConfigList.map(config => ({
81
87
custom : {
82
88
...config . custom ,
83
89
increase : true
84
- }
90
+ } as ICustom
85
91
} ) ) ;
86
92
93
+ const manyToManyConfigList = [
94
+ ...manyToOneConfigList . map ( config => ( {
95
+ ...config , custom : { ...config . custom , amount : 2 } as ICustom
96
+ } ) ) ,
97
+ ...manyToOneConfigList . map ( config => ( {
98
+ ...config , custom : { ...config . custom , amount : 3 } as ICustom
99
+ } ) ) ,
100
+ ...manyToOneConfigList . map ( config => ( {
101
+ ...config , custom : { ...config . custom , amount : 4 } as ICustom
102
+ } ) ) ,
103
+ ] ;
104
+
87
105
const shouldReplace = ( config : TestBedConfig ) => ( misc : Misc ) => async ( done : Function ) => {
88
106
await misc . relaxNext ( ) ;
89
107
const { adapter } = misc ;
90
- const { indexToReplace : index , indexesToReplace : indexes , token, increase } = config . custom ;
108
+ const { indexesToReplace : indexes , amount , token, increase } = config . custom ;
91
109
const { datasourceSettings : { minIndex, itemSize } } = config ;
92
- const diff = indexes ? indexes . length : 1 ;
110
+ const diff = amount - indexes . length ; // inserted - removed
93
111
const viewportSize = misc . getScrollableSize ( ) ;
94
- const sizeToRemove = ( diff - 1 ) * misc . getItemSize ( ) ;
95
- const maxScrollPosition = misc . getMaxScrollPosition ( ) - sizeToRemove ;
96
- const newIndex = indexes ? indexes [ increase ? indexes . length - 1 : 0 ] : index ;
97
- const newMinIndex = increase ? minIndex + diff - 1 : minIndex ;
98
- const position = token === 'last' ? maxScrollPosition : ( newIndex - newMinIndex ) * itemSize ;
99
- const newItem = generateItem ( newIndex ) ;
100
- newItem . text += '*' ;
112
+ const sizeToChange = diff * misc . getItemSize ( ) ;
113
+ const maxScrollPosition = misc . getMaxScrollPosition ( ) + sizeToChange ;
114
+ const newIndexFirst = indexes [ increase ? indexes . length - 1 : 0 ] ;
115
+ const newIndexLast = newIndexFirst + amount - 1 ;
116
+ const newAbsMinIndex = increase ? minIndex - diff : minIndex ;
117
+ const position = token === 'last' ? maxScrollPosition : ( newIndexFirst - newAbsMinIndex ) * itemSize ;
118
+ const items = Array . from ( { length : amount } ) . map ( ( j , i ) => generateItem ( newIndexFirst + i , false , '*' ) ) ;
101
119
102
120
// replace at the Datasource level (component)
103
- if ( index ) {
104
- ( misc . datasource as any ) . replaceOneToOne ( index , newItem ) ;
105
- } else if ( indexes ) {
106
- ( misc . datasource as any ) . replaceManyToOne ( indexes , newItem , increase ) ;
121
+ if ( indexes . length === 1 && amount === 1 ) {
122
+ ( misc . datasource as any ) . replaceOneToOne ( indexes [ 0 ] , items [ 0 ] ) ;
123
+ } else if ( indexes . length > 1 && amount === 1 ) {
124
+ ( misc . datasource as any ) . replaceManyToOne ( indexes , items [ 0 ] , increase ) ;
125
+ } else if ( indexes . length > 1 && amount > 1 ) {
126
+ ( misc . datasource as any ) . replaceManyToMany ( indexes , items , increase ) ;
107
127
}
108
128
109
129
// replace at the Viewport level (scroller)
110
130
await adapter . replace ( {
111
- predicate : ( { $index } ) => ( indexes || [ index ] ) . includes ( $index ) ,
112
- items : [ newItem ] ,
131
+ predicate : ( { $index } ) => indexes . includes ( $index ) ,
132
+ items,
113
133
increase
114
134
} ) ;
115
135
136
+ // refresh the view via scroll to edges
116
137
await misc . scrollMinMax ( ) ;
117
138
118
139
// scroll to replaced item
@@ -121,31 +142,35 @@ const shouldReplace = (config: TestBedConfig) => (misc: Misc) => async (done: Fu
121
142
await misc . relaxNext ( ) ;
122
143
}
123
144
124
- // check replaced item
145
+ // check edge replaced items
125
146
if ( token === 'last' ) {
126
- expect ( adapter . lastVisible . $index ) . toEqual ( newIndex ) ;
147
+ expect ( adapter . lastVisible . $index ) . toEqual ( newIndexLast ) ;
127
148
} else {
128
- expect ( adapter . firstVisible . $index ) . toEqual ( newIndex ) ;
149
+ expect ( adapter . firstVisible . $index ) . toEqual ( newIndexFirst ) ;
129
150
}
130
- expect ( misc . getElementText ( newIndex ) ) . toEqual ( newIndex + ': ' + newItem . text ) ;
151
+ expect ( misc . getElementText ( newIndexFirst ) ) . toEqual ( newIndexFirst + ': ' + items [ 0 ] . text ) ;
152
+ expect ( misc . getElementText ( newIndexLast ) ) . toEqual ( newIndexLast + ': ' + items [ items . length - 1 ] . text ) ;
131
153
132
- // check the next item
154
+ // check the item next to the last replaced one
133
155
if ( token === 'last' ) {
134
- expect ( misc . checkElementContent ( newIndex - 1 , newIndex - ( increase ? diff : 1 ) ) ) . toEqual ( true ) ;
156
+ expect ( misc . checkElementContent ( newIndexFirst - 1 , newIndexFirst - ( increase ? 1 - diff : 1 ) ) ) . toEqual ( true ) ;
135
157
} else {
136
- expect ( misc . checkElementContent ( newIndex + 1 , newIndex + ( increase ? 1 : diff ) ) ) . toEqual ( true ) ;
158
+ expect ( misc . checkElementContent ( newIndexLast + 1 , newIndexLast + ( increase ? 1 : 1 - diff ) ) ) . toEqual ( true ) ;
137
159
}
138
160
139
- expect ( misc . getScrollableSize ( ) ) . toBe ( viewportSize - sizeToRemove ) ;
161
+ expect ( misc . getScrollableSize ( ) ) . toBe ( viewportSize + sizeToChange ) ;
140
162
done ( ) ;
141
163
} ;
142
164
143
165
describe ( 'Adapter Replace Spec' , ( ) => {
144
166
167
+ const getTitle = ( { custom : { token, indexesToReplace : { length } , amount } } : TestBedConfig ) =>
168
+ `should replace ${ token } ${ length === 1 ? 'one' : length } to ${ amount === 1 ? 'one' : amount } ` ;
169
+
145
170
describe ( 'one-to-ne replacement' , ( ) =>
146
171
configList . forEach ( config =>
147
172
makeTest ( {
148
- title : `should work ( ${ config . custom . token } )` ,
173
+ title : getTitle ( config ) ,
149
174
config,
150
175
it : shouldReplace ( config )
151
176
} )
@@ -155,7 +180,7 @@ describe('Adapter Replace Spec', () => {
155
180
describe ( 'many-to-one replacement' , ( ) =>
156
181
manyToOneConfigList . forEach ( config =>
157
182
makeTest ( {
158
- title : `should work ( ${ config . custom . token } )` ,
183
+ title : getTitle ( config ) ,
159
184
config,
160
185
it : shouldReplace ( config )
161
186
} )
@@ -165,7 +190,17 @@ describe('Adapter Replace Spec', () => {
165
190
describe ( 'many-to-one increase replacement' , ( ) =>
166
191
manyToOneIncreaseConfigList . forEach ( config =>
167
192
makeTest ( {
168
- title : `should work (${ config . custom . token } )` ,
193
+ title : getTitle ( config ) ,
194
+ config,
195
+ it : shouldReplace ( config )
196
+ } )
197
+ )
198
+ ) ;
199
+
200
+ describe ( 'many-to-many replacement' , ( ) =>
201
+ manyToManyConfigList . filter ( ( i , j ) => j >= 0 ) . forEach ( config =>
202
+ makeTest ( {
203
+ title : getTitle ( config ) ,
169
204
config,
170
205
it : shouldReplace ( config )
171
206
} )
0 commit comments