|
| 1 | +// |
| 2 | +// InteractorTests.swift |
| 3 | +// RIBs |
| 4 | +// |
| 5 | +// Created by Alex Bush on 6/22/25. |
| 6 | +// |
| 7 | + |
| 8 | +@testable import RIBs |
| 9 | +import XCTest |
| 10 | +import RxSwift |
| 11 | + |
| 12 | +final class InteractorTests: XCTestCase { |
| 13 | + |
| 14 | + private var interactor: InteractorMock! |
| 15 | + |
| 16 | + override func setUp() { |
| 17 | + super.setUp() |
| 18 | + |
| 19 | + interactor = InteractorMock() // NOTE: we're using InteractorMock here to test the underlying parent class, Interactor, behavior so this is appropriate here. |
| 20 | + } |
| 21 | + |
| 22 | + func test_interactorIsInactiveByDefault() { |
| 23 | + XCTAssertFalse(interactor.isActive) |
| 24 | + let _ = interactor.isActiveStream.subscribe { isActive in |
| 25 | + XCTAssertFalse(isActive) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + func test_isActive_whenStarted_isTrue() { |
| 30 | + // give |
| 31 | + // when |
| 32 | + interactor.activate() |
| 33 | + // then |
| 34 | + XCTAssertTrue(interactor.isActive) |
| 35 | + let _ = interactor.isActiveStream.subscribe { isActive in |
| 36 | + XCTAssertTrue(isActive) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + func test_isActive_whenDeactivated_isFalse() { |
| 41 | + // given |
| 42 | + interactor.activate() |
| 43 | + // when |
| 44 | + interactor.deactivate() |
| 45 | + // then |
| 46 | + XCTAssertFalse(interactor.isActive) |
| 47 | + let _ = interactor.isActiveStream.subscribe { isActive in |
| 48 | + XCTAssertFalse(isActive) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + func test_didBecomeActive_isCalledWhenStarted() { |
| 53 | + // given |
| 54 | + // when |
| 55 | + interactor.activate() |
| 56 | + // then |
| 57 | + XCTAssertEqual(interactor.didBecomeActiveCallCount, 1) |
| 58 | + } |
| 59 | + |
| 60 | + func test_didBecomeActive_isNotCalledWhenAlreadyActive() { |
| 61 | + // given |
| 62 | + interactor.activate() |
| 63 | + XCTAssertEqual(interactor.didBecomeActiveCallCount, 1) |
| 64 | + // when |
| 65 | + interactor.activate() |
| 66 | + // then |
| 67 | + XCTAssertEqual(interactor.didBecomeActiveCallCount, 1) |
| 68 | + } |
| 69 | + |
| 70 | + func test_willResignActive_isCalledWhenDeactivated() { |
| 71 | + // given |
| 72 | + interactor.activate() |
| 73 | + // when |
| 74 | + interactor.deactivate() |
| 75 | + // then |
| 76 | + XCTAssertEqual(interactor.willResignActiveCallCount, 1) |
| 77 | + } |
| 78 | + |
| 79 | + func test_willResignActive_isNotCalledWhenAlreadyInactive() { |
| 80 | + // given |
| 81 | + interactor.activate() |
| 82 | + interactor.deactivate() |
| 83 | + XCTAssertEqual(interactor.willResignActiveCallCount, 1) |
| 84 | + // when |
| 85 | + interactor.deactivate() |
| 86 | + // then |
| 87 | + XCTAssertEqual(interactor.willResignActiveCallCount, 1) |
| 88 | + } |
| 89 | + |
| 90 | + func test_isActiveStream_completedOnInteractorDeinit() { |
| 91 | + // given |
| 92 | + var isActiveStreamCompleted = false |
| 93 | + interactor.activate() |
| 94 | + let _ = interactor.isActiveStream.subscribe { _ in } onCompleted: { |
| 95 | + isActiveStreamCompleted = true |
| 96 | + } |
| 97 | + |
| 98 | + // when |
| 99 | + interactor = nil |
| 100 | + // then |
| 101 | + XCTAssertTrue(isActiveStreamCompleted) |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + // MARK: - BEGIN Observables Attached/Detached to/from Interactor |
| 106 | + func test_observableAttachedToInactiveInteactorIsDisposedImmediately() { |
| 107 | + // given |
| 108 | + var onDisposeCalled = false |
| 109 | + let subjectEmiitingValues: PublishSubject<Int> = .init() |
| 110 | + let observable = subjectEmiitingValues.asObservable().do { _ in } onDispose: { |
| 111 | + onDisposeCalled = true |
| 112 | + } |
| 113 | + // when |
| 114 | + observable.subscribe().disposeOnDeactivate(interactor: interactor) |
| 115 | + // then |
| 116 | + XCTAssertTrue(onDisposeCalled) |
| 117 | + } |
| 118 | + |
| 119 | + func test_observableIsDisposedOnInteractorDeactivation() { |
| 120 | + // given |
| 121 | + var onDisposeCalled = false |
| 122 | + let subjectEmiitingValues: PublishSubject<Int> = .init() |
| 123 | + let observable = subjectEmiitingValues.asObservable().do { _ in } onDispose: { |
| 124 | + onDisposeCalled = true |
| 125 | + } |
| 126 | + interactor.activate() |
| 127 | + observable.subscribe().disposeOnDeactivate(interactor: interactor) |
| 128 | + // when |
| 129 | + interactor.deactivate() |
| 130 | + // then |
| 131 | + XCTAssertTrue(onDisposeCalled) |
| 132 | + } |
| 133 | + |
| 134 | + func test_observableIsDisposedOnInteractorDeinit() { |
| 135 | + // given |
| 136 | + var onDisposeCalled = false |
| 137 | + let subjectEmiitingValues: PublishSubject<Int> = .init() |
| 138 | + let observable = subjectEmiitingValues.asObservable().do { _ in } onDispose: { |
| 139 | + onDisposeCalled = true |
| 140 | + } |
| 141 | + interactor.activate() |
| 142 | + observable.subscribe().disposeOnDeactivate(interactor: interactor) |
| 143 | + XCTAssertFalse(onDisposeCalled) |
| 144 | + // when |
| 145 | + interactor = nil |
| 146 | + // then |
| 147 | + XCTAssertTrue(onDisposeCalled) |
| 148 | + } |
| 149 | + // MARK: Observables Attached/Detached to/from Interactor END - |
| 150 | + |
| 151 | + // MARK: - BEGIN Observables Confined to Interactor |
| 152 | + func test_observableConfinedToInteractorOnlyEmitsValueWhenInteractorIsActive() { |
| 153 | + // given |
| 154 | + var emittedValue: Int? |
| 155 | + let subjectEmiitingValues: PublishSubject<Int> = .init() |
| 156 | + let confinedObservable = subjectEmiitingValues.asObservable().confineTo(interactor) |
| 157 | + let _ = confinedObservable.confineTo(interactor) |
| 158 | + let _ = confinedObservable.subscribe { newValue in |
| 159 | + emittedValue = newValue |
| 160 | + } |
| 161 | + |
| 162 | + subjectEmiitingValues.onNext(1) |
| 163 | + XCTAssertNil(emittedValue) |
| 164 | + // when |
| 165 | + interactor.activate() |
| 166 | + subjectEmiitingValues.onNext(2) |
| 167 | + // then |
| 168 | + XCTAssertNotNil(emittedValue) |
| 169 | + XCTAssertEqual(emittedValue, 2) |
| 170 | + } |
| 171 | + // MARK: Observables Confined to Interactor - |
| 172 | +} |
0 commit comments