Skip to content

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

Open
dikasetiadi wants to merge 4 commits into
mainfrom
feature/implement-environment-on-reducerProtocol
Open

[ReducerProtocol] - Support Non-Dependencies Environment in ReducerProtocol#97
dikasetiadi wants to merge 4 commits into
mainfrom
feature/implement-environment-on-reducerProtocol

Conversation

@dikasetiadi
Copy link
Copy Markdown
Contributor

@dikasetiadi dikasetiadi commented Aug 14, 2023

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
}

@dikasetiadi dikasetiadi self-assigned this Aug 14, 2023
@dikasetiadi dikasetiadi added the enhancement New feature or request label Aug 14, 2023
@dikasetiadi dikasetiadi marked this pull request as ready for review August 17, 2023 17:36

internal final class EnvironmentReducerProtocolTests: XCTestCase {

internal func testEnvironmentReducerProtocol_getSuccess() {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider better name test case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants