Skip to content

Commit 2e24b25

Browse files
stephencelismluisbrown
authored andcommitted
Fix Tic Tac Toe Optional Bug (#291)
1 parent 9836fec commit 2e24b25

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

Examples/TicTacToe/Sources/Core/LoginCore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public let loginReducer =
9595

9696
case .twoFactorDismissed:
9797
state.twoFactor = nil
98-
return .none
98+
return .cancel(id: TwoFactorTearDownToken())
9999
}
100100
}
101101
)

Examples/TicTacToe/Sources/Core/TwoFactorCore.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public enum TwoFactorAction: Equatable {
2323
case twoFactorResponse(Result<AuthenticationResponse, AuthenticationError>)
2424
}
2525

26+
public struct TwoFactorTearDownToken: Hashable {
27+
public init() {}
28+
}
29+
2630
public struct TwoFactorEnvironment {
2731
public var authenticationClient: AuthenticationClient
2832
public var mainQueue: DateScheduler
@@ -56,6 +60,7 @@ public let twoFactorReducer = Reducer<TwoFactorState, TwoFactorAction, TwoFactor
5660
.observe(on: environment.mainQueue)
5761
.catchToEffect()
5862
.map(TwoFactorAction.twoFactorResponse)
63+
.cancellable(id: TwoFactorTearDownToken())
5964

6065
case let .twoFactorResponse(.failure(error)):
6166
state.alert = .init(title: .init(error.localizedDescription))

Examples/TicTacToe/Tests/LoginCoreTests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,51 @@ class LoginCoreTests: XCTestCase {
6161
}
6262
)
6363
}
64+
65+
func testFlow_DismissEarly_TwoFactor_Integration() {
66+
let store = TestStore(
67+
initialState: LoginState(),
68+
reducer: loginReducer,
69+
environment: LoginEnvironment(
70+
authenticationClient: .mock(
71+
login: { _ in
72+
Effect(value: .init(token: "deadbeefdeadbeef", twoFactorRequired: true))
73+
},
74+
twoFactor: { _ in
75+
Effect(value: .init(token: "deadbeefdeadbeef", twoFactorRequired: false))
76+
}
77+
),
78+
mainQueue: AnyScheduler(self.scheduler)
79+
)
80+
)
81+
82+
store.assert(
83+
.send(.emailChanged("[email protected]")) {
84+
$0.email = "[email protected]"
85+
},
86+
.send(.passwordChanged("password")) {
87+
$0.password = "password"
88+
$0.isFormValid = true
89+
},
90+
.send(.loginButtonTapped) {
91+
$0.isLoginRequestInFlight = true
92+
},
93+
.do { self.scheduler.advance() },
94+
.receive(.loginResponse(.success(.init(token: "deadbeefdeadbeef", twoFactorRequired: true))))
95+
{
96+
$0.isLoginRequestInFlight = false
97+
$0.twoFactor = TwoFactorState(token: "deadbeefdeadbeef")
98+
},
99+
.send(.twoFactor(.codeChanged("1234"))) {
100+
$0.twoFactor?.code = "1234"
101+
$0.twoFactor?.isFormValid = true
102+
},
103+
.send(.twoFactor(.submitButtonTapped)) {
104+
$0.twoFactor?.isTwoFactorRequestInFlight = true
105+
},
106+
.send(.twoFactorDismissed) {
107+
$0.twoFactor = nil
108+
}
109+
)
110+
}
64111
}

0 commit comments

Comments
 (0)