Skip to content

Commit b14be97

Browse files
committed
Add tests for reactor options
1 parent 99b55b7 commit b14be97

File tree

5 files changed

+94
-19
lines changed

5 files changed

+94
-19
lines changed

src/reactor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import {
2323
*/
2424
class Reactor {
2525
constructor(config = {}) {
26-
const baseOptions = config.debug ? DEBUG_OPTIONS : PROD_OPTIONS
26+
const debug = !!config.debug
27+
const baseOptions = debug ? DEBUG_OPTIONS : PROD_OPTIONS
2728
const initialReactorState = new ReactorState({
28-
debug: config.debug,
29+
debug: debug,
2930
// merge config options with the defaults
3031
options: baseOptions.merge(config.options || {}),
3132
})

src/reactor/fns.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ export function addObserver(observerState, getter, handler) {
192192
* @return {Boolean}
193193
*/
194194
export function getOption(reactorState, option) {
195-
return reactorState.getIn(['options', option], false)
195+
const value = reactorState.getIn(['options', option])
196+
if (value === undefined) {
197+
throw new Error('Invalid option: ' + option)
198+
}
199+
return value
196200
}
197201

198202
/**

src/reactor/records.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import { Map, Set, Record } from 'immutable'
22

3+
export const PROD_OPTIONS = Map({
4+
// logs information for each dispatch
5+
logDispatches: false,
6+
// log the entire app state after each dispatch
7+
logAppState: false,
8+
// logs what stores changed after a dispatch
9+
logDirtyStores: false,
10+
// if false, throw an error if a store returns undefined
11+
allowUndefinedDispatch: true,
12+
// if false, throw an error if a store returns undefined
13+
allowNonImmutableStores: true,
14+
// if false throw when dispatching in dispatch
15+
allowDispatchInDispatch: true,
16+
})
17+
318
export const ReactorState = Record({
419
dispatchId: 0,
520
state: Map(),
@@ -10,7 +25,7 @@ export const ReactorState = Record({
1025
dirtyStores: Set(),
1126
debug: false,
1227
// production defaults
13-
options: Map({}),
28+
options: PROD_OPTIONS,
1429
})
1530

1631
export const ObserverState = Record({
@@ -39,17 +54,3 @@ export const DEBUG_OPTIONS = Map({
3954
allowDispatchInDispatch: false,
4055
})
4156

42-
export const PROD_OPTIONS = Map({
43-
// logs information for each dispatch
44-
logDispatches: false,
45-
// log the entire app state after each dispatch
46-
logAppState: false,
47-
// logs what stores changed after a dispatch
48-
logDirtyStores: false,
49-
// if false, throw an error if a store returns undefined
50-
allowUndefinedDispatch: true,
51-
// if false, throw an error if a store returns undefined
52-
allowNonImmutableStores: true,
53-
// if false throw when dispatching in dispatch
54-
allowDispatchInDispatch: true,
55-
})

tests/reactor-fns-tests.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Map, Set, is } from 'immutable'
33
import { Store } from '../src/main'
44
import * as fns from '../src/reactor/fns'
5-
import { ReactorState, ObserverState } from '../src/reactor/records'
5+
import { ReactorState, ObserverState, DEBUG_OPTIONS } from '../src/reactor/records'
66
import { toImmutable } from '../src/immutable-helpers'
77

88
describe('reactor fns', () => {
@@ -501,5 +501,29 @@ describe('reactor fns', () => {
501501
})
502502
})
503503
})
504+
describe('#getDebugOption', () => {
505+
it('should parse the option value in a reactorState', () => {
506+
const reactorState = new ReactorState({
507+
options: Map({
508+
allowUndefinedDispatch: true,
509+
}),
510+
})
511+
512+
const result = fns.getOption(reactorState, 'allowUndefinedDispatch')
513+
expect(result).toBe(true)
514+
})
515+
516+
it('should throw an error if the option doesn\'t', () => {
517+
const reactorState = new ReactorState({
518+
options: Map({
519+
allowUndefinedDispatch: true,
520+
}),
521+
})
522+
523+
expect(function() {
524+
const result = fns.getOption(reactorState, 'unknownOption')
525+
}).toThrow()
526+
})
527+
})
504528
})
505529
/*eslint-enable one-var, comma-dangle*/

tests/reactor-tests.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Immutable, { Map, List, is } from 'immutable'
22
import { Reactor, Store } from '../src/main'
3+
import { getOption } from '../src/reactor/fns'
34
import { toImmutable } from '../src/immutable-helpers'
5+
import { PROD_OPTIONS, DEBUG_OPTIONS } from '../src/reactor/records'
46
import logging from '../src/logging'
57

68
describe('Reactor', () => {
@@ -9,6 +11,49 @@ describe('Reactor', () => {
911
expect(reactor instanceof Reactor).toBe(true)
1012
})
1113

14+
describe('debug and options flags', () => {
15+
it('should create a reactor with PROD_OPTIONS', () => {
16+
var reactor = new Reactor()
17+
expect(reactor.reactorState.get('debug')).toBe(false)
18+
expect(is(reactor.reactorState.get('options'), PROD_OPTIONS)).toBe(true)
19+
})
20+
it('should create a reactor with DEBUG_OPTIONS', () => {
21+
var reactor = new Reactor({
22+
debug: true,
23+
})
24+
expect(reactor.reactorState.get('debug')).toBe(true)
25+
expect(is(reactor.reactorState.get('options'), DEBUG_OPTIONS)).toBe(true)
26+
})
27+
it('should override PROD options', () => {
28+
var reactor = new Reactor({
29+
options: {
30+
logDispatches: true,
31+
}
32+
})
33+
expect(getOption(reactor.reactorState, 'logDispatches')).toBe(true)
34+
expect(getOption(reactor.reactorState, 'logAppState')).toBe(false)
35+
expect(getOption(reactor.reactorState, 'logDirtyStores')).toBe(false)
36+
expect(getOption(reactor.reactorState, 'allowUndefinedDispatch')).toBe(true)
37+
expect(getOption(reactor.reactorState, 'allowNonImmutableStores')).toBe(true)
38+
expect(getOption(reactor.reactorState, 'allowDispatchInDispatch')).toBe(true)
39+
})
40+
it('should override DEBUG options', () => {
41+
var reactor = new Reactor({
42+
debug: true,
43+
options: {
44+
logDispatches: false,
45+
allowDispatchInDispatch: true,
46+
}
47+
})
48+
expect(getOption(reactor.reactorState, 'logDispatches')).toBe(false)
49+
expect(getOption(reactor.reactorState, 'logAppState')).toBe(true)
50+
expect(getOption(reactor.reactorState, 'logDirtyStores')).toBe(true)
51+
expect(getOption(reactor.reactorState, 'allowUndefinedDispatch')).toBe(false)
52+
expect(getOption(reactor.reactorState, 'allowNonImmutableStores')).toBe(false)
53+
expect(getOption(reactor.reactorState, 'allowDispatchInDispatch')).toBe(true)
54+
})
55+
})
56+
1257
describe('Reactor with no initial state', () => {
1358
var checkoutActions
1459
var reactor

0 commit comments

Comments
 (0)