Skip to content

Commit 12d1651

Browse files
committed
Add tests
1 parent f95953e commit 12d1651

File tree

1 file changed

+90
-20
lines changed

1 file changed

+90
-20
lines changed

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncWorkerTest.kt

Lines changed: 90 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class WooPosLocalCatalogSyncWorkerTest : BaseUnitTest() {
3030
private lateinit var site: SiteModel
3131
private var logger: WooPosLogWrapper = mock()
3232
private var wooPosTabShouldBeVisible: WooPosTabShouldBeVisible = mock()
33-
private var isLocalCatalogSupported: WooPosIsLocalCatalogSupported = mock()
3433
private var syncStatusChecker: WooPosFullSyncStatusChecker = mock()
3534
private val mockTimeProvider: DateTimeProvider = mock {
3635
whenever(it.now()).thenReturn(CURRENT_TIME_MILLIS)
@@ -67,7 +66,6 @@ class WooPosLocalCatalogSyncWorkerTest : BaseUnitTest() {
6766
}
6867

6968
whenever(selectedSite.getOrNull()).thenReturn(site)
70-
whenever(isLocalCatalogSupported(site.localId())).thenReturn(true)
7169
whenever(syncStatusChecker.checkSyncRequirement()).thenReturn(WooPosFullSyncRequirement.BlockingRequired)
7270

7371
whenever(wooPosTabShouldBeVisible.invoke()).thenReturn(Result.success(true))
@@ -88,7 +86,6 @@ class WooPosLocalCatalogSyncWorkerTest : BaseUnitTest() {
8886
logger = logger,
8987
timeProvider = mockTimeProvider,
9088
wooPosTabShouldBeVisible = wooPosTabShouldBeVisible,
91-
isLocalCatalogSupported = isLocalCatalogSupported,
9289
syncStatusChecker = syncStatusChecker,
9390
)
9491
}
@@ -108,30 +105,17 @@ class WooPosLocalCatalogSyncWorkerTest : BaseUnitTest() {
108105
}
109106

110107
@Test
111-
fun `given local catalog not supported, when sync is attempted, then returns failure`() = testBlocking {
108+
fun `when no site selected, then returns retry`() = testBlocking {
112109
// GIVEN
113-
whenever(isLocalCatalogSupported(site.localId())).thenReturn(false)
110+
whenever(syncStatusChecker.checkSyncRequirement())
111+
.thenReturn(WooPosFullSyncRequirement.Error("No site selected"))
114112
val worker = createWorker()
115113

116114
// WHEN
117115
val result = worker.doWork()
118116

119117
// THEN
120-
assertThat(result).isEqualTo(ListenableWorker.Result.failure())
121-
verify(syncRepository, never()).syncLocalCatalogFull(any())
122-
}
123-
124-
@Test
125-
fun `when no site selected, then returns failure`() = testBlocking {
126-
// GIVEN
127-
whenever(selectedSite.getOrNull()).thenReturn(null)
128-
val worker = createWorker()
129-
130-
// WHEN
131-
val result = worker.doWork()
132-
133-
// THEN
134-
assertThat(result).isEqualTo(ListenableWorker.Result.failure())
118+
assertThat(result).isEqualTo(ListenableWorker.Result.retry())
135119
verify(syncRepository, never()).syncLocalCatalogFull(any())
136120
}
137121

@@ -276,4 +260,90 @@ class WooPosLocalCatalogSyncWorkerTest : BaseUnitTest() {
276260
verify(syncRepository).syncLocalCatalogFull(eq(site))
277261
verify(syncRepository).syncLocalCatalogIncremental(eq(site))
278262
}
263+
264+
@Test
265+
fun `given sync not required, when validateSyncStatus is called, then returns success without syncing`() = testBlocking {
266+
// GIVEN
267+
val lastSyncTimestamp = CURRENT_TIME_MILLIS - (3 * 24 * 60 * 60 * 1000L) // 3 days ago
268+
whenever(syncStatusChecker.checkSyncRequirement())
269+
.thenReturn(WooPosFullSyncRequirement.NotRequired(lastSyncTimestamp))
270+
271+
val worker = createWorker()
272+
273+
// WHEN
274+
val result = worker.doWork()
275+
276+
// THEN
277+
assertThat(result).isEqualTo(ListenableWorker.Result.success())
278+
verify(syncRepository, never()).syncLocalCatalogFull(any())
279+
verify(syncRepository, never()).syncLocalCatalogIncremental(any())
280+
}
281+
282+
@Test
283+
fun `given local catalog disabled, when validateSyncStatus is called, then returns success without syncing`() = testBlocking {
284+
// GIVEN
285+
whenever(syncStatusChecker.checkSyncRequirement())
286+
.thenReturn(WooPosFullSyncRequirement.LocalCatalogDisabled("Catalog too large"))
287+
288+
val worker = createWorker()
289+
290+
// WHEN
291+
val result = worker.doWork()
292+
293+
// THEN
294+
assertThat(result).isEqualTo(ListenableWorker.Result.success())
295+
verify(syncRepository, never()).syncLocalCatalogFull(any())
296+
verify(syncRepository, never()).syncLocalCatalogIncremental(any())
297+
}
298+
299+
@Test
300+
fun `given sync requirement check error, when validateSyncStatus is called, then returns retry`() = testBlocking {
301+
// GIVEN
302+
whenever(syncStatusChecker.checkSyncRequirement())
303+
.thenReturn(WooPosFullSyncRequirement.Error("No network connection"))
304+
305+
val worker = createWorker()
306+
307+
// WHEN
308+
val result = worker.doWork()
309+
310+
// THEN
311+
assertThat(result).isEqualTo(ListenableWorker.Result.retry())
312+
verify(syncRepository, never()).syncLocalCatalogFull(any())
313+
verify(syncRepository, never()).syncLocalCatalogIncremental(any())
314+
}
315+
316+
@Test
317+
fun `given blocking sync required, when validateSyncStatus is called, then proceeds with sync`() = testBlocking {
318+
// GIVEN
319+
whenever(syncStatusChecker.checkSyncRequirement())
320+
.thenReturn(WooPosFullSyncRequirement.BlockingRequired)
321+
322+
val worker = createWorker()
323+
324+
// WHEN
325+
val result = worker.doWork()
326+
327+
// THEN
328+
assertThat(result).isEqualTo(ListenableWorker.Result.success())
329+
verify(syncRepository).syncLocalCatalogFull(eq(site))
330+
verify(syncRepository).syncLocalCatalogIncremental(eq(site))
331+
}
332+
333+
@Test
334+
fun `given sync overdue, when validateSyncStatus is called, then proceeds with sync`() = testBlocking {
335+
// GIVEN
336+
whenever(syncStatusChecker.checkSyncRequirement())
337+
.thenReturn(WooPosFullSyncRequirement.Overdue)
338+
339+
val worker = createWorker()
340+
341+
// WHEN
342+
val result = worker.doWork()
343+
344+
// THEN
345+
assertThat(result).isEqualTo(ListenableWorker.Result.success())
346+
verify(syncRepository).syncLocalCatalogFull(eq(site))
347+
verify(syncRepository).syncLocalCatalogIncremental(eq(site))
348+
}
279349
}

0 commit comments

Comments
 (0)