@@ -11,6 +11,13 @@ const baseSettings = {
11
11
itemSize : 20
12
12
} ;
13
13
14
+ interface ICustom {
15
+ token : 'first' | 'last' | 'middle' ;
16
+ indexToReplace ?: number ;
17
+ indexesToReplace ?: number [ ] ;
18
+ increase ?: boolean ;
19
+ }
20
+
14
21
const configList : TestBedConfig [ ] = [ {
15
22
datasourceSettings : { ...baseSettings } ,
16
23
custom : {
@@ -22,19 +29,19 @@ const configList: TestBedConfig[] = [{
22
29
custom : {
23
30
indexToReplace : baseSettings . minIndex ,
24
31
token : 'first'
25
- }
32
+ } as ICustom
26
33
} , {
27
34
datasourceSettings : { ...baseSettings , startIndex : baseSettings . maxIndex } ,
28
35
custom : {
29
36
indexToReplace : baseSettings . maxIndex ,
30
37
token : 'last'
31
- }
38
+ } as ICustom
32
39
} ] . map ( config => ( {
33
40
...config ,
34
41
datasourceClass : getDatasourceReplacementsClass ( config . datasourceSettings )
35
42
} ) ) ;
36
43
37
- const someToOneConfigList : TestBedConfig [ ] = [ {
44
+ const manyToOneConfigList : TestBedConfig [ ] = [ {
38
45
datasourceSettings : { ...baseSettings } ,
39
46
custom : {
40
47
indexesToReplace : [
@@ -43,7 +50,7 @@ const someToOneConfigList: TestBedConfig[] = [{
43
50
baseSettings . minIndex + 3
44
51
] ,
45
52
token : 'middle'
46
- }
53
+ } as ICustom
47
54
} , {
48
55
datasourceSettings : { ...baseSettings } ,
49
56
custom : {
@@ -53,7 +60,7 @@ const someToOneConfigList: TestBedConfig[] = [{
53
60
baseSettings . minIndex + 2
54
61
] ,
55
62
token : 'first'
56
- }
63
+ } as ICustom
57
64
} , {
58
65
datasourceSettings : { ...baseSettings , startIndex : baseSettings . maxIndex } ,
59
66
custom : {
@@ -63,36 +70,47 @@ const someToOneConfigList: TestBedConfig[] = [{
63
70
baseSettings . maxIndex
64
71
] ,
65
72
token : 'last'
66
- }
73
+ } as ICustom
67
74
} ] . map ( config => ( {
68
75
...config ,
69
76
datasourceClass : getDatasourceReplacementsClass ( config . datasourceSettings )
70
77
} ) ) ;
71
78
79
+ const manyToOneIncreaseConfigList = manyToOneConfigList . map ( config => ( {
80
+ ...config ,
81
+ custom : {
82
+ ...config . custom ,
83
+ increase : true
84
+ }
85
+ } ) ) ;
72
86
73
87
const shouldReplace = ( config : TestBedConfig ) => ( misc : Misc ) => async ( done : Function ) => {
74
88
await misc . relaxNext ( ) ;
75
89
const { adapter } = misc ;
76
- const { custom : { indexToReplace : index , indexesToReplace : indexes , token } } = config ;
90
+ const { indexToReplace : index , indexesToReplace : indexes , token, increase } = config . custom ;
77
91
const { datasourceSettings : { minIndex, itemSize } } = config ;
78
92
const diff = indexes ? indexes . length : 1 ;
79
- const maxScrollPosition = misc . getMaxScrollPosition ( ) - ( diff - 1 ) * misc . getItemSize ( ) ;
80
- const newIndex = indexes ? indexes [ 0 ] : index ;
81
- const position = token === 'last' ? maxScrollPosition : ( newIndex - 1 + minIndex - 1 ) * itemSize ;
93
+ 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 ;
82
99
const newItem = generateItem ( newIndex ) ;
83
100
newItem . text += '*' ;
84
101
85
102
// replace at the Datasource level (component)
86
103
if ( index ) {
87
104
( misc . datasource as any ) . replaceOneToOne ( index , newItem ) ;
88
105
} else if ( indexes ) {
89
- ( misc . datasource as any ) . replaceManyToOne ( indexes , newItem ) ;
106
+ ( misc . datasource as any ) . replaceManyToOne ( indexes , newItem , increase ) ;
90
107
}
91
108
92
109
// replace at the Viewport level (scroller)
93
110
await adapter . replace ( {
94
111
predicate : ( { $index } ) => ( indexes || [ index ] ) . includes ( $index ) ,
95
- items : [ newItem ]
112
+ items : [ newItem ] ,
113
+ increase
96
114
} ) ;
97
115
98
116
await misc . scrollMinMax ( ) ;
@@ -113,17 +131,18 @@ const shouldReplace = (config: TestBedConfig) => (misc: Misc) => async (done: Fu
113
131
114
132
// check the next item
115
133
if ( token === 'last' ) {
116
- expect ( misc . checkElementContent ( newIndex - 1 , newIndex - 1 ) ) . toEqual ( true ) ;
134
+ expect ( misc . checkElementContent ( newIndex - 1 , newIndex - ( increase ? diff : 1 ) ) ) . toEqual ( true ) ;
117
135
} else {
118
- expect ( misc . checkElementContent ( newIndex + 1 , newIndex + diff ) ) . toEqual ( true ) ;
136
+ expect ( misc . checkElementContent ( newIndex + 1 , newIndex + ( increase ? 1 : diff ) ) ) . toEqual ( true ) ;
119
137
}
120
138
139
+ expect ( misc . getScrollableSize ( ) ) . toBe ( viewportSize - sizeToRemove ) ;
121
140
done ( ) ;
122
141
} ;
123
142
124
143
describe ( 'Adapter Replace Spec' , ( ) => {
125
144
126
- describe ( 'single replacement' , ( ) =>
145
+ describe ( 'one-to-ne replacement' , ( ) =>
127
146
configList . forEach ( config =>
128
147
makeTest ( {
129
148
title : `should work (${ config . custom . token } )` ,
@@ -133,8 +152,18 @@ describe('Adapter Replace Spec', () => {
133
152
)
134
153
) ;
135
154
136
- describe ( 'some-to-one replacement' , ( ) =>
137
- someToOneConfigList . forEach ( config =>
155
+ describe ( 'many-to-one replacement' , ( ) =>
156
+ manyToOneConfigList . forEach ( config =>
157
+ makeTest ( {
158
+ title : `should work (${ config . custom . token } )` ,
159
+ config,
160
+ it : shouldReplace ( config )
161
+ } )
162
+ )
163
+ ) ;
164
+
165
+ describe ( 'many-to-one increase replacement' , ( ) =>
166
+ manyToOneIncreaseConfigList . forEach ( config =>
138
167
makeTest ( {
139
168
title : `should work (${ config . custom . token } )` ,
140
169
config,
0 commit comments