From 3e0ec2f09f71f56eba75d7695015ae7aaca70fe7 Mon Sep 17 00:00:00 2001 From: pkupczyk <piotr.kupczyk@id.ethz.ch> Date: Sun, 29 Dec 2019 15:56:43 +0100 Subject: [PATCH] SSDM-7583 : ObjectTypeForm - unit tests for change handler --- .../srcTest/js/common/ComponentState.js | 34 ++++++ .../srcTest/js/common/componentState.js | 13 --- .../ObjectTypeHandlerAddProperty.test.js | 24 ++-- .../ObjectTypeHandlerAddSection.test.js | 32 +++--- .../ObjectTypeHandlerChange.test.js | 104 ++++++++++++++++++ 5 files changed, 166 insertions(+), 41 deletions(-) create mode 100644 openbis_ng_ui/srcTest/js/common/ComponentState.js delete mode 100644 openbis_ng_ui/srcTest/js/common/componentState.js create mode 100644 openbis_ng_ui/srcTest/js/components/types/objectType/ObjectTypeHandlerChange.test.js 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 00000000000..1996729c244 --- /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 b7d4dab8ab8..00000000000 --- 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 ce462d951d6..7c959e52ac0 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 9e269b86df5..c1df431ef5b 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 00000000000..dabb4921ebe --- /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) +} -- GitLab