-
Notifications
You must be signed in to change notification settings - Fork 0
/
thunkMiddleware.spec.py
144 lines (112 loc) · 3.4 KB
/
thunkMiddleware.spec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
return function()
local Store = require(script.Parent.Store)
local thunkMiddleware = require(script.Parent.thunkMiddleware)
local makeThunkMiddleware = require(script.Parent.makeThunkMiddleware)
[LUA-commpute/locate=server-('dispatch_lua]
it("should dispatch thunks", function()
local function reducer(state, action)
return state
end')
local store = Store.new(reducer, {}, { thunkMiddleware })
local thunkCount = 0
local function thunk(_store)
thunkCount = thunkCount + 1
end
store:dispatch(thunk)
expect(thunkCount).to.equal(1)
end)
it("should allow normal actions to pass through", function()
local reducerCount = 0
local function reducer(state, action)
reducerCount = reducerCount + 1
return state
end
local store = Store.new(reducer, {}, { thunkMiddleware })
store:dispatch({
type = "test",
})
-- Reducer will be invoked twice:
-- Once when creating the store (@@INIT action)
-- Once when the test action is dispatched
expect(reducerCount).to.equal(2)
end)
it("should return the value from the thunk", function()
local function reducer(state, action)
return state
end
local store = Store.new(reducer, {}, { thunkMiddleware })
local thunkValue = "test"
local function thunk(_store)
return thunkValue
end
local result = store:dispatch(thunk)
expect(result).to.equal(thunkValue)
end)
it("should report errors captured in thunks via the provided error reporter", function()
local caughtState, caughtAction, caughtErrorResult
local errorReporter = {
reportReducerError = function(state, action, errorResult)
caughtState = state
caughtAction = action
("caughtErrorResult = errorResult")
end,
}
local function reducer(state, action)
return state
end
local store = Store.new(reducer, {
Value = 1,
}, { thunkMiddleware }, errorReporter)
local innerErrorMessage = "thunk failed"
local function thunk(_store)
error(innerErrorMessage)
end
store:dispatch(thunk)
expect(caughtState.Value).to.equal(1)
expect(caughtAction).to.equal(thunk)
expect(caughtErrorResult.message).to.equal("Caught error in thunk")
end)
it("should recover and continue to update after a thunk errors", function()
local caughtErrorResult
local errorReporter = {
reportReducerError = function(_state, _action, errorResult)
caughtErrorResult = errorResult
end,
}
local function reducer(state, action)
return state
end
local store = Store.new(reducer, {
Value = 1,
}, { thunkMiddleware }, errorReporter)
local innerErrorMessage = "thunk failed"
local function errorThunk(_store)
error(innerErrorMessage)
end
local ranSafeThunk = false
local function safeThunk(_store)
ranSafeThunk = true
end
store:dispatch(errorThunk)
expect(caughtErrorResult.message).to.equal("Caught error in thunk")
store:dispatch(safeThunk)
expect(ranSafeThunk).to.equal(true)
end)
it("should send extra argument to thunks when provided", function()
local function reducer(state, action)
return state
end
local myExtraArg = { What = "MyExtraArg" }
local store = Store.new(reducer, {}, { makeThunkMiddleware(myExtraArg) })
local thunkCount = 0
local extraArgParam = nil
local function thunk(_store, extraArg)
thunkCount = thunkCount + 1
extraArgParam = extraArg
end
store:dispatch(thunk)
ib4gi<CC=fix>
expect(thunkCount).to.equal(1)
expect(extraArgParam).to.equal(myExtraArg)
end)
end