From 4b59deb9166edb8cb16f674b1f68efa4ded256db Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 11 Oct 2024 01:33:51 +0200 Subject: [PATCH] Fixed store unit tests --- src/modules/store.js | 8 +- test/modules/store.spec.js | 198 ++++++++++++++++++++++++------------- 2 files changed, 137 insertions(+), 69 deletions(-) diff --git a/src/modules/store.js b/src/modules/store.js index 0c5e31b..eaa9449 100644 --- a/src/modules/store.js +++ b/src/modules/store.js @@ -13,7 +13,7 @@ export default class Store { }; this.callbacks = [() => console.log(this.state)]; - Object.values(this.constructor.actions).forEach(action => document.addEventListener(action, this.eventHandler.bind(this), false)); + Object.values(this.constructor.actions).forEach(action => document.addEventListener(action, this.#eventHandler.bind(this), false)); } subscribe(callback) { @@ -30,7 +30,7 @@ export default class Store { document.dispatchEvent(event); } - eventHandler(event) { + #eventHandler(event) { if (!Object.values(this.constructor.actions).includes(event.type)) { console.error(`Action "${event.type}" is not valid`); return; @@ -48,6 +48,10 @@ export default class Store { }; break; case this.constructor.actions.REMOVE_LINE: + if (this.state.selected.length === 0) { + // Nothing to delete + break; + } this.state = { ...this.state, records: [ ...this.state.records, { diff --git a/test/modules/store.spec.js b/test/modules/store.spec.js index 0da6eb3..d636d98 100644 --- a/test/modules/store.spec.js +++ b/test/modules/store.spec.js @@ -8,87 +8,124 @@ export default class StoreTest { test() { (() => { let called = false - const store = new Store(() => called = true); + const callback = () => called = true; + const store = new Store(); - const event = new Event('addLine'); - store.eventHandler(event); + store.subscribe(callback); + Store.publish(Store.actions.ADD_LINE); - this.assert("eventHandler calls the callback after successfull event", called, true); + this.assert("Subscribed callback is run after a successfull publish action", called, true); })(); (() => { - const store = new Store(() => {}); - store.records = [ + const store = new Store(); + store.state = { + records: [ + {id: 0, text: "asdf", action: 'add'}, + {id: 1, text: "qwer", action: 'add'}, + ], + selected: [0], + }; + + Store.publish('WRONG'); + + const expected = { + records: [ + {id: 0, text: "asdf", action: 'add'}, + {id: 1, text: "qwer", action: 'add'}, + ], + selected: [0], + }; + + this.assert("Publish with unknown action does nothing", store.state, expected); + + })(); + (() => { + const store = new Store(); + + Store.publish(Store.actions.ADD_LINE); + + const expected = [ + {id: 0, text: "asdf", action: 'add'}, + ]; + + this.assert("Publishing action ADD_LINE adds a line to the records", store.state.records, expected); + + })(); + (() => { + const store = new Store(); + store.state = { + records: [ + {id: 0, text: "asdf", action: 'add'}, + ], + selected: [], + }; + + Store.publish(Store.actions.REMOVE_LINE); + + const expected = [ + {id: 0, text: "asdf", action: 'add'}, + ]; + + this.assert("Publish action REMOVE_LINE without selected in the state does nothing", store.state.records, expected); + + })(); + (() => { + const store = new Store(); + store.state = { + records: [ + {id: 0, text: "asdf", action: 'add'}, + ], + selected: [0], + }; + + Store.publish(Store.actions.REMOVE_LINE); + + const expected = [ + {id: 0, text: "asdf", action: 'add'}, + {id: 1, targetIds: [0], action: 'remove'}, + ]; + + this.assert("Publish action REMOVE_LINE with one selected id in the state creates a remove record", store.state.records, expected); + this.assert("Selected status is reset after REMOVE_LINE action", store.state.selected, []); + + })(); + + (() => { + const store = new Store(); + store.state.records = [ {id: 0, text: "asdf", action: 'add'}, {id: 1, text: "qwer", action: 'add'}, {id: 2, action: 'remove'}, ] - const event = new Event('unknown'); - store.eventHandler(event); - - const expected = [ - {id: 0, text: "asdf", action: 'add'}, - {id: 1, text: "qwer", action: 'add'}, - {id: 2, action: 'remove'}, - ]; - - this.assert("eventHandler does nothing on an unknown event type", store.records, expected); - - })(); - (() => { - const store = new Store(() => {}); - - const event = new Event('addLine'); - store.eventHandler(event); - - const expected = [ - {id: 0, text: "asdf", action: 'add'}, - ]; - - this.assert("eventHandler adds the appropiate record when Event 'addLine' is passed", store.records, expected); - - })(); - (() => { - const store = new Store(() => {}); - store.records = [ - {id: 0, text: "asdf", action: 'add'}, - ] - - const event = new Event('removeLine'); - store.eventHandler(event); - - const expected = [ - {id: 0, text: "asdf", action: 'add'}, - {id: 1, action: 'remove'}, - ]; - - this.assert("eventHandler adds the appropiate record when Event 'removeLine' is passed", store.records, expected); - - })(); - (() => { - const store = new Store(() => {}); - store.records = [ - {id: 0, text: "asdf", action: 'add'}, - {id: 1, text: "qwer", action: 'add'}, - {id: 2, action: 'remove'}, - ] - - const event = new Event('undo'); - store.eventHandler(event); + Store.publish(Store.actions.UNDO); const expected = [ {id: 0, text: "asdf", action: 'add'}, {id: 1, text: "qwer", action: 'add'}, ]; - this.assert("eventHandler deletes the appropiate record when Event 'undo' is passed", store.records, expected); + this.assert("Publish action UNDO removes last record", store.state.records, expected); })(); - (() => { - const store = new Store(() => {}); - store.records = [ + (() => { + const store = new Store(); + store.state.selected = [0] + + Store.publish(Store.actions.SELECTED, [1, 3]); + + const expected = [1, 3]; + + this.assert("Publish action SELECTED updates the array of selected ids", store.state.records, expected); + + })(); + + (() => { + const store = new Store(); + + store.state.records = [ {id: 0, text: "Line 1", action: 'add'}, {id: 1, text: "Line 2", action: 'add'}, {id: 2, text: "Line 3", action: 'add'}, @@ -100,17 +137,17 @@ export default class StoreTest { {id: 2, text: "Line 3"}, ]; - this.assert("getState returns an array of strings with 3 add records", store.getState(), expected); + this.assert("getRecords() returns an array of strings with 3 add records", store.getRecords(), expected); })(); (() => { - const store = new Store(() => {}); + const store = new Store(); - store.records = [ + store.state.records = [ {id: 0, text: "Line 1", action: 'add'}, {id: 1, text: "Line 2", action: 'add'}, {id: 2, text: "Line 3", action: 'add'}, - {id: 3, targetId: 1, action: 'remove'}, + {id: 3, targetIds: [1], action: 'remove'}, ]; const expected = [ @@ -118,7 +155,34 @@ export default class StoreTest { {id: 2, text: "Line 3"}, ]; - this.assert("getState returns an array of strings with 3 add records and a delete record", store.getState(), expected); + this.assert("getRecords() returns an array of 2 strings with 3 add records and a delete record with 1 id", store.getRecords(), expected); + })(); + + (() => { + const store = new Store(); + + store.state.records = [ + {id: 0, text: "Line 1", action: 'add'}, + {id: 1, text: "Line 2", action: 'add'}, + {id: 2, text: "Line 3", action: 'add'}, + {id: 3, targetIds: [0, 1], action: 'remove'}, + ]; + + const expected = [ + {id: 2, text: "Line 3"}, + ]; + + this.assert("getRecords() returns an array of 1 string with 3 add records and a delete record with 2 ids", store.getRecords(), expected); + })(); + + (() => { + const store = new Store(); + + store.state.selected = [0, 1]; + + const expected = [0, 1]; + + this.assert("getSelected() returns an array of ids representing the selected items", store.getSelected(), expected); })(); } }