Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dikasetiadi
Copy link
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
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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants