From 370ee08a62d39d27833971c62c18e98cb05ce3fd Mon Sep 17 00:00:00 2001 From: Naphtali Makori Date: Wed, 25 Jan 2023 22:10:19 +0300 Subject: [PATCH 1/2] add TreeImageReviewViewModel Test --- app/build.gradle | 1 + .../capture/TreeImageReviewViewModelTest.kt | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 app/src/test/java/org/greenstand/android/TreeTracker/capture/TreeImageReviewViewModelTest.kt diff --git a/app/build.gradle b/app/build.gradle index cf333af94..9c5329445 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -225,6 +225,7 @@ dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0' implementation 'org.jetbrains.kotlinx:kotlinx-datetime:0.3.2' + testImplementation 'junit:junit:4.12' coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5") //Database diff --git a/app/src/test/java/org/greenstand/android/TreeTracker/capture/TreeImageReviewViewModelTest.kt b/app/src/test/java/org/greenstand/android/TreeTracker/capture/TreeImageReviewViewModelTest.kt new file mode 100644 index 000000000..9ac99b3ce --- /dev/null +++ b/app/src/test/java/org/greenstand/android/TreeTracker/capture/TreeImageReviewViewModelTest.kt @@ -0,0 +1,75 @@ +package org.greenstand.android.TreeTracker.capture + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.mockk +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runBlockingTest +import org.greenstand.android.TreeTracker.MainCoroutineRule +import org.greenstand.android.TreeTracker.models.TreeCapturer +import org.greenstand.android.TreeTracker.models.UserRepo +import org.greenstand.android.TreeTracker.utils.getOrAwaitValueTest +import org.junit.Assert.* +import org.junit.Before +import org.junit.Rule +import org.junit.Test + +@ExperimentalCoroutinesApi +class TreeImageReviewViewModelTest { + + @get:Rule + val instantTaskExecutorRule = InstantTaskExecutorRule() + + @get:Rule + val mainCoroutineRule = MainCoroutineRule() + + private val treeCapturer = mockk() + private val userRepo = mockk() + + private lateinit var treeImageReviewViewModel: TreeImageReviewViewModel + + + @Before + fun setUp() { + treeImageReviewViewModel = TreeImageReviewViewModel(treeCapturer, userRepo) + } + + @Test + fun `updateNote should update state`() = runBlockingTest { + val note = "note" + treeImageReviewViewModel.updateNote(note) + assertEquals(note, treeImageReviewViewModel.state.getOrAwaitValueTest().note) + } + + @Test + fun `updateNote review tutorial dialog, returns true`() = runBlockingTest { + treeImageReviewViewModel.updateReviewTutorialDialog(true) + treeImageReviewViewModel.state.getOrAwaitValueTest().showReviewTutorial?.let { assert(it) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().showReviewTutorial}" } } + } + + @Test + fun `updateNote review tutorial dialog, returns false`() = runBlockingTest { + treeImageReviewViewModel.updateReviewTutorialDialog(false) + assert(!treeImageReviewViewModel.state.getOrAwaitValueTest().showReviewTutorial!!) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().showReviewTutorial}" } + } + + @Test + fun `addNote setsNote And ClosesDialog, returns false`() = runBlockingTest { + treeImageReviewViewModel.addNote() + assert(!treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen}" } + } + + @Test + fun `set Dialog state, check if dialog is open, returns true`() = runBlockingTest { + treeImageReviewViewModel.setDialogState(true) + assert(treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen}" } + } + + @Test + fun `set Dialog state, check if dialog is closed, returns false`() = runBlockingTest { + treeImageReviewViewModel.setDialogState(false) + assert(!treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen}" } + } + +} \ No newline at end of file From ae54d937442396d40ab90f0caa38eee2550f31de Mon Sep 17 00:00:00 2001 From: Naphtali Makori Date: Fri, 27 Jan 2023 23:00:36 +0300 Subject: [PATCH 2/2] remove unnecessary dependencies, remove ! in asserts --- app/build.gradle | 1 - .../TreeTracker/capture/TreeImageReviewViewModelTest.kt | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9c5329445..cf333af94 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -225,7 +225,6 @@ dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0' implementation 'org.jetbrains.kotlinx:kotlinx-datetime:0.3.2' - testImplementation 'junit:junit:4.12' coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5") //Database diff --git a/app/src/test/java/org/greenstand/android/TreeTracker/capture/TreeImageReviewViewModelTest.kt b/app/src/test/java/org/greenstand/android/TreeTracker/capture/TreeImageReviewViewModelTest.kt index 9ac99b3ce..f103c5bfd 100644 --- a/app/src/test/java/org/greenstand/android/TreeTracker/capture/TreeImageReviewViewModelTest.kt +++ b/app/src/test/java/org/greenstand/android/TreeTracker/capture/TreeImageReviewViewModelTest.kt @@ -51,13 +51,13 @@ class TreeImageReviewViewModelTest { @Test fun `updateNote review tutorial dialog, returns false`() = runBlockingTest { treeImageReviewViewModel.updateReviewTutorialDialog(false) - assert(!treeImageReviewViewModel.state.getOrAwaitValueTest().showReviewTutorial!!) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().showReviewTutorial}" } + assert(treeImageReviewViewModel.state.getOrAwaitValueTest().showReviewTutorial == false) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().showReviewTutorial}" } } @Test fun `addNote setsNote And ClosesDialog, returns false`() = runBlockingTest { treeImageReviewViewModel.addNote() - assert(!treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen}" } + assert(treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen == false) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen}" } } @Test @@ -69,7 +69,7 @@ class TreeImageReviewViewModelTest { @Test fun `set Dialog state, check if dialog is closed, returns false`() = runBlockingTest { treeImageReviewViewModel.setDialogState(false) - assert(!treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen}" } + assert(treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen == false) { "${treeImageReviewViewModel.state.getOrAwaitValueTest().isDialogOpen}" } } } \ No newline at end of file