Skip to content

Commit d3663c3

Browse files
committed
ArraySchema: add test swapping one item from one array to another
1 parent 09000d4 commit d3663c3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

test/ArraySchema.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,45 @@ describe("ArraySchema Tests", () => {
18311831
assertDeepStrictEqualEncodeAll(state);
18321832
});
18331833

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+
18341873
it("should splice an ArraySchema of primitive values", () => {
18351874
class Player extends Schema {
18361875
@type(["string"]) itemIds = new ArraySchema<string>();

0 commit comments

Comments
 (0)