@@ -3,13 +3,18 @@ import Combine
33
44import RxStore
55
6- enum CounterAction : RxStore . Action {
7- case Increment
8- case Decrement
9- case Dummy
6+ enum CounterAction {
7+ struct Increment : RxStoreAction { }
8+ struct Decrement : RxStoreAction { }
9+
1010}
1111
1212final class RxStoreTests : XCTestCase {
13+ var subscriptions = Set < AnyCancellable > ( )
14+
15+ override func tearDown( ) {
16+ subscriptions = [ ]
17+ }
1318 func testExampleWithCounter( ) {
1419 // This is an example of a functional test case.
1520 // Use XCTAssert and related functions to verify your tests produce the correct
@@ -23,9 +28,9 @@ final class RxStoreTests: XCTestCase {
2328
2429 let reducer : AppStore . Reducer < Int > = { state, action in
2530 switch action {
26- case CounterAction . Increment:
31+ case _ as CounterAction . Increment :
2732 return state + 1
28- case CounterAction . Decrement:
33+ case _ as CounterAction . Decrement :
2934 return state - 1
3035 default :
3136 return state
@@ -35,35 +40,14 @@ final class RxStoreTests: XCTestCase {
3540 let store = AppStore ( ) . registerReducer ( for: \. counterState, reducer)
3641 . initialize ( )
3742
38- store. dispatch ( action: CounterAction . Increment)
43+ store. dispatch ( action: CounterAction . Increment ( ) )
3944 let _ = store. counterState. sink ( receiveValue: { value in
4045 XCTAssertEqual ( value, 1 )
4146 } )
4247
4348
4449 }
4550
46- func testEmptyActionsIgnored( ) {
47- class TestStore : RxStore {
48- let emptyState = RxStore . State ( false )
49- }
50-
51- let store = TestStore ( ) . registerReducer ( for: \. emptyState, { state, action in
52- if case RxStoreActions . Empty = action {
53- return true
54- }
55- return false
56- } ) . initialize ( )
57-
58- enum Action : RxStore . Action {
59- case first
60- }
61- let _ = store. emptyState. sink ( receiveValue: { state in
62- XCTAssertEqual ( state, false )
63- } )
64- store. dispatch ( action: RxStoreActions . Empty)
65- store. dispatch ( action: Action . first)
66- }
6751
6852 func testEffects( ) {
6953 struct Todo : Codable , Equatable {
@@ -115,12 +99,9 @@ final class RxStoreTests: XCTestCase {
11599 }
116100
117101
118-
119102 let loadTodosEffect = AppStore . createEffect ( Action . LoadTodos. self) { store, action in
120103 mockGetTodosFromServer ( )
121- . map {
122- Action . LoadTodosSuccess ( $0)
123- }
104+ . map { Action . LoadTodosSuccess ( $0) }
124105 . replaceError ( with: Action . LoadTodosFailure ( ) )
125106 . eraseToAnyPublisher ( )
126107 }
@@ -129,12 +110,20 @@ final class RxStoreTests: XCTestCase {
129110 . registerReducer ( for: \. todosState, todoReducer)
130111 . registerEffects ( [ loadTodosEffect] )
131112 . initialize ( )
132-
113+ var actions : [ RxStoreAction ] = [ ]
114+ store. stream. prefix ( 2 ) . sink ( receiveCompletion: { _ in
115+ XCTAssertTrue ( actions [ 0 ] is Action . LoadTodos )
116+ XCTAssertTrue ( actions [ 1 ] is Action . LoadTodosSuccess )
117+ } , receiveValue: { action in
118+ actions. append ( action)
119+ } ) . store ( in: & subscriptions)
120+
133121 store. dispatch ( action: Action . LoadTodos ( ) )
122+
134123 let _ = store. todosState. sink ( receiveValue: { state in
135124 XCTAssertEqual ( state, [ mockTodo. id: mockTodo] )
136125 } )
137-
126+
138127 }
139128
140129 func testSelector( ) {
@@ -143,15 +132,17 @@ final class RxStoreTests: XCTestCase {
143132 var userTodoIds = RxStore . State < Dictionary < Int , [ Int ] > > ( [ userId: [ mockTodo. id] , userId2: [ mockTodo2. id] ] )
144133 var counter = RxStore . State ( 0 )
145134 }
146- enum Action : RxStore . Action {
147- case AddTodo( Todo )
135+ enum Action {
136+ struct AddTodo : RxStoreAction {
137+ let todo : Todo
138+ }
148139 }
149140
150141 func counterReducer( _ state: Int , action: RxStore . Action ) -> Int {
151142 switch action {
152- case CounterAction . Increment:
143+ case _ as CounterAction . Increment :
153144 return state + 1
154- case CounterAction . Decrement:
145+ case _ as CounterAction . Decrement :
155146 return state - 1
156147 default :
157148 return state
@@ -160,9 +151,9 @@ final class RxStoreTests: XCTestCase {
160151
161152 let store = AppStore ( )
162153 . registerReducer ( for: \. todos, { state, action in
163- if case Action . AddTodo ( let todo ) = action {
154+ if let action = action as? Action . AddTodo {
164155 var newState = state
165- newState. append ( todo)
156+ newState. append ( action . todo)
166157 return newState
167158 }
168159 return state
@@ -177,12 +168,13 @@ final class RxStoreTests: XCTestCase {
177168 return userTodos
178169 }
179170 }
180-
181- store. dispatch ( action: Action . AddTodo ( mockTodo2) )
182- let _ = store. select ( getTodosForSelectedUser ( userId2) ) . sink { userTodos in
183- XCTAssertEqual ( userTodos, [ mockTodo2] )
184- }
185171
172+ let _ = store. select ( getTodosForSelectedUser ( userId2) ) . prefix ( 2 ) . collect ( )
173+ . sink { todos in
174+ XCTAssert ( todos == [ [ ] , [ mockTodo2] ] as [ [ Todo ] ] , " failed " )
175+ } . store ( in: & subscriptions)
176+
177+ store. dispatch ( action: Action . AddTodo ( todo: mockTodo2) )
186178 }
187179
188180 static var allTests = [
0 commit comments