Skip to content

Commit

Permalink
add test reproducing #107
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Apr 28, 2021
1 parent 815d776 commit a09b0ca
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions test/ArraySchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ describe("ArraySchema Tests", () => {
});
});

it("#clear()", () => {
describe("#clear", () => {
class Point extends Schema {
@type("number") x: number;
@type("number") y: number;
Expand All @@ -1271,23 +1271,46 @@ describe("ArraySchema Tests", () => {
@type([Point]) points = new ArraySchema<Point>();
}

const state = new State();
state.points.push(
new Point().assign({ x: 0, y: 0 }),
new Point().assign({ x: 1, y: 1 }),
new Point().assign({ x: 2, y: 2 }),
);
it("should have 0 entries after clear", () => {
const state = new State();
state.points.push(
new Point().assign({ x: 0, y: 0 }),
new Point().assign({ x: 1, y: 1 }),
new Point().assign({ x: 2, y: 2 }),
);

let decodedState = new State();
decodedState.decode(state.encodeAll());
assert.strictEqual(3, decodedState.points.length);
let decodedState = new State();
decodedState.decode(state.encodeAll());
assert.strictEqual(3, decodedState.points.length);

state.points.clear();
state.points.clear();

decodedState = new State();
decodedState.decode(state.encodeAll());
assert.strictEqual(0, decodedState.points.length);
});

xit("should trigger onAdd callback only once after clearing and adding one item", () => {
const state = new State();
const decodedState = new State();

// state.points.push(new Point().assign({ x: 0, y: 0 }));
// state.points.push(new Point().assign({ x: 1, y: 1 }));
state.points.clear();
state.points.push(new Point().assign({ x: 2, y: 2 }));
state.points.push(new Point().assign({ x: 3, y: 3 }));

decodedState.points.onAdd = (point, key) => console.log(point.toJSON(), key);

const onAddSpy = sinon.spy(decodedState.points, 'onAdd');

decodedState.decode(state.encodeAll());
decodedState.decode(state.encode());

sinon.assert.callCount(onAddSpy, 1);
});
})

decodedState = new State();
decodedState.decode(state.encodeAll());
assert.strictEqual(0, decodedState.points.length);
});

describe("array methods", () => {
it("#find()", () => {
Expand Down

0 comments on commit a09b0ca

Please sign in to comment.