-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
123 lines (97 loc) · 3.04 KB
/
middleware_test.go
File metadata and controls
123 lines (97 loc) · 3.04 KB
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
package godux_test
import (
. "github.com/zpencerq/godux"
. "github.com/zpencerq/godux/middleware"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Middleware", func() {
Describe("Apply", func() {
It("Wraps dispatch method with middleware once", func() {
fn := func(args ...interface{}) {}
spy := MakeSpy(fn, &fn)
test := func(mc *MiddlewareContext) func(Dispatcher) Dispatcher {
fn(mc)
return func(dispatcher Dispatcher) Dispatcher {
return func(action *Action) *Action {
return dispatcher(action)
}
}
}
store := Apply(test, Thunk)(NewStore)(&StoreInput{Reducer: Reducers["todos"]})
store.Dispatch(AddTodo("Use Redux"))
store.Dispatch(AddTodo("Flux FTW!"))
Expect(spy.Calls).To(HaveLen(1))
Expect(store.State()).To(Equal([]Todo{
{Id: 1, Text: "Use Redux"},
{Id: 2, Text: "Flux FTW!"},
}))
})
It("Passed recursive dispatches through the middleware chain", func(done Done) {
var nextSpy *Spy
test := CreateSimpleMiddleware(func(mc *MiddlewareContext, next Dispatcher, action *Action) *Action {
if nextSpy == nil {
nextSpy = MakeSpy(next, &next)
}
return next(action)
})
thunk := CreateSimpleMiddleware(func(mc *MiddlewareContext, next Dispatcher, action *Action) *Action {
switch a := action.Value.(type) {
case func(*MiddlewareContext) *Action:
return a(mc)
default:
return next(action)
}
})
store := Apply(test, thunk)(NewStore)(&StoreInput{Reducer: Reducers["todos"]})
go func() {
if promise, ok := store.Dispatch(AddTodoAsync("Use Redux")).Value.(FutureAction); ok {
<-promise
Expect(nextSpy.Calls).To(HaveLen(2))
}
close(done)
}()
})
It("Works with thunk middleware", func(done Done) {
store := Apply(Thunk)(NewStore)(&StoreInput{Reducer: Reducers["todos"]})
store.Dispatch(AddTodoIfEmpty("Hello"))
Expect(store.State()).To(Equal([]Todo{
{Id: 1, Text: "Hello"},
}))
store.Dispatch(AddTodoIfEmpty("Hello"))
Expect(store.State()).To(Equal([]Todo{
{Id: 1, Text: "Hello"},
}))
store.Dispatch(AddTodo("World"))
Expect(store.State()).To(Equal([]Todo{
{Id: 1, Text: "Hello"},
{Id: 2, Text: "World"},
}))
go func() {
if promise, ok := store.Dispatch(AddTodoAsync("Maybe")).Value.(FutureAction); ok {
<-promise
Expect(store.State()).To(Equal([]Todo{
{Id: 1, Text: "Hello"},
{Id: 2, Text: "World"},
{Id: 3, Text: "Maybe"},
}))
}
close(done)
}()
})
It("Uses contextual dispatch when none is given", func() {
dispatch := func(action *Action) *Action { return action }
getState := func() State { return "hi" }
mc := &MiddlewareContext{getState, dispatch}
var nextSpy *Spy
test := CreateSimpleMiddleware(func(mc *MiddlewareContext, next Dispatcher, action *Action) *Action {
if nextSpy == nil {
nextSpy = MakeSpy(next, &next)
}
return next(action)
})
Expect(test(mc)(nil)(AddTodo("Hi"))).To(Equal(AddTodo("Hi")))
Expect(nextSpy.Calls).To(HaveLen(1))
})
})
})