@@ -1831,6 +1831,45 @@ describe("ArraySchema Tests", () => {
1831
1831
assertDeepStrictEqualEncodeAll ( state ) ;
1832
1832
} ) ;
1833
1833
1834
+ it ( "should allow to swap items between two ArraySchema" , ( ) => {
1835
+ class Item extends Schema {
1836
+ @type ( "string" ) name : string ;
1837
+ }
1838
+ class State extends Schema {
1839
+ @type ( [ Item ] ) items1 = new ArraySchema < Item > ( ) ;
1840
+ @type ( [ Item ] ) items2 = new ArraySchema < Item > ( ) ;
1841
+ }
1842
+
1843
+ const state = new State ( ) ;
1844
+ const decodedState = new State ( ) ;
1845
+
1846
+ state . items1 . push ( new Item ( ) . assign ( { name : "Item 1" } ) ) ;
1847
+ state . items1 . push ( new Item ( ) . assign ( { name : "Item 2" } ) ) ;
1848
+
1849
+ decodedState . decode ( state . encode ( ) ) ;
1850
+ assert . strictEqual ( 2 , decodedState . items1 . length ) ;
1851
+
1852
+ const decodedItem1 = decodedState . items1 [ 0 ] ;
1853
+ state . items2 . push ( state . items1 . shift ( ) ) ;
1854
+
1855
+ const swapOperation = state . encode ( ) ;
1856
+ console . log ( "(can we have less bytes here?)" , Array . from ( swapOperation ) ) ;
1857
+ // TODO: encode length should be less than 10 bytes
1858
+ // assert.ok(swapOperation.byteLength < 10);
1859
+ decodedState . decode ( swapOperation ) ;
1860
+
1861
+ const movedItem1 = decodedState . items2 [ 0 ] ;
1862
+
1863
+ assert . strictEqual ( 1 , decodedState . items1 . length ) ;
1864
+ assert . strictEqual ( 1 , decodedState . items2 . length ) ;
1865
+
1866
+ assert . strictEqual ( decodedItem1 , movedItem1 , "should hold the same Item reference." ) ;
1867
+
1868
+ assert . deepStrictEqual ( state . toJSON ( ) , decodedState . toJSON ( ) ) ;
1869
+
1870
+ assertDeepStrictEqualEncodeAll ( state ) ;
1871
+ } ) ;
1872
+
1834
1873
it ( "should splice an ArraySchema of primitive values" , ( ) => {
1835
1874
class Player extends Schema {
1836
1875
@type ( [ "string" ] ) itemIds = new ArraySchema < string > ( ) ;
0 commit comments