1 | const ReduxStore = require('./index')
|
2 | const Redux = require('redux')
|
3 |
|
4 | describe('ReduxStore', () => {
|
5 | function createStore (reducers = {}) {
|
6 | const reducer = Redux.combineReducers(Object.assign({}, reducers, {
|
7 | uppy: ReduxStore.reducer
|
8 | }))
|
9 | return Redux.createStore(reducer)
|
10 | }
|
11 |
|
12 | it('can be created with or without new', () => {
|
13 | const r = createStore()
|
14 | let store = ReduxStore({ store: r })
|
15 | expect(typeof store).toBe('object')
|
16 | store = new ReduxStore({ store: r })
|
17 | expect(typeof store).toBe('object')
|
18 | })
|
19 |
|
20 | it('merges in state using `setState`', () => {
|
21 | const r = createStore()
|
22 | const store = ReduxStore({ store: r })
|
23 | expect(store.getState()).toEqual({})
|
24 |
|
25 | store.setState({
|
26 | a: 1,
|
27 | b: 2
|
28 | })
|
29 | expect(store.getState()).toEqual({ a: 1, b: 2 })
|
30 |
|
31 | store.setState({ b: 3 })
|
32 | expect(store.getState()).toEqual({ a: 1, b: 3 })
|
33 | })
|
34 |
|
35 | it('notifies subscriptions when state changes', () => {
|
36 | let expected = []
|
37 | let calls = 0
|
38 | function listener (prevState, nextState, patch) {
|
39 | calls++
|
40 | expect([ prevState, nextState, patch ]).toEqual(expected)
|
41 | }
|
42 |
|
43 | const r = createStore()
|
44 | const store = ReduxStore({ store: r })
|
45 | store.subscribe(listener)
|
46 |
|
47 | expected = [{}, { a: 1, b: 2 }, { a: 1, b: 2 }]
|
48 | store.setState({
|
49 | a: 1,
|
50 | b: 2
|
51 | })
|
52 |
|
53 | expected = [{ a: 1, b: 2 }, { a: 1, b: 3 }, { b: 3 }]
|
54 | store.setState({ b: 3 })
|
55 |
|
56 | expect(calls).toBe(2)
|
57 | })
|
58 |
|
59 | it('fires `subscribe` if state is modified externally (eg redux devtools)', () => {
|
60 | const reducer = Redux.combineReducers({ uppy: ReduxStore.reducer })
|
61 | const r = Redux.createStore((state, action) => {
|
62 |
|
63 |
|
64 | if (action.type === 'SET') return action.payload
|
65 | return reducer(state, action)
|
66 | })
|
67 |
|
68 | let expected = []
|
69 | let calls = 0
|
70 | function listener (prevState, nextState, patch) {
|
71 | calls++
|
72 | expect([ prevState, nextState, patch ]).toEqual(expected)
|
73 | }
|
74 |
|
75 | const store = ReduxStore({ store: r })
|
76 | store.subscribe(listener)
|
77 |
|
78 | expected = [{}, { a: 1 }, { a: 1 }]
|
79 | store.setState({ a: 1 })
|
80 |
|
81 | expected = [{ a: 1 }, { b: 2 }, { b: 2 }]
|
82 |
|
83 | r.dispatch({
|
84 | type: 'SET',
|
85 | payload: {
|
86 | uppy: {
|
87 | [store._id]: { b: 2 }
|
88 | }
|
89 | }
|
90 | })
|
91 |
|
92 | expect(calls).toBe(2)
|
93 | })
|
94 |
|
95 | it('can mount in a custom state key', () => {
|
96 | const reducer = Redux.combineReducers({
|
97 | hello: ReduxStore.reducer
|
98 | })
|
99 | const r = Redux.createStore(reducer)
|
100 | const store = ReduxStore({
|
101 | store: r,
|
102 | id: 'world',
|
103 | selector: state => state.hello.world
|
104 | })
|
105 | store.setState({ a: 1 })
|
106 |
|
107 | expect(r.getState()).toEqual({
|
108 | hello: {
|
109 | world: {
|
110 | a: 1
|
111 | }
|
112 | }
|
113 | })
|
114 | })
|
115 | })
|