Skip to content
Permalink
Browse files

Add Reactor.isStubEnabled property and deprecated Stub.isEnabled

  • Loading branch information...
devxoul committed Jun 15, 2019
1 parent b6720f3 commit e813112e4497a66f046e1fa0e9783adbace388bf
Showing with 17 additions and 7 deletions.
  1. +8 −2 Sources/ReactorKit/Reactor.swift
  2. +5 −1 Sources/ReactorKit/Stub.swift
  3. +4 −4 Tests/ReactorKitTests/ReactorTests.swift
@@ -69,14 +69,15 @@ private var actionKey = "action"
private var currentStateKey = "currentState"
private var stateKey = "state"
private var disposeBagKey = "disposeBag"
private var isStubEnabledKey = "isStubEnabled"
private var stubKey = "stub"


// MARK: - Default Implementations
extension Reactor {
private var _action: ActionSubject<Action> {
if self.stub.isEnabled {
if self.isStubEnabled {
return self.stub.action
} else {
return self.associatedObject(forKey: &actionKey, default: .init())
@@ -97,7 +98,7 @@ extension Reactor {
}

private var _state: Observable<State> {
if self.stub.isEnabled {
if self.isStubEnabled {
return self.stub.state.asObservable()
} else {
return self.associatedObject(forKey: &stateKey, default: self.createStateStream())
@@ -170,6 +171,11 @@ extension Reactor where Action == Mutation {
// MARK: - Stub
extension Reactor {
public var isStubEnabled: Bool {
set { self.setAssociatedObject(newValue, forKey: &isStubEnabledKey) }
get { return self.associatedObject(forKey: &isStubEnabledKey, default: false) }
}

public var stub: Stub<Self> {
return self.associatedObject(
forKey: &stubKey,
@@ -4,7 +4,11 @@ public class Stub<Reactor: ReactorKit.Reactor> {
private unowned var reactor: Reactor
private let disposeBag: DisposeBag

public var isEnabled: Bool = false
@available(*, deprecated, message: "Use 'Reactor.isStubEnabled' instead.")
public var isEnabled: Bool {
set { self.reactor.isStubEnabled = newValue }
get { return self.reactor.isStubEnabled }
}

public let state: StateRelay<Reactor.State>
public let action: ActionSubject<Reactor.Action>
@@ -158,14 +158,14 @@ final class ReactorTests: XCTestCase {

func testStub_actionAndStateMemoryAddress() {
let reactor = TestReactor()
reactor.stub.isEnabled = true
reactor.isStubEnabled = true
XCTAssertTrue(reactor.action === reactor.stub.action)
XCTAssertTrue(reactor.state === reactor.stub.state.asObservable())
}

func testStub_actions() {
let reactor = StopwatchReactor(scheduler: MainScheduler.instance)
reactor.stub.isEnabled = true
reactor.isStubEnabled = true
reactor.action.onNext(.start)
reactor.action.onNext(.start)
reactor.action.onNext(.stop)
@@ -174,7 +174,7 @@ final class ReactorTests: XCTestCase {

func testStub_state() {
let reactor = StopwatchReactor(scheduler: MainScheduler.instance)
reactor.stub.isEnabled = true
reactor.isStubEnabled = true
reactor.stub.state.value = 0
XCTAssertEqual(reactor.currentState, 0)
reactor.stub.state.value = 1
@@ -187,7 +187,7 @@ final class ReactorTests: XCTestCase {

func testStub_ignoreAction() {
let reactor = TestReactor()
reactor.stub.isEnabled = true
reactor.isStubEnabled = true
reactor.action.onNext(["A"])
XCTAssertEqual(reactor.currentState, [])
}

0 comments on commit e813112

Please sign in to comment.
You can’t perform that action at this time.