tokopedia / RxComposableArchitecture

RxComposableArchitecture is a forked of Composable Architecture with adjustment to make it work with UIKit.
Apache License 2.0
31 stars 5 forks source link

[ReducerProtocol] - Support Non-Dependencies Environment in ReducerProtocol #97

Open dikasetiadi opened 1 year ago

dikasetiadi commented 1 year ago

Improvement to support Non-Dependencies Environment inside ReducerProtocol implementation. We need to find a way how we can do mock on the fly since it only possible when using Dependencies, not with non-dependencies Environment.

✅ Solution: We are improve our TestStore to have capability to hold our Environment, and giving access to developer to change it on the fly

let testStore = TestStore(
    initialState: "",
    environment: AuthenticatorService.mock,
    reducer: Authenticator.init(authenticatorService:)
)

/// here we can mock our environment
////
testStore.environment.getAuthResult = {
    .success("success")
}

The reducer implementation will be like this:

struct Authenticator: ReducerProtocol {
    typealias State = String
    typealias Action = Void

    /// ✅ here our Environment 
    ///
    internal var authenticatorService: AuthenticatorService

    func reduce(into state: inout String, action: Void) -> Effect<Void> {
        switch authenticatorService.getAuthResult() {
        case let .success(successMessage):
            state = successMessage
        case let .failure(failureData):
            state = failureData.message
        }
        return .none
    }
}

For mocking Example we can do it like this:

Bootstrap.mock(
    for: Authenticator(authenticatorService: .mockFailed) // you can mock directly the Environment
) { currentReducer in
    ... your mocking Dependencies logic
}