Skip to content

Commit

Permalink
Fix Tic Tac Toe Optional Bug (pointfreeco#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis authored Sep 16, 2020
1 parent 43a122a commit fe06d95
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Examples/TicTacToe/Sources/Core/LoginCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public let loginReducer =

case .twoFactorDismissed:
state.twoFactor = nil
return .none
return .cancel(id: TwoFactorTearDownToken())
}
}
)
5 changes: 5 additions & 0 deletions Examples/TicTacToe/Sources/Core/TwoFactorCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public enum TwoFactorAction: Equatable {
case twoFactorResponse(Result<AuthenticationResponse, AuthenticationError>)
}

public struct TwoFactorTearDownToken: Hashable {
public init() {}
}

public struct TwoFactorEnvironment {
public var authenticationClient: AuthenticationClient
public var mainQueue: AnySchedulerOf<DispatchQueue>
Expand Down Expand Up @@ -56,6 +60,7 @@ public let twoFactorReducer = Reducer<TwoFactorState, TwoFactorAction, TwoFactor
.receive(on: environment.mainQueue)
.catchToEffect()
.map(TwoFactorAction.twoFactorResponse)
.cancellable(id: TwoFactorTearDownToken())

case let .twoFactorResponse(.failure(error)):
state.alert = .init(title: .init(error.localizedDescription))
Expand Down
47 changes: 47 additions & 0 deletions Examples/TicTacToe/Tests/LoginCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,51 @@ class LoginCoreTests: XCTestCase {
}
)
}

func testFlow_DismissEarly_TwoFactor_Integration() {
let store = TestStore(
initialState: LoginState(),
reducer: loginReducer,
environment: LoginEnvironment(
authenticationClient: .mock(
login: { _ in
Effect(value: .init(token: "deadbeefdeadbeef", twoFactorRequired: true))
},
twoFactor: { _ in
Effect(value: .init(token: "deadbeefdeadbeef", twoFactorRequired: false))
}
),
mainQueue: AnyScheduler(self.scheduler)
)
)

store.assert(
.send(.emailChanged("[email protected]")) {
$0.email = "[email protected]"
},
.send(.passwordChanged("password")) {
$0.password = "password"
$0.isFormValid = true
},
.send(.loginButtonTapped) {
$0.isLoginRequestInFlight = true
},
.do { self.scheduler.advance() },
.receive(.loginResponse(.success(.init(token: "deadbeefdeadbeef", twoFactorRequired: true))))
{
$0.isLoginRequestInFlight = false
$0.twoFactor = TwoFactorState(token: "deadbeefdeadbeef")
},
.send(.twoFactor(.codeChanged("1234"))) {
$0.twoFactor?.code = "1234"
$0.twoFactor?.isFormValid = true
},
.send(.twoFactor(.submitButtonTapped)) {
$0.twoFactor?.isTwoFactorRequestInFlight = true
},
.send(.twoFactorDismissed) {
$0.twoFactor = nil
}
)
}
}

0 comments on commit fe06d95

Please sign in to comment.