Skip to content

Commit c88d666

Browse files
Make tests less flaky (#86)
1 parent ad37c6b commit c88d666

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

Sources/Fluxor/Store.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ open class Store<State, Environment>: ObservableObject {
3131
/// The environment passed to the `Effects`. The `Environment` can contain services and other dependencies.
3232
public let environment: Environment
3333
internal private(set) var stateHash = UUID()
34-
private var stateHashSink: AnyCancellable!
3534
private let actions = PassthroughSubject<Action, Never>()
3635
private var reducers = [KeyedReducer<State>]()
3736
private var effects = [String: [AnyCancellable]]()
@@ -49,7 +48,6 @@ open class Store<State, Environment>: ObservableObject {
4948
public init(initialState: State, environment: Environment, reducers: [Reducer<State>] = []) {
5049
state = initialState
5150
self.environment = environment
52-
stateHashSink = $state.sink { _ in self.stateHash = UUID() }
5351
reducers.forEach(register(reducer:))
5452
}
5553

@@ -68,6 +66,7 @@ open class Store<State, Environment>: ObservableObject {
6866
let oldState = state
6967
var newState = oldState
7068
reducers.forEach { $0.reduce(&newState, action) }
69+
stateHash = UUID()
7170
state = newState
7271
interceptors.forEach { $0.actionDispatched(action: action, oldState: oldState, newState: newState) }
7372
actions.send(action)

Sources/FluxorTestSupport/MockStore.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ public class MockStore<State, Environment>: Store<State, Environment> {
3232
- Parameter effects: The `Effect`s to register
3333
*/
3434
public override init(initialState: State, environment: Environment, reducers: [Reducer<State>] = []) {
35-
let reducers = reducers + [Reducer(ReduceOn(setState) { state, action in
36-
state = action.payload
37-
})]
35+
let reducers = reducers + [Reducer(ReduceOn(setState) { state, action in state = action.payload })]
3836
super.init(initialState: initialState, environment: environment, reducers: reducers)
3937
super.register(interceptor: self.testInterceptor)
4038
}

Tests/FluxorSwiftUITests/ObservableValueTests.swift

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ import XCTest
1111
class ObservableValueTests: XCTestCase {
1212
private let counterSelector = Selector(keyPath: \TestState.counter)
1313
private let increment = ActionTemplate(id: "Increment", payloadType: Int.self)
14-
private lazy var store = Store(initialState: TestState(), reducers: [
15-
Reducer<TestState>(
16-
ReduceOn(increment) { state, action in
17-
state.counter += action.payload
18-
}
19-
)
20-
])
14+
private var store: Store<TestState, Void>!
15+
16+
override func setUp() {
17+
super.setUp()
18+
store = Store(initialState: .init(), reducers: [
19+
Reducer<TestState>(
20+
ReduceOn(increment) { state, action in
21+
state.counter += action.payload
22+
}
23+
)
24+
])
25+
}
2126

2227
func testBindingWithOneActionTemplate() {
2328
// Given

Tests/FluxorTests/MockStoreTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class MockStoreTests: XCTestCase {
5353
// When
5454
store.setState(newState: TestState(counter: 123))
5555
// Then
56+
XCTAssertEqual(store.selectCurrent(selector), value)
5657
wait(for: [expectation1], timeout: 5)
57-
XCTAssertNotNil(cancellable1)
58+
cancellable1.cancel()
5859

5960
// Given
6061
let newValue = 42

0 commit comments

Comments
 (0)