Skip to content

Commit

Permalink
ArraySchema: add failing test case for #94. (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed May 13, 2024
1 parent ae99eaf commit 53da4b8
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/ArraySchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,40 @@ describe("ArraySchema Tests", () => {
});
});

it("push, splice, push", () => {
class State extends Schema {
@type(["number"]) cards = new ArraySchema<number>();
}

const state = new State();
const decodedState = new State();
const $ = getCallbacks(decodedState).$;

const onAddIndexes: Array<{ item: number, index: number }> = [];
const onRemoveIndexes: Array<{ item: number, index: number }> = [];
$(decodedState).cards.onAdd((item, index) => onAddIndexes.push({ item, index }));
$(decodedState).cards.onRemove((item, index) => onRemoveIndexes.push({ item, index }));

decodedState.decode(state.encodeAll());

state.cards.push(1);
decodedState.decode(state.encode());

assert.strictEqual(1, state.cards[0]);
assert.deepStrictEqual([{ item: 1, index: 0 }], onAddIndexes);

state.cards.splice(0, 1);
decodedState.decode(state.encode());

assert.strictEqual(undefined, state.cards[0]);
assert.deepStrictEqual([{ item: 1, index: 0 }], onAddIndexes);

state.cards.push(2);
decodedState.decode(state.encode());
assert.strictEqual(2, state.cards[0]);
assert.deepStrictEqual([{ item: 1, index: 0 }, { item: 2, index: 0 }], onAddIndexes);
});

it("should allow using push/pop before encoding", () => {
class State extends Schema {
@type(["number"]) numbers = new ArraySchema<number>();
Expand Down

0 comments on commit 53da4b8

Please sign in to comment.