@@ -109,11 +109,11 @@ describe('array-changes', function () {
109109 expect ( arrayChanges ( [ 1 , 2 , 3 , 0 ] , [ 0 , 1 , 2 , 3 ] , function ( a , b ) {
110110 return a === b ;
111111 } ) , 'to equal' , [
112- { type : 'moveTarget' , value : 0 , actualIndex : 3 , expected : 0 , expectedIndex : 0 , equal : true , last : false } ,
112+ { type : 'moveTarget' , value : 0 , actualIndex : 3 , expected : 0 , expectedIndex : 0 , id : 0 , equal : true , last : false } ,
113113 { type : 'equal' , value : 1 , actualIndex : 0 , expected : 1 , expectedIndex : 1 } ,
114114 { type : 'equal' , value : 2 , actualIndex : 1 , expected : 2 , expectedIndex : 2 } ,
115115 { type : 'equal' , value : 3 , actualIndex : 2 , expected : 3 , expectedIndex : 3 } ,
116- { type : 'moveSource' , value : 0 , actualIndex : 3 , expected : 0 , expectedIndex : 0 , equal : true , last : true }
116+ { type : 'moveSource' , value : 0 , actualIndex : 3 , expected : 0 , expectedIndex : 0 , id : 0 , equal : true , last : true }
117117 ] ) ;
118118 } ) ;
119119
@@ -122,9 +122,9 @@ describe('array-changes', function () {
122122 return a === b ;
123123 } ) , 'to equal' , [
124124 { type : 'equal' , value : 0 , actualIndex : 0 , expected : 0 , expectedIndex : 0 } ,
125- { type : 'moveTarget' , value : 2 , actualIndex : 2 , expected : 2 , expectedIndex : 1 , equal : true , last : false } ,
125+ { type : 'moveTarget' , value : 2 , actualIndex : 2 , expected : 2 , expectedIndex : 1 , id : 0 , equal : true , last : false } ,
126126 { type : 'equal' , value : 1 , actualIndex : 1 , expected : 1 , expectedIndex : 2 } ,
127- { type : 'moveSource' , value : 2 , actualIndex : 2 , expected : 2 , expectedIndex : 1 , equal : true } ,
127+ { type : 'moveSource' , value : 2 , actualIndex : 2 , expected : 2 , expectedIndex : 1 , id : 0 , equal : true } ,
128128 { type : 'equal' , value : 3 , actualIndex : 3 , expected : 3 , expectedIndex : 3 , last : true }
129129 ] ) ;
130130 } ) ;
@@ -451,6 +451,7 @@ describe('array-changes', function () {
451451 expected : { kind : 1 , type : 'tag' , name : 'p' , children : [ { data : 'Hello world 2025' , type : 'text' } ] , attribs : { } } ,
452452 actualIndex : 1 ,
453453 expectedIndex : 0 ,
454+ id : 0 ,
454455 equal : false ,
455456 last : false
456457 } ,
@@ -467,9 +468,46 @@ describe('array-changes', function () {
467468 expected : { kind : 1 , type : 'tag' , name : 'p' , children : [ { data : 'Hello world 2025' , type : 'text' } ] , attribs : { } } ,
468469 actualIndex : 1 ,
469470 expectedIndex : 0 ,
471+ id : 0 ,
470472 equal : false ,
471473 last : true
472474 }
473475 ] ) ;
474476 } ) ;
477+
478+ it ( "handles moves where no items are similar" , function ( ) {
479+ var a = [ 'a' , 'b' , 'c' ] ;
480+ var b = [ 'c' , 'b' , 'a' ] ;
481+
482+ expect ( arrayChanges ( a , b , function ( a , b ) {
483+ return expect . equal ( a , b ) ;
484+ } , function ( ) {
485+ return false ;
486+ } ) , 'to equal' , [
487+ { type : 'moveTarget' , value : 'c' , actualIndex : 2 , last : false , expected : 'c' , expectedIndex : 0 , id : 1 , equal : true } ,
488+ { type : 'moveTarget' , value : 'b' , actualIndex : 1 , last : false , expected : 'b' , expectedIndex : 1 , id : 0 , equal : true } ,
489+ { type : 'equal' , value : 'a' , actualIndex : 0 , expected : 'a' , expectedIndex : 2 } ,
490+ { type : 'moveSource' , value : 'b' , actualIndex : 1 , expected : 'b' , expectedIndex : 1 , id : 0 , equal : true } ,
491+ { type : 'moveSource' , value : 'c' , actualIndex : 2 , expected : 'c' , expectedIndex : 0 , id : 1 , equal : true , last : true }
492+ ] ) ;
493+ } ) ;
494+
495+ it ( "handles moves with a mix of equal and similar items" , function ( ) {
496+ var a = [ 'aaa' , 'bbb' , 'ccc' , 'dddd' ] ;
497+ var b = [ 'ddd' , 'ccc' , 'bbb' , 'aaa' ] ;
498+
499+ expect ( arrayChanges ( a , b , function ( a , b ) {
500+ return expect . equal ( a , b ) ;
501+ } , function ( a , b ) {
502+ return expect . equal ( a . slice ( 0 , 3 ) , b . slice ( 0 , 3 ) ) ;
503+ } ) , 'to equal' , [
504+ { type : 'moveTarget' , value : 'dddd' , actualIndex : 3 , id : 2 , last : false , expected : 'ddd' , expectedIndex : 0 , equal : false } ,
505+ { type : 'moveTarget' , value : 'ccc' , actualIndex : 2 , id : 1 , last : false , expected : 'ccc' , expectedIndex : 1 , equal : true } ,
506+ { type : 'moveTarget' , value : 'bbb' , actualIndex : 1 , id : 0 , last : false , expected : 'bbb' , expectedIndex : 2 , equal : true } ,
507+ { type : 'equal' , value : 'aaa' , actualIndex : 0 , expected : 'aaa' , expectedIndex : 3 } ,
508+ { type : 'moveSource' , value : 'bbb' , actualIndex : 1 , id : 0 , expected : 'bbb' , expectedIndex : 2 , equal : true } ,
509+ { type : 'moveSource' , value : 'ccc' , actualIndex : 2 , id : 1 , expected : 'ccc' , expectedIndex : 1 , equal : true } ,
510+ { type : 'moveSource' , value : 'dddd' , actualIndex : 3 , id : 2 , expected : 'ddd' , expectedIndex : 0 , equal : false , last : true }
511+ ] ) ;
512+ } ) ;
475513} ) ;
0 commit comments