diff --git a/openbis_ng_ui/srcTest/js/common/ComponentState.js b/openbis_ng_ui/srcTest/js/common/ComponentState.js new file mode 100644 index 0000000000000000000000000000000000000000..1996729c2449cfab294b380a62a42ed8081034d3 --- /dev/null +++ b/openbis_ng_ui/srcTest/js/common/ComponentState.js @@ -0,0 +1,34 @@ +import _ from 'lodash' + +class ComponentState { + constructor(initialState) { + this.initialState = initialState + this.initialStateCopy = _.cloneDeep(initialState) + this.state = initialState + } + + getState() { + return this.state + } + + getSetState() { + return newStateOrFunction => { + let changes + + if (_.isFunction(newStateOrFunction)) { + changes = newStateOrFunction(this.state) + } else { + changes = newStateOrFunction + } + + this.state = { ...this.state, ...changes } + } + } + + assertState(expectedState) { + expect(this.state).toEqual(expectedState) + expect(this.initialState).toEqual(this.initialStateCopy) + } +} + +export default ComponentState diff --git a/openbis_ng_ui/srcTest/js/common/componentState.js b/openbis_ng_ui/srcTest/js/common/componentState.js deleted file mode 100644 index b7d4dab8ab8e78578f44b3e3832ca58559d2a196..0000000000000000000000000000000000000000 --- a/openbis_ng_ui/srcTest/js/common/componentState.js +++ /dev/null @@ -1,13 +0,0 @@ -import _ from 'lodash' - -const createSetState = state => { - return newStateOrFunction => { - if (_.isFunction(newStateOrFunction)) { - _.assign(state, newStateOrFunction(state)) - } else { - _.assign(state, newStateOrFunction) - } - } -} - -export default { createSetState } diff --git a/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerAddProperty.test.js b/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerAddProperty.test.js index ce462d951d6f533d7afa6b0bc3f4db5a4c0b02c3..7c959e52ac08617719a3c2050f07f3315274b8d5 100644 --- a/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerAddProperty.test.js +++ b/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerAddProperty.test.js @@ -1,9 +1,9 @@ import ObjectTypeHandlerAddProperty from '../../../../../src/js/components/types/objectType/ObjectTypeHandlerAddProperty.js' -import componentState from '../../../common/componentState.js' +import ComponentState from '../../../common/ComponentState.js' describe('ObjectTypeHandlerAddPropertyTest', () => { test('add with a section selected', () => { - let state = { + const componentState = new ComponentState({ selection: { type: 'section', params: { id: 'section-0' } @@ -20,11 +20,11 @@ describe('ObjectTypeHandlerAddPropertyTest', () => { ], properties: [{ id: 'property-9', section: 'section-0' }], propertiesCounter: 10 - } + }) - execute(state) + execute(componentState) - expect(state).toEqual({ + componentState.assertState({ selection: { type: 'property', params: { id: 'property-10' } @@ -61,7 +61,7 @@ describe('ObjectTypeHandlerAddPropertyTest', () => { }) test('add with a property selected', () => { - let state = { + const componentState = new ComponentState({ selection: { type: 'property', params: { id: 'property-0' } @@ -82,11 +82,11 @@ describe('ObjectTypeHandlerAddPropertyTest', () => { { id: 'property-2', section: 'section-1' } ], propertiesCounter: 3 - } + }) - execute(state) + execute(componentState) - expect(state).toEqual({ + componentState.assertState({ selection: { type: 'property', params: { id: 'property-3' } @@ -125,9 +125,9 @@ describe('ObjectTypeHandlerAddPropertyTest', () => { }) }) -const execute = state => { +const execute = componentState => { new ObjectTypeHandlerAddProperty( - state, - componentState.createSetState(state) + componentState.getState(), + componentState.getSetState() ).execute() } diff --git a/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerAddSection.test.js b/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerAddSection.test.js index 9e269b86df529b1a1330109fc7398a8d5514e187..c1df431ef5b001f4478a313c97930cbc85062680 100644 --- a/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerAddSection.test.js +++ b/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerAddSection.test.js @@ -1,16 +1,16 @@ import ObjectTypeHandlerAddSection from '../../../../../src/js/components/types/objectType/ObjectTypeHandlerAddSection.js' -import componentState from '../../../common/componentState.js' +import ComponentState from '../../../common/ComponentState.js' describe('ObjectTypeHandlerAddSectionTest', () => { test('add with nothing selected', () => { - let state = { + const componentState = new ComponentState({ sections: [], sectionsCounter: 10 - } + }) - execute(state) + execute(componentState) - expect(state).toEqual({ + componentState.assertState({ selection: { type: 'section', params: { @@ -29,7 +29,7 @@ describe('ObjectTypeHandlerAddSectionTest', () => { }) test('add with a section selected', () => { - let state = { + const componentState = new ComponentState({ selection: { type: 'section', params: { id: 'section-0' } @@ -46,11 +46,11 @@ describe('ObjectTypeHandlerAddSectionTest', () => { ], properties: [{ id: 'property-9', section: 'section-0' }], sectionsCounter: 2 - } + }) - execute(state) + execute(componentState) - expect(state).toEqual({ + componentState.assertState({ selection: { type: 'section', params: { id: 'section-2' } @@ -76,7 +76,7 @@ describe('ObjectTypeHandlerAddSectionTest', () => { }) test('add with a property selected', () => { - let state = { + const componentState = new ComponentState({ selection: { type: 'property', params: { id: 'property-0' } @@ -97,11 +97,11 @@ describe('ObjectTypeHandlerAddSectionTest', () => { { id: 'property-2', section: 'section-1' } ], sectionsCounter: 2 - } + }) - execute(state) + execute(componentState) - expect(state).toEqual({ + componentState.assertState({ selection: { type: 'section', params: { id: 'section-2' } @@ -131,9 +131,9 @@ describe('ObjectTypeHandlerAddSectionTest', () => { }) }) -const execute = state => { +const execute = componentState => { new ObjectTypeHandlerAddSection( - state, - componentState.createSetState(state) + componentState.getState(), + componentState.getSetState() ).execute() } diff --git a/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerChange.test.js b/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerChange.test.js new file mode 100644 index 0000000000000000000000000000000000000000..dabb4921ebe83cd5a7a37217ac6c1a209d6dd603 --- /dev/null +++ b/openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerChange.test.js @@ -0,0 +1,104 @@ +import ObjectTypeHandlerChange from '../../../../../src/js/components/types/objectType/ObjectTypeHandlerChange.js' +import ComponentState from '../../../common/ComponentState.js' + +describe('ObjectTypeHandlerChangeTest', () => { + test('type', () => { + const componentState = new ComponentState({ + type: { + field1: 'value1', + field2: 'value2' + } + }) + + execute(componentState, 'type', { + field: 'field2', + value: 'value2 changed' + }) + + componentState.assertState({ + type: { + field1: 'value1', + field2: 'value2 changed' + } + }) + }) + + test('section', () => { + const componentState = new ComponentState({ + sections: [ + { + id: 'section-0', + field1: 'value1', + field2: 'value2' + }, + { + id: 'section-1', + field1: 'value1' + } + ] + }) + + execute(componentState, 'section', { + id: 'section-1', + field: 'field1', + value: 'value1 changed' + }) + + componentState.assertState({ + sections: [ + { + id: 'section-0', + field1: 'value1', + field2: 'value2' + }, + { + id: 'section-1', + field1: 'value1 changed' + } + ] + }) + }) + + test('property', () => { + const componentState = new ComponentState({ + properties: [ + { + id: 'property-0', + field1: 'value1', + field2: 'value2' + }, + { + id: 'property-1', + field1: 'value1' + } + ] + }) + + execute(componentState, 'property', { + id: 'property-1', + field: 'field1', + value: 'value1 changed' + }) + + componentState.assertState({ + properties: [ + { + id: 'property-0', + field1: 'value1', + field2: 'value2' + }, + { + id: 'property-1', + field1: 'value1 changed' + } + ] + }) + }) +}) + +const execute = (componentState, type, params) => { + new ObjectTypeHandlerChange( + componentState.getState(), + componentState.getSetState() + ).execute(type, params) +}