Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests fixes #9

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading