From d372d6ececd73bf64b6024cc59a29a4827ac203d Mon Sep 17 00:00:00 2001 From: "nosho.hiroaki" Date: Thu, 25 Apr 2024 02:16:37 +0900 Subject: [PATCH 1/4] improve BookmarksViewModelTest coverage Change-Id: Ide13abd9a125a337ba9c9fff2136221e16ebd46e --- .../bookmarks/BookmarksViewModelTest.kt | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt index 19ab0ad85..2ce762055 100644 --- a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt +++ b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt @@ -31,7 +31,9 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertIs +import kotlin.test.assertTrue /** * To learn more about how this test handles Flows created with stateIn, see @@ -86,5 +88,53 @@ class BookmarksViewModelTest { val item = viewModel.feedUiState.value assertIs(item) assertEquals(item.feed.size, 0) + assertTrue(viewModel.shouldDisplayUndoBookmark) + } + + @Test + fun whenResourceViewed_setResourcesViewed() = runTest { + val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } + + // Give + newsRepository.sendNewsResources(newsResourcesTestData) + userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true) + val itemBeforeViewed = viewModel.feedUiState.value + check(itemBeforeViewed is Success) + check(!itemBeforeViewed.feed.first().hasBeenViewed) + + // When + viewModel.setNewsResourceViewed(newsResourcesTestData[0].id, true) + + // Then + val item = viewModel.feedUiState.value + assertIs(item) + assertTrue(item.feed.first().hasBeenViewed) + + collectJob.cancel() + } + + @Test + fun whenUndoBookmarkRemoval_thenBookmarkIsRestored() = runTest { + val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } + + // Give + newsRepository.sendNewsResources(newsResourcesTestData) + userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true) + viewModel.removeFromSavedResources(newsResourcesTestData[0].id) + check(viewModel.shouldDisplayUndoBookmark) + val itemBeforeUndo = viewModel.feedUiState.value + check(itemBeforeUndo is Success) + check(itemBeforeUndo.feed.isEmpty()) + + // When + viewModel.undoBookmarkRemoval() + + // Then + assertFalse(viewModel.shouldDisplayUndoBookmark) + val item = viewModel.feedUiState.value + assertIs(item) + assertEquals(item.feed.size, 1) + + collectJob.cancel() } } From 94f86b7975f3ca80ab2bca64b2cf895d5cf96f45 Mon Sep 17 00:00:00 2001 From: "nosho.hiroaki" Date: Sat, 27 Apr 2024 16:46:08 +0900 Subject: [PATCH 2/4] fix test name and typo, and user assertion instead of check method Change-Id: I45e04df676fe9f5e5e893a1fc6e8f54e9e7d7a1a --- .../bookmarks/BookmarksViewModelTest.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt index 2ce762055..645bdb987 100644 --- a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt +++ b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt @@ -92,15 +92,15 @@ class BookmarksViewModelTest { } @Test - fun whenResourceViewed_setResourcesViewed() = runTest { + fun feedUiState_resourceIsViewed_setResourcesViewed() = runTest { val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } - // Give + // Given newsRepository.sendNewsResources(newsResourcesTestData) userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true) val itemBeforeViewed = viewModel.feedUiState.value - check(itemBeforeViewed is Success) - check(!itemBeforeViewed.feed.first().hasBeenViewed) + assertIs(itemBeforeViewed) + assertFalse(itemBeforeViewed.feed.first().hasBeenViewed) // When viewModel.setNewsResourceViewed(newsResourcesTestData[0].id, true) @@ -114,17 +114,17 @@ class BookmarksViewModelTest { } @Test - fun whenUndoBookmarkRemoval_thenBookmarkIsRestored() = runTest { + fun feedUiState_undoneBookmarkRemoval_bookmarkIsRestored() = runTest { val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } - // Give + // Given newsRepository.sendNewsResources(newsResourcesTestData) userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true) viewModel.removeFromSavedResources(newsResourcesTestData[0].id) - check(viewModel.shouldDisplayUndoBookmark) + assertTrue(viewModel.shouldDisplayUndoBookmark) val itemBeforeUndo = viewModel.feedUiState.value - check(itemBeforeUndo is Success) - check(itemBeforeUndo.feed.isEmpty()) + assertIs(itemBeforeUndo) + assertEquals(0, itemBeforeUndo.feed.size) // When viewModel.undoBookmarkRemoval() @@ -133,7 +133,7 @@ class BookmarksViewModelTest { assertFalse(viewModel.shouldDisplayUndoBookmark) val item = viewModel.feedUiState.value assertIs(item) - assertEquals(item.feed.size, 1) + assertEquals(1, item.feed.size) collectJob.cancel() } From 8bb823ede74874958a6825cd7bde3f81eb79112e Mon Sep 17 00:00:00 2001 From: hiroaki404 Date: Wed, 11 Dec 2024 19:28:45 +0900 Subject: [PATCH 3/4] use backgroundScope Change-Id: I78d5e53d343dac7e7d27896c95714a53a9a7229a --- .../feature/bookmarks/BookmarksViewModelTest.kt | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt index 645bdb987..48677e68d 100644 --- a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt +++ b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt @@ -93,7 +93,7 @@ class BookmarksViewModelTest { @Test fun feedUiState_resourceIsViewed_setResourcesViewed() = runTest { - val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } + backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } // Given newsRepository.sendNewsResources(newsResourcesTestData) @@ -109,13 +109,11 @@ class BookmarksViewModelTest { val item = viewModel.feedUiState.value assertIs(item) assertTrue(item.feed.first().hasBeenViewed) - - collectJob.cancel() } @Test fun feedUiState_undoneBookmarkRemoval_bookmarkIsRestored() = runTest { - val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } + launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } // Given newsRepository.sendNewsResources(newsResourcesTestData) @@ -134,7 +132,5 @@ class BookmarksViewModelTest { val item = viewModel.feedUiState.value assertIs(item) assertEquals(1, item.feed.size) - - collectJob.cancel() } } From 5b99313ae51c3ca8d4272e9c56da29f1ec2753a8 Mon Sep 17 00:00:00 2001 From: hiroaki404 Date: Wed, 11 Dec 2024 20:03:34 +0900 Subject: [PATCH 4/4] fix typo Change-Id: I65dcde81c68c24bfd58f0b66c6ed826e802aff67 --- .../nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt index 48677e68d..aa42adae2 100644 --- a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt +++ b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt @@ -113,7 +113,7 @@ class BookmarksViewModelTest { @Test fun feedUiState_undoneBookmarkRemoval_bookmarkIsRestored() = runTest { - launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } + backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } // Given newsRepository.sendNewsResources(newsResourcesTestData)