From 49dae77fc89445bb4aa909a636d6e415af0e1343 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 20 Mar 2024 10:10:41 -0700 Subject: [PATCH] Added a test for nested dismissal. --- .../Reducers/PresentationReducerTests.swift | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Tests/ComposableArchitectureTests/Reducers/PresentationReducerTests.swift b/Tests/ComposableArchitectureTests/Reducers/PresentationReducerTests.swift index c5accdd862a3..15ed9b2d9f52 100644 --- a/Tests/ComposableArchitectureTests/Reducers/PresentationReducerTests.swift +++ b/Tests/ComposableArchitectureTests/Reducers/PresentationReducerTests.swift @@ -2639,4 +2639,51 @@ final class PresentationReducerTests: BaseTCATestCase { XCTAssertEqual($state, $state) XCTAssertLessThan(Date().timeIntervalSince(start), 0.1) } + + @MainActor + func testNestedDismiss() async { + let store = TestStore(initialState: NestedDismissFeature.State()) { + NestedDismissFeature() + } + + await store.send(\.presentButtonTapped) { + $0.child = NestedDismissFeature.State() + } + await store.send(\.child.presentButtonTapped) { + $0.child?.child = NestedDismissFeature.State() + } + await store.send(\.child.child.dismissButtonTapped) + await store.receive(\.child.child.dismiss) { + $0.child?.child = nil + } + } +} + +@Reducer +private struct NestedDismissFeature { + struct State: Equatable { + @PresentationState var child: NestedDismissFeature.State? + } + enum Action { + case child(PresentationAction) + case dismissButtonTapped + case presentButtonTapped + } + @Dependency(\.dismiss) var dismiss + var body: some ReducerOf { + Reduce { state, action in + switch action { + case .child: + return .none + case .dismissButtonTapped: + return .run { _ in await dismiss() } + case .presentButtonTapped: + state.child = State() + return .none + } + } + .ifLet(\.$child, action: \.child) { + Self() + } + } }