Swiftpack.co -  SwiftRex/TestingExtensions as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
SwiftRex/TestingExtensions
Testing helpers and extensions for SwiftRex
.package(url: "https://github.com/SwiftRex/TestingExtensions.git", from: "v0.2.3")

TestingExtensions

Testing helpers and extensions for SwiftRex and Combine

SwiftRex Functional Tests (Use Case)

let (dependencies, scheduler) = self.setup

assert(
    initialValue: AppState.initial,
    reducer: Reducer.myReducer.lift(action: \.actionReducerScope, state: \.stateReducerScope),
    middleware: MyHandler.middleware.inject(dependencies),
    steps: {
        Send(action: .certainAction(.verySpecific))

        SideEffectResult {
            scheduler.advance(by: .seconds(5))
        }

        Send(action: .anotherAction(.withSomeValues(list: [], date: dependencies.now())))

        // if you receive an action and don't add this block, the test will fail to remind you
        Receive { action -> Bool in
            // validates that the received action is what you would expect
            // if this function returns false, the test will fail to show you that you've got an unexpected action
            if case let .my(.expectedAction(list)) = action {
                return list.isEmpty
            } else {
                return false
            }
        }

        SideEffectResult {
            scheduler.advance(by: .seconds(5))
        }

        Send(action: .anotherAction(.stopSomething)).expectStateToHaveChanged { state in
            // if during Send or Receive action, your state is expected to mutate, you must indicate which change is expected to happen here:
            state.somePropertyShouldHaveChangedTo = true
            // any unexpected state mutation will fail the test, as well as any expected state mutation that doesn't occur, will also fail the test
        }

        SideEffectResult {
            scheduler.advance(by: .seconds(5))
        }

        // if you receive an action and don't add this block, the test will fail to remind you
        Receive { action -> Bool in
            // validates that the received action is what you would expect
            // if this function returns false, the test will fail to show you that you've got an unexpected action
            if case let .my(.expectedAction(list)) = action {
                return list.isEmpty
            } else {
                return false
            }
        }.expectStateToHaveChanged { state in
            // if during Send or Receive action, your state is expected to mutate, you must indicate which change is expected to happen here:
            state.somePropertyShouldHaveChangedTo = true
            // any unexpected state mutation will fail the test, as well as any expected state mutation that doesn't occur, will also fail the test
        }
    }
)

Combine

Validate Output of Publishers

let operation = assert(
    publisher: myPublisher,
    eventuallyReceives: "πŸ™‰", "πŸ™Š", "πŸ™ˆ",
    andCompletes: false
)
somethingHappensAndPublisherReceives("πŸ™‰")
somethingHappensAndPublisherReceives("πŸ™Š")
somethingHappensAndPublisherReceives("πŸ™ˆ")

operation.wait(0.0001)

Validate Output and Successful Completion of Publishers

let operation = assert(
    publisher: myPublisher,
    eventuallyReceives: "πŸ™‰", "πŸ™Š", "πŸ™ˆ",
    andCompletesWith: .isSuccess
)
somethingHappensAndPublisherReceives("πŸ™‰")
somethingHappensAndPublisherReceives("πŸ™Š")
somethingHappensAndPublisherReceives("πŸ™ˆ")
somethingHappensAndPublisherCompletes()

operation.wait(0.0001)

Validate Output and some Failure of Publishers

let operation = assert(
    publisher: myPublisher,
    eventuallyReceives: "πŸ™‰", "πŸ™Š", "πŸ™ˆ",
    andCompletesWith: .isFailure
)
somethingHappensAndPublisherReceives("πŸ™‰")
somethingHappensAndPublisherReceives("πŸ™Š")
somethingHappensAndPublisherReceives("πŸ™ˆ")
somethingHappensAndPublisherFails(SomeError())

operation.wait(0.0001)

Validate Output and specific Failure of Publishers

let operation = assert(
    publisher: myPublisher,
    eventuallyReceives: "πŸ™‰", "πŸ™Š", "πŸ™ˆ",
    andCompletesWith: .failedWithError { error in error == SomeError("123") }
)
somethingHappensAndPublisherReceives("πŸ™‰")
somethingHappensAndPublisherReceives("πŸ™Š")
somethingHappensAndPublisherReceives("πŸ™ˆ")
somethingHappensAndPublisherFails(SomeError("123"))

operation.wait(0.0001)

Validate No Output but completion of Publishers

let operation = assert(
    publisher: myPublisher,
    completesWithoutValues: .isSuccess
)
somethingHappensAndPublisherCompletes()

operation.wait(0.0001)

GitHub

link
Stars: 0
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Dark/Light mode for snapshot tests
1 week ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API