Skip to content

Commit 21b3d5e

Browse files
committed
Update tests
1 parent e1ea486 commit 21b3d5e

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

WooCommerce/Classes/Bookings/BookingList/BookingListContainerViewModel.swift

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ final class BookingListContainerViewModel: ObservableObject {
77
private let siteID: Int64
88
private let stores: StoresManager
99

10-
private lazy var allTabViewModels: [BookingListViewModel] = [
11-
todayListViewModel,
12-
upcomingListViewModel,
13-
allListViewModel
14-
]
1510
private let todayListViewModel: BookingListViewModel
1611
private let upcomingListViewModel: BookingListViewModel
1712
private let allListViewModel: BookingListViewModel
@@ -29,6 +24,11 @@ final class BookingListContainerViewModel: ObservableObject {
2924
private var searchQuerySubscription: AnyCancellable?
3025
private var sortBySubscription: AnyCancellable?
3126

27+
private lazy var allTabViewModels: [BookingListViewModel] = [
28+
todayListViewModel,
29+
upcomingListViewModel,
30+
allListViewModel
31+
]
3232

3333
private var filters = BookingFiltersViewModel.Filters()
3434

@@ -92,37 +92,13 @@ final class BookingListContainerViewModel: ObservableObject {
9292
}
9393

9494
restorePersistedFilters()
95-
}
9695

97-
@MainActor
98-
func pullToRefresh(on tab: BookingListTab) async {
99-
await withCheckedContinuation { continuation in
100-
let action = BookingAction.clearBookingsCache(siteID: siteID) {
101-
continuation.resume()
102-
}
103-
stores.dispatch(action)
104-
}
105-
106-
// Launch all tab refreshes in parallel and wait for all to complete
107-
await withTaskGroup(of: Void.self) { group in
108-
for viewModel in allTabViewModels {
109-
group.addTask { @MainActor in
110-
await viewModel.onRefreshSelfAction(
111-
reason: BookingListViewModel.siblingRefreshReason
112-
)
113-
}
114-
}
115-
}
96+
todayListViewModel.refreshCoordinator = self
97+
upcomingListViewModel.refreshCoordinator = self
98+
allListViewModel.refreshCoordinator = self
11699
}
117100

118101
func listViewModel(for tab: BookingListTab) -> BookingListViewModel {
119-
120-
121-
122-
todayListViewModel.parent = self
123-
upcomingListViewModel.parent = self
124-
allListViewModel.parent = self
125-
126102
switch tab {
127103
case .today:
128104
return todayListViewModel
@@ -214,6 +190,29 @@ private extension BookingListContainerViewModel {
214190
}
215191
}
216192

193+
extension BookingListContainerViewModel: BookingListsRefreshCoordinating {
194+
@MainActor
195+
func refreshAllLists() async {
196+
await withCheckedContinuation { continuation in
197+
let action = BookingAction.clearBookingsCache(siteID: siteID) {
198+
continuation.resume()
199+
}
200+
stores.dispatch(action)
201+
}
202+
203+
// Launch all tab refreshes in parallel and wait for all to complete
204+
await withTaskGroup(of: Void.self) { group in
205+
for viewModel in allTabViewModels {
206+
group.addTask { @MainActor in
207+
await viewModel.onRefreshSelfAction(
208+
reason: BookingListViewModel.siblingRefreshReason
209+
)
210+
}
211+
}
212+
}
213+
}
214+
}
215+
217216
private extension BookingListContainerViewModel {
218217
enum Localization {
219218
static let filter = NSLocalizedString(

WooCommerce/Classes/Bookings/BookingList/BookingListViewModel.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import Combine
55
import protocol Storage.StorageManagerType
66
import class Networking.BookingsRemote
77

8+
protocol BookingListsRefreshCoordinating: AnyObject {
9+
func refreshAllLists() async
10+
}
11+
812
/// View model for `BookingListView`
913
final class BookingListViewModel: ObservableObject {
1014

@@ -14,7 +18,7 @@ final class BookingListViewModel: ObservableObject {
1418

1519
@Published private(set) var hasFilters = false
1620

17-
weak var parent: BookingListContainerViewModel?
21+
weak var refreshCoordinator: BookingListsRefreshCoordinating?
1822

1923
var emptyStateTitle: String {
2024
type.emptyStateTitle(hasFilters: hasFilters)
@@ -101,7 +105,7 @@ final class BookingListViewModel: ObservableObject {
101105

102106
/// Called when the user pulls down the list to refresh.
103107
func onRefreshAction() async {
104-
await parent?.pullToRefresh(on: type)
108+
await refreshCoordinator?.refreshAllLists()
105109
}
106110

107111
@MainActor

WooCommerce/WooCommerceTests/ViewRelated/Bookings/BookingListViewModelTests.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ struct BookingListViewModelTests {
307307
let viewModel = BookingListViewModel(siteID: sampleSiteID, type: .all, stores: stores)
308308

309309
// When
310-
await viewModel.onRefreshAction()
310+
await viewModel.onRefreshSelfAction()
311311

312312
// Then
313313
#expect(skip == 0)
@@ -441,26 +441,18 @@ struct BookingListViewModelTests {
441441
#expect(actionCallCount == 2, "Should have made two API calls")
442442
}
443443

444-
@Test func on_refresh_action_clears_cache() async {
444+
@Test func on_refresh_action_calls_refreshcoordiantor() async {
445445
// Given
446446
let stores = MockStoresManager(sessionManager: .testingInstance)
447-
var capturedShouldClearCache: Bool?
448-
449-
stores.whenReceivingAction(ofType: BookingAction.self) { action in
450-
guard case let .synchronizeBookings(_, _, _, _, _, shouldClearCache, onCompletion) = action else {
451-
return
452-
}
453-
capturedShouldClearCache = shouldClearCache
454-
onCompletion(.success(false))
455-
}
456-
447+
let mockRefresher = MockBookingListsRefreshCoordinating()
457448
let viewModel = BookingListViewModel(siteID: sampleSiteID, type: .all, stores: stores)
449+
viewModel.refreshCoordinator = mockRefresher
458450

459451
// When
460452
await viewModel.onRefreshAction()
461453

462454
// Then
463-
#expect(capturedShouldClearCache == true, "Refresh action should clear cache")
455+
#expect(mockRefresher.refreshAllListsCalled)
464456
}
465457

466458
// MARK: - Local storage filtering
@@ -719,3 +711,12 @@ private extension BookingListViewModelTests {
719711
return Booking.fake().copy(siteID: siteID ?? self.sampleSiteID, bookingID: id, startDate: startDate)
720712
}
721713
}
714+
715+
716+
class MockBookingListsRefreshCoordinating: BookingListsRefreshCoordinating {
717+
private(set) var refreshAllListsCalled = false
718+
719+
func refreshAllLists() async {
720+
refreshAllListsCalled = true
721+
}
722+
}

0 commit comments

Comments
 (0)