From 0c83ec057051c9364e321083fe94873b0874cb18 Mon Sep 17 00:00:00 2001 From: Frederic Heem Date: Tue, 26 Nov 2024 17:04:19 -0300 Subject: [PATCH] add tests for arrays --- bau/test/state-array.test.js | 64 ++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/bau/test/state-array.test.js b/bau/test/state-array.test.js index 9cf0639e..16ce1d75 100644 --- a/bau/test/state-array.test.js +++ b/bau/test/state-array.test.js @@ -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"); }); });