Testing Selectors

In Fluxor Selectors are projectors of State. Selectors can be created by a KeyPath, by a closure or based on up to 5 other Selectors. When a Selector is based on other Selectors, the projector takes the Values from the others as parameters.

Testing basic Selectors

The Selector‘s map function takes the State and returns a Value.

struct Selectors {
    static let getNameState = Selector(keyPath: \AppState.name)
}

class SelectorsTests: XCTestCase {
    func testGetNameState() {
        // Given
        let state = AppState(name: NameState(firstName: "Tim", lastName: "Cook"))
        // Then
        XCTAssertEqual(Selectors.getNameState.map(state), state.name)
    }
}

Testing Selectors based on Selectors

If a Selector is based on the Values from other Selectors, it will also have a projector property. The projector can be used in tests, to easily test the Selector without creating the full State instance.

extension Selectors {
    static let congratulations = Selector.with(getFullName, getBirthday) { fullName, birthday in
        "Congratulations \(fullName)! Today is \(birthday.month) \(birthday.day) - your birthday!"
    }
}

class SelectorsTests: XCTestCase {
    func testCongratulations() {
        XCTAssertEqual(Selectors.congratulations.projector("Tim Cook", Birthday(month: "November", day: "1")),
                       "Congratulations Tim Cook! Today is November 1 - your birthday!")
    }
}