import Store from '../../src/modules/store.js'; export default class StoreTest { constructor(assert) { this.assert = assert; } test() { (() => { let called = false const callback = () => called = true; const store = new Store(); store.subscribe(callback); Store.publish(Store.actions.ADD_LINE); this.assert("Subscribed callback is run after a successfull publish action", called, true); })(); (() => { 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'}, ] Store.publish(Store.actions.UNDO); const expected = [ {id: 0, text: "asdf", action: 'add'}, {id: 1, text: "qwer", action: 'add'}, ]; this.assert("Publish action UNDO removes last record", store.state.records, expected); })(); (() => { 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'}, ]; const expected = [ {id: 0, text: "Line 1"}, {id: 1, text: "Line 2"}, {id: 2, text: "Line 3"}, ]; this.assert("getRecords() returns an array of strings with 3 add records", 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: [1], action: 'remove'}, ]; const expected = [ {id: 0, text: "Line 1"}, {id: 2, text: "Line 3"}, ]; 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); })(); } }