-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_middleware.py
175 lines (118 loc) · 3.94 KB
/
test_middleware.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# ruff: noqa: D100, D101, D102, D103, D104, D107
from __future__ import annotations
from dataclasses import replace
import pytest
from immutable import Immutable
from redux.basic_types import (
BaseAction,
BaseEvent,
CompleteReducerResult,
FinishAction,
FinishEvent,
InitAction,
InitializationActionError,
ReducerResult,
StoreOptions,
)
from redux.main import Store
class StateType(Immutable):
value: int
class IncrementAction(BaseAction): ...
class DecrementAction(BaseAction): ...
class SomeEvent(BaseEvent): ...
Action = IncrementAction | DecrementAction | InitAction | FinishAction
def reducer(
state: StateType | None,
action: Action,
) -> StateType | ReducerResult[StateType, Action, SomeEvent | FinishEvent]:
if state is None:
if isinstance(action, InitAction):
return StateType(value=0)
raise InitializationActionError(action)
if isinstance(action, IncrementAction):
return CompleteReducerResult(
state=replace(state, value=state.value + 1),
events=[SomeEvent()],
)
if isinstance(action, DecrementAction):
return replace(state, value=state.value - 1)
return state
class StoreType(Store[StateType, Action, FinishEvent | SomeEvent]):
@property
def state(self: StoreType) -> StateType | None:
return self._state
@pytest.fixture
def store() -> StoreType:
return StoreType(reducer, options=StoreOptions(auto_init=True))
def test_identity_action_middleware(store: StoreType) -> None:
calls = []
def middleware(action: Action) -> Action:
calls.append(action)
if isinstance(action, IncrementAction):
return DecrementAction()
return action
store.register_action_middleware(middleware)
actions = [
IncrementAction(),
IncrementAction(),
FinishAction(),
]
def check() -> None:
assert calls == actions
assert store.state
assert store.state.value == -2
store.subscribe_event(FinishEvent, check)
for action in actions:
store.dispatch(action)
def test_cancelling_action_middleware(store: StoreType) -> None:
calls = []
def middleware(action: Action) -> Action | None:
calls.append(action)
if len(calls) == 1:
return None
return action
store.register_action_middleware(middleware)
actions = [
IncrementAction(),
IncrementAction(),
FinishAction(),
]
def check() -> None:
assert store.state
assert store.state.value == 1
store.subscribe_event(FinishEvent, check)
for action in actions:
store.dispatch(action)
def test_identity_event_middlewares(store: StoreType) -> None:
calls = []
def middleware(event: SomeEvent) -> SomeEvent | FinishEvent:
calls.append(event)
if len(calls) == 2:
return FinishEvent()
return event
store.register_event_middleware(middleware)
actions = [IncrementAction()] * 3
def check() -> None:
assert calls == [SomeEvent()] * 3
store.subscribe_event(FinishEvent, check)
for action in actions:
store.dispatch(action)
def test_cancelling_event_middlewares(store: StoreType) -> None:
calls = []
def middleware(event: SomeEvent | FinishEvent) -> SomeEvent | FinishEvent | None:
calls.append(event)
if len(calls) == 1 and isinstance(event, SomeEvent):
return None
return event
side_effect_calls = []
def some_side_effect(event: SomeEvent) -> None:
side_effect_calls.append(event)
store.register_event_middleware(middleware)
actions = [IncrementAction()] * 2
def check() -> None:
assert side_effect_calls == actions[1:2]
store.subscribe_event(SomeEvent, some_side_effect)
store.subscribe_event(FinishEvent, check)
for action in actions:
store.dispatch(action)
store.dispatch(FinishAction())