@@ -448,9 +448,9 @@ describe( 'Collection', () => {
448
448
collection . remove ( 'bom' ) ; // by id
449
449
450
450
sinon . assert . calledThrice ( spy ) ;
451
- sinon . assert . calledWithExactly ( spy , sinon . match . has ( 'source' , collection ) , item1 ) ;
452
- sinon . assert . calledWithExactly ( spy , sinon . match . has ( 'source' , collection ) , item2 ) ;
453
- sinon . assert . calledWithExactly ( spy , sinon . match . has ( 'source' , collection ) , item3 ) ;
451
+ sinon . assert . calledWithExactly ( spy , sinon . match . has ( 'source' , collection ) , item1 , 0 ) ;
452
+ sinon . assert . calledWithExactly ( spy , sinon . match . has ( 'source' , collection ) , item2 , 1 ) ;
453
+ sinon . assert . calledWithExactly ( spy , sinon . match . has ( 'source' , collection ) , item3 , 0 ) ;
454
454
} ) ;
455
455
456
456
it ( 'should throw an error on invalid index' , ( ) => {
@@ -767,6 +767,34 @@ describe( 'Collection', () => {
767
767
expect ( collection . get ( 0 ) . value ) . to . equal ( 'foo' ) ;
768
768
expect ( collection . get ( 1 ) . value ) . to . equal ( 'bar' ) ;
769
769
} ) ;
770
+
771
+ it ( 'skips when there is no item' , ( ) => {
772
+ // Add before collection is bound.
773
+ items . add ( { value : 1 , skip : true } ) ;
774
+
775
+ expect ( collection ) . to . have . length ( 0 ) ;
776
+
777
+ collection . bindTo ( items ) . using ( item => {
778
+ if ( item . skip ) {
779
+ return null ;
780
+ }
781
+
782
+ return item ;
783
+ } ) ;
784
+
785
+ // Still 0 because initial item was skipped.
786
+ expect ( collection ) . to . have . length ( 0 ) ;
787
+
788
+ items . add ( { value : 2 , skip : false } ) ;
789
+ items . add ( { value : 3 , skip : true } ) ;
790
+ items . add ( { value : 4 , skip : false } ) ;
791
+
792
+ expect ( Array . from ( collection , item => item . value ) ) . to . deep . equal ( [ 2 , 4 ] ) ;
793
+
794
+ items . add ( { value : 5 , skip : false } , 2 ) ;
795
+
796
+ expect ( Array . from ( collection , item => item . value ) ) . to . deep . equal ( [ 2 , 5 , 4 ] ) ;
797
+ } ) ;
770
798
} ) ;
771
799
772
800
describe ( 'property name' , ( ) => {
@@ -802,6 +830,25 @@ describe( 'Collection', () => {
802
830
items . remove ( 0 ) ;
803
831
expect ( collection ) . to . have . length ( 0 ) ;
804
832
} ) ;
833
+
834
+ it ( 'skips when there is no item' , ( ) => {
835
+ items . add ( { prop : null } ) ;
836
+
837
+ collection . bindTo ( items ) . using ( 'prop' ) ;
838
+
839
+ // Still 0 because initial item was skipped.
840
+ expect ( collection ) . to . have . length ( 0 ) ;
841
+
842
+ items . add ( { prop : { value : 2 , skip : false } } ) ;
843
+ items . add ( { prop : null } ) ;
844
+ items . add ( { prop : { value : 4 , skip : false } } ) ;
845
+
846
+ expect ( Array . from ( collection , item => item . value ) ) . to . deep . equal ( [ 2 , 4 ] ) ;
847
+
848
+ items . add ( { prop : { value : 5 } } , 2 ) ;
849
+
850
+ expect ( Array . from ( collection , item => item . value ) ) . to . deep . equal ( [ 2 , 5 , 4 ] ) ;
851
+ } ) ;
805
852
} ) ;
806
853
} ) ;
807
854
@@ -1032,6 +1079,115 @@ describe( 'Collection', () => {
1032
1079
sinon . assert . callCount ( spyRemoveA , 2 ) ;
1033
1080
sinon . assert . callCount ( spyRemoveB , 2 ) ;
1034
1081
} ) ;
1082
+
1083
+ describe ( 'skipping items' , ( ) => {
1084
+ let collectionA , collectionB ;
1085
+
1086
+ beforeEach ( ( ) => {
1087
+ collectionA = new Collection ( ) ;
1088
+ collectionB = new Collection ( ) ;
1089
+
1090
+ // A<--->B
1091
+ collectionA . bindTo ( collectionB ) . using ( item => {
1092
+ if ( item . skip ) {
1093
+ return null ;
1094
+ }
1095
+
1096
+ return item ;
1097
+ } ) ;
1098
+
1099
+ collectionB . bindTo ( collectionA ) . using ( item => {
1100
+ if ( item . skip ) {
1101
+ return null ;
1102
+ }
1103
+
1104
+ return item ;
1105
+ } ) ;
1106
+ } ) ;
1107
+
1108
+ it ( 'should add items at the enf of collections when includes skipped items' , ( ) => {
1109
+ collectionA . add ( { v : 'A' } ) ;
1110
+ collectionA . add ( { v : 'B' , skip : true } ) ;
1111
+ collectionA . add ( { v : 'C' , skip : true } ) ;
1112
+ collectionA . add ( { v : 'D' } ) ;
1113
+
1114
+ expect ( collectionA . _skippedIndexesFromExternal ) . to . have . members ( [ ] ) ;
1115
+ expect ( collectionB . _skippedIndexesFromExternal ) . to . have . members ( [ 1 , 2 ] ) ;
1116
+ assertItems ( collectionA , [ 'A' , 'B' , 'C' , 'D' ] ) ;
1117
+ assertItems ( collectionB , [ 'A' , 'D' ] ) ;
1118
+
1119
+ collectionB . add ( { v : 'E' } ) ;
1120
+ collectionB . add ( { v : 'F' , skip : true } ) ;
1121
+ collectionB . add ( { v : 'G' } ) ;
1122
+
1123
+ expect ( collectionA . _skippedIndexesFromExternal ) . to . have . members ( [ 3 ] ) ;
1124
+ expect ( collectionB . _skippedIndexesFromExternal ) . to . have . members ( [ 1 , 2 ] ) ;
1125
+ assertItems ( collectionA , [ 'A' , 'B' , 'C' , 'D' , 'E' , 'G' ] ) ;
1126
+ assertItems ( collectionB , [ 'A' , 'D' , 'E' , 'F' , 'G' ] ) ;
1127
+ } ) ;
1128
+
1129
+ it ( 'should add items between skipped items' , ( ) => {
1130
+ collectionA . add ( { v : 'A' } ) ;
1131
+ collectionA . add ( { v : 'B' , skip : true } ) ;
1132
+ collectionA . add ( { v : 'C' , skip : true } ) ;
1133
+ collectionA . add ( { v : 'D' } ) ;
1134
+
1135
+ collectionB . add ( { v : 'E' } ) ;
1136
+ collectionB . add ( { v : 'F' , skip : true } ) ;
1137
+ collectionB . add ( { v : 'G' , skip : true } ) ;
1138
+ collectionB . add ( { v : 'H' } ) ;
1139
+
1140
+ expect ( collectionA . _skippedIndexesFromExternal ) . to . have . members ( [ 3 , 4 ] ) ;
1141
+ expect ( collectionB . _skippedIndexesFromExternal ) . to . have . members ( [ 1 , 2 ] ) ;
1142
+ assertItems ( collectionA , [ 'A' , 'B' , 'C' , 'D' , 'E' , 'H' ] ) ;
1143
+ assertItems ( collectionB , [ 'A' , 'D' , 'E' , 'F' , 'G' , 'H' ] ) ;
1144
+
1145
+ collectionA . add ( { v : 'I' } , 2 ) ;
1146
+
1147
+ expect ( collectionA . _skippedIndexesFromExternal ) . to . have . members ( [ 4 , 5 ] ) ;
1148
+ expect ( collectionB . _skippedIndexesFromExternal ) . to . have . members ( [ 1 , 2 ] ) ;
1149
+ assertItems ( collectionA , [ 'A' , 'B' , 'I' , 'C' , 'D' , 'E' , 'H' ] ) ;
1150
+ assertItems ( collectionB , [ 'A' , 'I' , 'D' , 'E' , 'F' , 'G' , 'H' ] ) ;
1151
+
1152
+ collectionB . add ( { v : 'J' } , 5 ) ;
1153
+
1154
+ expect ( collectionA . _skippedIndexesFromExternal ) . to . have . members ( [ 4 , 5 ] ) ;
1155
+ expect ( collectionB . _skippedIndexesFromExternal ) . to . have . members ( [ 1 , 2 ] ) ;
1156
+ assertItems ( collectionA , [ 'A' , 'B' , 'I' , 'C' , 'D' , 'E' , 'J' , 'H' ] ) ;
1157
+ assertItems ( collectionB , [ 'A' , 'I' , 'D' , 'E' , 'F' , 'J' , 'G' , 'H' ] ) ;
1158
+ } ) ;
1159
+
1160
+ it ( 'should properly remove skipped items and update skipped indexes' , ( ) => {
1161
+ collectionA . add ( { v : 'A' } ) ;
1162
+ collectionA . add ( { v : 'B' , skip : true } ) ;
1163
+ collectionA . add ( { v : 'C' , skip : true } ) ;
1164
+ collectionA . add ( { v : 'D' } ) ;
1165
+
1166
+ collectionB . add ( { v : 'E' } ) ;
1167
+ collectionB . add ( { v : 'F' , skip : true } ) ;
1168
+ collectionB . add ( { v : 'G' , skip : true } ) ;
1169
+ collectionB . add ( { v : 'H' } ) ;
1170
+
1171
+ expect ( collectionA . _skippedIndexesFromExternal ) . to . have . members ( [ 3 , 4 ] ) ;
1172
+ expect ( collectionB . _skippedIndexesFromExternal ) . to . have . members ( [ 1 , 2 ] ) ;
1173
+ assertItems ( collectionA , [ 'A' , 'B' , 'C' , 'D' , 'E' , 'H' ] ) ;
1174
+ assertItems ( collectionB , [ 'A' , 'D' , 'E' , 'F' , 'G' , 'H' ] ) ;
1175
+
1176
+ collectionA . remove ( 2 ) ;
1177
+
1178
+ expect ( collectionA . _skippedIndexesFromExternal ) . to . have . members ( [ 3 , 4 ] ) ;
1179
+ expect ( collectionB . _skippedIndexesFromExternal ) . to . have . members ( [ 1 ] ) ;
1180
+ assertItems ( collectionA , [ 'A' , 'B' , 'D' , 'E' , 'H' ] ) ;
1181
+ assertItems ( collectionB , [ 'A' , 'D' , 'E' , 'F' , 'G' , 'H' ] ) ;
1182
+
1183
+ collectionB . remove ( 3 ) ;
1184
+
1185
+ expect ( collectionA . _skippedIndexesFromExternal ) . to . have . members ( [ 3 ] ) ;
1186
+ expect ( collectionB . _skippedIndexesFromExternal ) . to . have . members ( [ 1 ] ) ;
1187
+ assertItems ( collectionA , [ 'A' , 'B' , 'D' , 'E' , 'H' ] ) ;
1188
+ assertItems ( collectionB , [ 'A' , 'D' , 'E' , 'G' , 'H' ] ) ;
1189
+ } ) ;
1190
+ } ) ;
1035
1191
} ) ;
1036
1192
} ) ;
1037
1193
0 commit comments