Skip to content

Commit

Permalink
#88: Fix tests failing for updated Loadable
Browse files Browse the repository at this point in the history
  • Loading branch information
nalexn committed Nov 11, 2023
1 parent b1ad1f4 commit 231a47f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
9 changes: 9 additions & 0 deletions CountriesSwiftUI/Utilities/CancelBag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ import Combine

final class CancelBag {
fileprivate(set) var subscriptions = Set<AnyCancellable>()
private let equalToAny: Bool

init(equalToAny: Bool = false) {
self.equalToAny = equalToAny
}

func cancel() {
subscriptions.removeAll()
}

func isEqual(to other: CancelBag) -> Bool {
return other === self || other.equalToAny || self.equalToAny
}
}

extension AnyCancellable {
Expand Down
2 changes: 1 addition & 1 deletion CountriesSwiftUI/Utilities/Loadable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ extension Loadable: Equatable where T: Equatable {
switch (lhs, rhs) {
case (.notRequested, .notRequested): return true
case let (.isLoading(lhsV, lhsC), .isLoading(rhsV, rhsC)):
return lhsV == rhsV && lhsC === rhsC
return lhsV == rhsV && lhsC.isEqual(to: rhsC)
case let (.loaded(lhsV), .loaded(rhsV)): return lhsV == rhsV
case let (.failed(lhsE), .failed(rhsE)):
return lhsE.localizedDescription == rhsE.localizedDescription
Expand Down
18 changes: 9 additions & 9 deletions UnitTests/Interactors/CountriesInteractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class LoadCountriesTests: CountriesInteractorTests {
countries.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.loaded(list.lazyList)
], removing: Country.prefixes)
self.mockedWebRepo.verify()
Expand Down Expand Up @@ -93,7 +93,7 @@ final class LoadCountriesTests: CountriesInteractorTests {
countries.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.failed(error)
], removing: Country.prefixes)
self.mockedWebRepo.verify()
Expand Down Expand Up @@ -126,7 +126,7 @@ final class LoadCountriesTests: CountriesInteractorTests {
countries.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.failed(error)
], removing: Country.prefixes)
self.mockedWebRepo.verify()
Expand Down Expand Up @@ -163,7 +163,7 @@ final class LoadCountriesTests: CountriesInteractorTests {
countries.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.loaded(list.lazyList)
], removing: Country.prefixes)
self.mockedWebRepo.verify()
Expand Down Expand Up @@ -199,7 +199,7 @@ final class LoadCountriesTests: CountriesInteractorTests {
countries.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.failed(error)
], removing: Country.prefixes)
self.mockedWebRepo.verify()
Expand Down Expand Up @@ -236,7 +236,7 @@ final class LoadCountryDetailsTests: CountriesInteractorTests {
details.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.loaded(data.details)
], removing: Country.prefixes)
self.mockedWebRepo.verify()
Expand Down Expand Up @@ -270,7 +270,7 @@ final class LoadCountryDetailsTests: CountriesInteractorTests {
details.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.failed(error)
], removing: Country.prefixes)
self.mockedWebRepo.verify()
Expand Down Expand Up @@ -307,7 +307,7 @@ final class LoadCountryDetailsTests: CountriesInteractorTests {
details.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.failed(error)
], removing: Country.prefixes)
self.mockedWebRepo.verify()
Expand Down Expand Up @@ -343,7 +343,7 @@ final class LoadCountryDetailsTests: CountriesInteractorTests {
details.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.loaded(data.details)
], removing: Country.prefixes)
self.mockedWebRepo.verify()
Expand Down
6 changes: 3 additions & 3 deletions UnitTests/Interactors/ImagesInteractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ final class ImagesInteractorTests: XCTestCase {
image.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.loaded(self.testImage)
])
self.verifyRepoActions()
Expand All @@ -76,7 +76,7 @@ final class ImagesInteractorTests: XCTestCase {
image.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.failed(error)
])
self.verifyRepoActions()
Expand All @@ -95,7 +95,7 @@ final class ImagesInteractorTests: XCTestCase {
image.updatesRecorder.sink { updates in
XCTAssertEqual(updates, [
.loaded(self.testImage),
.isLoading(last: self.testImage, cancelBag: CancelBag()),
.isLoading(last: self.testImage, cancelBag: .test),
.failed(error)
])
self.verifyRepoActions()
Expand Down
10 changes: 8 additions & 2 deletions UnitTests/Utilities/LoadableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ final class LoadableTests: XCTestCase {
]
let expect: [Loadable<String>] = [
.notRequested,
.isLoading(last: nil, cancelBag: CancelBag()),
.isLoading(last: "5", cancelBag: CancelBag()),
.isLoading(last: nil, cancelBag: .test),
.isLoading(last: "5", cancelBag: .test),
.loaded("7"),
.failed(NSError.test)
]
Expand Down Expand Up @@ -100,3 +100,9 @@ final class LoadableTests: XCTestCase {
XCTAssertEqual(ValueIsMissingError().localizedDescription, "Data is missing")
}
}

extension CancelBag {
static var test: CancelBag {
return CancelBag(equalToAny: true)
}
}

0 comments on commit 231a47f

Please sign in to comment.