Skip to content

Commit 330a2d4

Browse files
committed
Update POSSettingsLocalCatalogViewModelTests to remove timeout usage
Instead of using timeouts in tests we: - Call async method - Verify intermediate state - Manually continue the mocked async method - Verify the finished state
1 parent 790171e commit 330a2d4

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

WooCommerce/WooCommerceTests/POS/Presentation/Settings/POSSettingsLocalCatalogViewModelTests.swift

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,19 @@ struct POSSettingsLocalCatalogViewModelTests {
9191
lastFullSyncDate: nil,
9292
lastIncrementalSyncDate: nil
9393
))
94-
catalogSettingsService.shouldDelayResponse = true
9594

96-
// When
97-
let loadTask = Task {
98-
await sut.loadCatalogData()
99-
}
95+
catalogSettingsService.onLoadCatalogInfoCalled = { continuation in
96+
// When loadCatalogInfo is called, loading is set to true
97+
#expect(sut.isLoading == true)
10098

101-
// Then
102-
try await Task.sleep(for: .milliseconds(10))
103-
#expect(sut.isLoading == true)
99+
// Finsh loadCatalogInfo
100+
continuation.resume()
101+
}
104102

105-
catalogSettingsService.shouldDelayResponse = false
106-
await loadTask.value
103+
// When
104+
await sut.loadCatalogData()
107105

106+
// Then
108107
#expect(sut.isLoading == false)
109108
}
110109

@@ -137,20 +136,19 @@ struct POSSettingsLocalCatalogViewModelTests {
137136
lastFullSyncDate: nil,
138137
lastIncrementalSyncDate: nil
139138
))
140-
catalogSyncCoordinator.shouldDelayResponse = true
141139

142-
// When
143-
let refreshTask = Task {
144-
await sut.refreshCatalog()
145-
}
140+
catalogSyncCoordinator.onPerformFullSyncCalled = { continuation in
141+
// When performFullSyncCalled is called, isRefreshingCatalog is set to true
142+
#expect(sut.isRefreshingCatalog == true)
146143

147-
// Then
148-
try await Task.sleep(for: .milliseconds(10))
149-
#expect(sut.isRefreshingCatalog == true)
144+
// Finsh performFullSync
145+
continuation.resume()
146+
}
150147

151-
catalogSyncCoordinator.shouldDelayResponse = false
152-
await refreshTask.value
148+
// When
149+
await sut.refreshCatalog()
153150

151+
// Then
154152
#expect(sut.isRefreshingCatalog == false)
155153
}
156154

0 commit comments

Comments
 (0)