diff --git a/Sources/ComposableArchitecture/UIKit/IfLetUIKit.swift b/Sources/ComposableArchitecture/UIKit/IfLetUIKit.swift index 7ba05ba..d8c8d14 100644 --- a/Sources/ComposableArchitecture/UIKit/IfLetUIKit.swift +++ b/Sources/ComposableArchitecture/UIKit/IfLetUIKit.swift @@ -44,7 +44,8 @@ extension Store { else: @escaping () -> Void ) -> Disposable where State == Wrapped? { - let elseDisposable = self + let elseDisposable = + self .scope( state: { state in state.distinctUntilChanged({ ($0 != nil) == ($1 != nil) }) @@ -54,7 +55,8 @@ extension Store { if store.state == nil { `else`() } }) - let unwrapDisposable = self + let unwrapDisposable = + self .scope( state: { state in state.distinctUntilChanged({ ($0 != nil) == ($1 != nil) }) diff --git a/Tests/ComposableArchitectureTests/StoreTests.swift b/Tests/ComposableArchitectureTests/StoreTests.swift index aeeecda..c74bff3 100644 --- a/Tests/ComposableArchitectureTests/StoreTests.swift +++ b/Tests/ComposableArchitectureTests/StoreTests.swift @@ -235,119 +235,120 @@ final class StoreTests: XCTestCase { XCTAssertEqual(ViewStore(store).state, 100_000) } - func testPublisherScope() { - let appReducer = Reducer { state, action, _ in - state += action ? 1 : 0 - return .none - } + func testPublisherScope() { + let appReducer = Reducer { state, action, _ in + state += action ? 1 : 0 + return .none + } - let parentStore = Store(initialState: 0, reducer: appReducer, environment: ()) + let parentStore = Store(initialState: 0, reducer: appReducer, environment: ()) - var outputs: [Int] = [] + var outputs: [Int] = [] - parentStore - .scope { $0.distinctUntilChanged() } - .subscribe(onNext: { - outputs.append($0.state) - }) - .disposed(by: disposeBag) + parentStore + .scope { $0.distinctUntilChanged() } + .subscribe(onNext: { + outputs.append($0.state) + }) + .disposed(by: disposeBag) - XCTAssertEqual(outputs, [0]) + XCTAssertEqual(outputs, [0]) - parentStore.send(true) - XCTAssertEqual(outputs, [0, 1]) + parentStore.send(true) + XCTAssertEqual(outputs, [0, 1]) - parentStore.send(false) - XCTAssertEqual(outputs, [0, 1]) - parentStore.send(false) - XCTAssertEqual(outputs, [0, 1]) - parentStore.send(false) - XCTAssertEqual(outputs, [0, 1]) - parentStore.send(false) - XCTAssertEqual(outputs, [0, 1]) - } + parentStore.send(false) + XCTAssertEqual(outputs, [0, 1]) + parentStore.send(false) + XCTAssertEqual(outputs, [0, 1]) + parentStore.send(false) + XCTAssertEqual(outputs, [0, 1]) + parentStore.send(false) + XCTAssertEqual(outputs, [0, 1]) + } - func testIfLetAfterScope() { - struct AppState { - var count: Int? - } + func testIfLetAfterScope() { + struct AppState { + var count: Int? + } - let appReducer = Reducer { state, action, _ in - state.count = action - return .none - } + let appReducer = Reducer { state, action, _ in + state.count = action + return .none + } - let parentStore = Store(initialState: AppState(), reducer: appReducer, environment: ()) + let parentStore = Store(initialState: AppState(), reducer: appReducer, environment: ()) - // NB: This test needs to hold a strong reference to the emitted stores - var outputs: [Int?] = [] - var stores: [Any] = [] + // NB: This test needs to hold a strong reference to the emitted stores + var outputs: [Int?] = [] + var stores: [Any] = [] - parentStore - .scope(state: \.count) - .ifLet( - then: { store in - stores.append(store) - outputs.append(store.state) + parentStore + .scope(state: \.count) + .ifLet( + then: { store in + stores.append(store) + outputs.append(store.state) }, else: { - outputs.append(nil) - }) - .disposed(by: disposeBag) - - XCTAssertEqual(outputs, [nil]) - - parentStore.send(1) - XCTAssertEqual(outputs, [nil, 1]) - - parentStore.send(nil) - XCTAssertEqual(outputs, [nil, 1, nil]) + outputs.append(nil) + } + ) + .disposed(by: disposeBag) - parentStore.send(1) - XCTAssertEqual(outputs, [nil, 1, nil, 1]) + XCTAssertEqual(outputs, [nil]) - parentStore.send(nil) - XCTAssertEqual(outputs, [nil, 1, nil, 1, nil]) + parentStore.send(1) + XCTAssertEqual(outputs, [nil, 1]) - parentStore.send(1) - XCTAssertEqual(outputs, [nil, 1, nil, 1, nil, 1]) + parentStore.send(nil) + XCTAssertEqual(outputs, [nil, 1, nil]) - parentStore.send(nil) - XCTAssertEqual(outputs, [nil, 1, nil, 1, nil, 1, nil]) - } + parentStore.send(1) + XCTAssertEqual(outputs, [nil, 1, nil, 1]) - func testIfLetTwo() { - let parentStore = Store( - initialState: 0, - reducer: Reducer { state, action, _ in - if action { - state? += 1 - return .none - } else { - return Effect(value: true) - .observeOn(MainScheduler.instance) - .eraseToEffect() - } - }, - environment: () - ) + parentStore.send(nil) + XCTAssertEqual(outputs, [nil, 1, nil, 1, nil]) - parentStore.ifLet { childStore in - let vs = ViewStore(childStore) + parentStore.send(1) + XCTAssertEqual(outputs, [nil, 1, nil, 1, nil, 1]) - vs - .publisher - .subscribe(onNext: { _ in }) - .disposed(by: self.disposeBag) + parentStore.send(nil) + XCTAssertEqual(outputs, [nil, 1, nil, 1, nil, 1, nil]) + } - vs.send(false) - _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) - vs.send(false) - _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) - vs.send(false) - _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) - XCTAssertEqual(vs.state, 3) - } - .disposed(by: disposeBag) + func testIfLetTwo() { + let parentStore = Store( + initialState: 0, + reducer: Reducer { state, action, _ in + if action { + state? += 1 + return .none + } else { + return Effect(value: true) + .observeOn(MainScheduler.instance) + .eraseToEffect() + } + }, + environment: () + ) + + parentStore.ifLet { childStore in + let vs = ViewStore(childStore) + + vs + .publisher + .subscribe(onNext: { _ in }) + .disposed(by: self.disposeBag) + + vs.send(false) + _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) + vs.send(false) + _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) + vs.send(false) + _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) + XCTAssertEqual(vs.state, 3) } + .disposed(by: disposeBag) + } }