Skip to content

Commit

Permalink
Tests fixes (#9)
Browse files Browse the repository at this point in the history
* use dummy reference type observer for tests

* flaky tests fixes
  • Loading branch information
KazaiMazai authored Mar 31, 2024
1 parent f8ca8f3 commit 29f76cf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
14 changes: 9 additions & 5 deletions Tests/PureduxTests/StoreTests/ObserverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class ObserverTests: XCTestCase {
func test_WhenObserverObjectDeallocated_ThenObserverDies() {
let asyncExpectation = expectation(description: "Observer handler")
asyncExpectation.expectedFulfillmentCount = 2
var someClass: SomeClass? = SomeClass()
var someClass: ReferenceTypeObserver? = ReferenceTypeObserver()

let observer = Observer<Int>(someClass!) { state, complete in
asyncExpectation.fulfill()
Expand All @@ -67,7 +67,7 @@ final class ObserverTests: XCTestCase {
func test_WhenObserverObjectIsAlive_ThenObserverClosureCalledWithState() {
let asyncExpectation = expectation(description: "Observer handler")

let someClass = SomeClass()
let someClass = ReferenceTypeObserver()

let observer = Observer<Int>(someClass) { state, complete in
asyncExpectation.fulfill()
Expand All @@ -82,7 +82,7 @@ final class ObserverTests: XCTestCase {
func test_WhenObserverDedublicatesEqualValues_ThenObserverIsCalledOnce() {
let asyncExpectation = expectation(description: "Observer handler")
asyncExpectation.expectedFulfillmentCount = 1
let someClass = SomeClass()
let someClass = ReferenceTypeObserver()

let observer = Observer<Int>(someClass, removeStateDuplicates: .alwaysEqual) { state, complete in
asyncExpectation.fulfill()
Expand All @@ -99,7 +99,7 @@ final class ObserverTests: XCTestCase {
func test_WhenObserverDedublicatesAsAlwaysEqual_ThenObserverIsCalledOnce() {
let asyncExpectation = expectation(description: "Observer handler")
asyncExpectation.expectedFulfillmentCount = 1
let someClass = SomeClass()
let someClass = ReferenceTypeObserver()

let observer = Observer<Int>(someClass, removeStateDuplicates: .alwaysEqual) { state, complete in
asyncExpectation.fulfill()
Expand All @@ -116,7 +116,7 @@ final class ObserverTests: XCTestCase {
func test_WhenObserverDedublicatesAsEquatable_ThenObserverIsCalledAccordingly() {
let asyncExpectation = expectation(description: "Observer handler")
asyncExpectation.expectedFulfillmentCount = 3
let someClass = SomeClass()
let someClass = ReferenceTypeObserver()

let observer = Observer<Int>(someClass, removeStateDuplicates: .asEquatable) { state, complete in
asyncExpectation.fulfill()
Expand All @@ -130,3 +130,7 @@ final class ObserverTests: XCTestCase {
waitForExpectations(timeout: timeout)
}
}

class ReferenceTypeObserver {

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,46 @@ final class StoreNodeChildStoreObserverRefCycleTests: XCTestCase {
)

func test_WhenStrongRefToStoreObjectAndObserverLive_ThenReferencCycleIsCreated() {
weak var weakChildStore: ChildStore?

weak var weakRefObject: ReferenceTypeState?
autoreleasepool {
let strongRefObject = ReferenceTypeState()
let strongChildStore = rootStore.createChildStore(
initialState: ChildTestState(currentIndex: 0),
stateMapping: { state, childState in
StateComposition(state: state, childState: childState)
},
reducer: { state, action in state.reduce(action: action) }
initialState: strongRefObject,
stateMapping: { state, childState in childState },
reducer: { state, action in state.reduce(action) }
)
weakChildStore = strongChildStore
weakRefObject = strongRefObject

let referencedStore = strongChildStore.referencedStore()

let observer = Observer<StateComposition> { _, complete in
let observer = Observer<ReferenceTypeState> { _, complete in
referencedStore.dispatch(UpdateIndex(index: 1))
complete(.active)
}

referencedStore.subscribe(observer: observer)
}

XCTAssertNotNil(weakChildStore)
XCTAssertNotNil(weakRefObject)
}

func test_WhenStrongRefToStoreObjectAndObserverDead_ThenStoreIsReleased() {
weak var weakChildStore: ChildStore?

weak var weakRefObject: ReferenceTypeState?
let asyncExpectation = expectation(description: "Observer state handler")

autoreleasepool {
let strongRefObject = ReferenceTypeState()
let strongChildStore = rootStore.createChildStore(
initialState: ChildTestState(currentIndex: 0),
stateMapping: { state, childState in
StateComposition(state: state, childState: childState)
},
reducer: { state, action in state.reduce(action: action) }
initialState: strongRefObject,
stateMapping: { state, childState in childState },
reducer: { state, action in state.reduce(action) }
)
weakChildStore = strongChildStore
weakRefObject = strongRefObject

let referencedStore = strongChildStore.referencedStore()

let observer = Observer<StateComposition> { _, complete in
let observer = Observer<ReferenceTypeState> { _, complete in
referencedStore.dispatch(UpdateIndex(index: 1))
complete(.dead)
asyncExpectation.fulfill()
Expand All @@ -71,34 +68,33 @@ final class StoreNodeChildStoreObserverRefCycleTests: XCTestCase {
}

waitForExpectations(timeout: timeout) { _ in
XCTAssertNil(weakChildStore)
XCTAssertNil(weakRefObject)
}
}

func test_WhenWeakStoreAndObserverLive_ThenStoreIsReleased() {
weak var weakChildStore: ChildStore?
weak var weakRefObject: ReferenceTypeState?

autoreleasepool {
let strongRefObject = ReferenceTypeState()
let strongChildStore = rootStore.createChildStore(
initialState: ChildTestState(currentIndex: 0),
stateMapping: { state, childState in
StateComposition(state: state, childState: childState)
},
reducer: { state, action in state.reduce(action: action) }
initialState: strongRefObject,
stateMapping: { state, childState in childState },
reducer: { state, action in state.reduce(action) }
)
weakChildStore = strongChildStore

weakRefObject = strongRefObject

let store = strongChildStore.store()

let observer = Observer<StateComposition> { _, complete in
let observer = Observer<ReferenceTypeState> { _, complete in
store.dispatch(UpdateIndex(index: 1))
complete(.active)

}

store.subscribe(observer: observer)
}

XCTAssertNil(weakChildStore)
XCTAssertNil(weakRefObject)
}
}
2 changes: 1 addition & 1 deletion Tests/PureduxTests/TestUtils/TestState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct SubStateWithIndex {
}
}

class SomeClass {
class ReferenceTypeState {

func reduce(_ action: Action) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ final class ChildStorePresenterRefCycleTests: XCTestCase {

func test_WhenStrongRefToVC_ThenStrongRefToChildStore() {
var strongViewController: StubViewController?
weak var weakRefObject: SomeClass?
weak var weakRefObject: ReferenceTypeState?

autoreleasepool {
let strongRefObject = SomeClass()
let strongRefObject = ReferenceTypeState()

let strongChildStore = factory.childStore(
initialState: strongRefObject,
Expand All @@ -49,11 +49,11 @@ final class ChildStorePresenterRefCycleTests: XCTestCase {
}

func test_WhenNoStrongRefToVC_ThenChildStoreIsReleased() {
weak var weakRefObject: SomeClass?
weak var weakRefObject: ReferenceTypeState?
var strongViewController: StubViewController?

autoreleasepool {
let strongRefObject = SomeClass()
let strongRefObject = ReferenceTypeState()

let strongChildStore = factory.childStore(
initialState: strongRefObject,
Expand Down

0 comments on commit 29f76cf

Please sign in to comment.