Code Monkey home page Code Monkey logo

testingextensions's Introduction

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)

testingextensions's People

Contributors

luizmb avatar melle avatar zweigraf avatar marcusficner avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.