diff options
Diffstat (limited to 'src/features/utils/FeatureStore.test.js')
-rw-r--r-- | src/features/utils/FeatureStore.test.js | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/features/utils/FeatureStore.test.js b/src/features/utils/FeatureStore.test.js new file mode 100644 index 000000000..92308bf52 --- /dev/null +++ b/src/features/utils/FeatureStore.test.js | |||
@@ -0,0 +1,92 @@ | |||
1 | import PropTypes from 'prop-types'; | ||
2 | import { observable } from 'mobx'; | ||
3 | import { FeatureStore } from './FeatureStore'; | ||
4 | import { createActionsFromDefinitions } from '../../actions/lib/actions'; | ||
5 | import { createActionBindings } from './ActionBinding'; | ||
6 | import { createReactions } from '../../stores/lib/Reaction'; | ||
7 | |||
8 | const actions = createActionsFromDefinitions({ | ||
9 | countUp: {}, | ||
10 | }, PropTypes.checkPropTypes); | ||
11 | |||
12 | class TestFeatureStore extends FeatureStore { | ||
13 | @observable count = 0; | ||
14 | |||
15 | reactionInvokedCount = 0; | ||
16 | |||
17 | start() { | ||
18 | this._registerActions(createActionBindings([ | ||
19 | [actions.countUp, this._countUp], | ||
20 | ])); | ||
21 | this._registerReactions(createReactions([ | ||
22 | this._countReaction, | ||
23 | ])); | ||
24 | } | ||
25 | |||
26 | _countUp = () => { | ||
27 | this.count += 1; | ||
28 | }; | ||
29 | |||
30 | _countReaction = () => { | ||
31 | this.reactionInvokedCount += 1; | ||
32 | } | ||
33 | } | ||
34 | |||
35 | describe('FeatureStore', () => { | ||
36 | let store = null; | ||
37 | |||
38 | beforeEach(() => { | ||
39 | store = new TestFeatureStore(); | ||
40 | }); | ||
41 | |||
42 | describe('registering actions', () => { | ||
43 | it('starts the actions', () => { | ||
44 | store.start(); | ||
45 | actions.countUp(); | ||
46 | expect(store.count).toBe(1); | ||
47 | }); | ||
48 | it('starts the reactions', () => { | ||
49 | store.start(); | ||
50 | actions.countUp(); | ||
51 | expect(store.reactionInvokedCount).toBe(1); | ||
52 | }); | ||
53 | }); | ||
54 | |||
55 | describe('stopping the store', () => { | ||
56 | it('stops the actions', () => { | ||
57 | store.start(); | ||
58 | actions.countUp(); | ||
59 | store.stop(); | ||
60 | actions.countUp(); | ||
61 | expect(store.count).toBe(1); | ||
62 | }); | ||
63 | it('stops the reactions', () => { | ||
64 | store.start(); | ||
65 | actions.countUp(); | ||
66 | store.stop(); | ||
67 | store.count += 1; | ||
68 | expect(store.reactionInvokedCount).toBe(1); | ||
69 | }); | ||
70 | }); | ||
71 | |||
72 | describe('toggling the store', () => { | ||
73 | it('restarts the actions correctly', () => { | ||
74 | store.start(); | ||
75 | actions.countUp(); | ||
76 | store.stop(); | ||
77 | actions.countUp(); | ||
78 | store.start(); | ||
79 | actions.countUp(); | ||
80 | expect(store.count).toBe(2); | ||
81 | }); | ||
82 | it('restarts the reactions correctly', () => { | ||
83 | store.start(); | ||
84 | actions.countUp(); | ||
85 | store.stop(); | ||
86 | actions.countUp(); | ||
87 | store.start(); | ||
88 | actions.countUp(); | ||
89 | expect(store.count).toBe(2); | ||
90 | }); | ||
91 | }); | ||
92 | }); | ||