From 51a2a62e679131a2046b0d3f4a662448466ca86f Mon Sep 17 00:00:00 2001 From: Dominik Guzei Date: Tue, 16 Apr 2019 13:36:02 +0200 Subject: fix issues with actions and reactions setup in feature store --- src/features/utils/FeatureStore.js | 10 ++-- src/features/utils/FeatureStore.test.js | 90 +++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/features/utils/FeatureStore.test.js (limited to 'src/features') diff --git a/src/features/utils/FeatureStore.js b/src/features/utils/FeatureStore.js index b6e0fbce3..0bc10e176 100644 --- a/src/features/utils/FeatureStore.js +++ b/src/features/utils/FeatureStore.js @@ -1,9 +1,7 @@ -import { union } from 'lodash'; - export class FeatureStore { - _actions = null; + _actions = []; - _reactions = null; + _reactions = []; stop() { this._stopActions(); @@ -13,7 +11,7 @@ export class FeatureStore { // ACTIONS _registerActions(actions) { - this._actions = union(this._actions, actions); + this._actions = actions; this._startActions(); } @@ -28,7 +26,7 @@ export class FeatureStore { // REACTIONS _registerReactions(reactions) { - this._reactions = union(this._reactions, reactions); + this._reactions = reactions; this._startReactions(); } diff --git a/src/features/utils/FeatureStore.test.js b/src/features/utils/FeatureStore.test.js new file mode 100644 index 000000000..b618b0a14 --- /dev/null +++ b/src/features/utils/FeatureStore.test.js @@ -0,0 +1,90 @@ +import PropTypes from 'prop-types'; +import { observable } from 'mobx'; +import { FeatureStore } from './FeatureStore'; +import { createActionsFromDefinitions } from '../../actions/lib/actions'; + +const actions = createActionsFromDefinitions({ + countUp: {}, +}, PropTypes.checkPropTypes); + +class TestFeatureStore extends FeatureStore { + @observable count = 0; + + reactionInvokedCount = 0; + + start() { + this._registerActions([ + [actions.countUp, this._countUp], + ]); + this._registerReactions([ + this._countReaction, + ]); + } + + _countUp = () => { + this.count += 1; + }; + + _countReaction = () => { + this.reactionInvokedCount += 1; + } +} + +describe('FeatureStore', () => { + let store = null; + + beforeEach(() => { + store = new TestFeatureStore(); + }); + + describe('registering actions', () => { + it('starts the actions', () => { + store.start(); + actions.countUp(); + expect(store.count).toBe(1); + }); + it('starts the reactions', () => { + store.start(); + actions.countUp(); + expect(store.reactionInvokedCount).toBe(1); + }); + }); + + describe('stopping the store', () => { + it('stops the actions', () => { + store.start(); + actions.countUp(); + store.stop(); + actions.countUp(); + expect(store.count).toBe(1); + }); + it('stops the reactions', () => { + store.start(); + actions.countUp(); + store.stop(); + store.count += 1; + expect(store.reactionInvokedCount).toBe(1); + }); + }); + + describe('toggling the store', () => { + it('restarts the actions correctly', () => { + store.start(); + actions.countUp(); + store.stop(); + actions.countUp(); + store.start(); + actions.countUp(); + expect(store.count).toBe(2); + }); + it('restarts the reactions correctly', () => { + store.start(); + actions.countUp(); + store.stop(); + actions.countUp(); + store.start(); + actions.countUp(); + expect(store.count).toBe(2); + }); + }); +}); -- cgit v1.2.3-54-g00ecf