Skip to content

Commit

Permalink
add tests for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
FredericHeem committed Nov 26, 2024
1 parent d68f0f8 commit 0c83ec0
Showing 1 changed file with 58 additions and 6 deletions.
64 changes: 58 additions & 6 deletions bau/test/state-array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,64 @@ describe("state array", async () => {
const state = bau.state([1]);
state.val = [2];
});
it("push one", () => {
const state = bau.state([1]);
state.val.push(2);
it("push", () => {
const state = bau.state([]);
assert.equal(state.val.length, 0);
// Push one
state.val.push("banana");
assert.equal(state.val.length, 1);
assert.equal(state.val[0], "banana");
// Push multiple
state.val.push("mango", "grape");
assert.equal(state.val.length, 3);
assert.equal(state.val[2], "grape");
});
it("push 2 elements", () => {
const state = bau.state([1]);
state.val.push(2, 3);
it("pop", () => {
// pop: remove the last element
const state = bau.state(["banana", "apple", "mango"]);
state.val.pop();
assert.equal(state.val.length, 2);
});
it("shift", () => {
// remove the first element
const state = bau.state(["banana", "apple", "mango"]);
state.val.shift();
assert.equal(state.val.length, 2);
assert.equal(state.val[0], "apple");
});
it("unshift", () => {
// unshift: add item at the front
const state = bau.state(["banana", "mango"]);
state.val.unshift("apple");
assert.equal(state.val.length, 3);
assert.equal(state.val[0], "apple");

// unshift: add item at the front
state.val.unshift("ananas", "melon");
assert.equal(state.val.length, 5);
assert.equal(state.val[0], "ananas");
assert.equal(state.val[1], "melon");
});
it("splice: remove one item", () => {
const state = bau.state(["banana", "apple", "mango"]);
state.val.splice(1, 1);
assert.equal(state.val.length, 2);
assert.equal(state.val[1], "mango");
});
it("splice: remove the last 2", () => {
const state = bau.state(["banana", "apple", "mango"]);
state.val.splice(-2, 2);
assert.equal(state.val.length, 1);
assert.equal(state.val[0], "banana");
});
it("sort", () => {
const state = bau.state(["banana", "apple"]);
state.val.sort();
assert.equal(state.val[0], "apple");
});
it("reverse", () => {
const state = bau.state(["banana", "apple"]);
state.val.reverse();
assert.equal(state.val[0], "apple");
});
});

0 comments on commit 0c83ec0

Please sign in to comment.