From a3502f553d2e82e72a9dec93c3dcc0275253bc04 Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Fri, 17 Nov 2023 13:30:06 +0100 Subject: [PATCH] Fix and add tests in ReportProblemModelTest --- .../viewmodel/ReportProblemModelTest.kt | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/ReportProblemModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/ReportProblemModelTest.kt index 4f91189d0a42..e0b1c5274c5e 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/ReportProblemModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/ReportProblemModelTest.kt @@ -5,12 +5,16 @@ import app.cash.turbine.test import io.mockk.MockKAnnotations import io.mockk.coEvery import io.mockk.impl.annotations.MockK +import io.mockk.verify import kotlin.test.assertEquals import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.test.runTest import net.mullvad.mullvadvpn.dataproxy.MullvadProblemReport import net.mullvad.mullvadvpn.dataproxy.SendProblemReportResult +import net.mullvad.mullvadvpn.dataproxy.UserReport import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule +import net.mullvad.mullvadvpn.repository.ProblemReportRepository import org.junit.After import org.junit.Before import org.junit.Rule @@ -20,6 +24,9 @@ class ReportProblemModelTest { @get:Rule val testCoroutineRule = TestCoroutineRule() @MockK private lateinit var mockMullvadProblemReport: MullvadProblemReport + @MockK(relaxed = true) private lateinit var mockProblemReportRepository: ProblemReportRepository + + private val problemReportFlow = MutableStateFlow(UserReport("", "")) private lateinit var viewModel: ReportProblemViewModel @@ -27,7 +34,8 @@ class ReportProblemModelTest { fun setUp() { MockKAnnotations.init(this) coEvery { mockMullvadProblemReport.collectLogs() } returns true - viewModel = ReportProblemViewModel(mockMullvadProblemReport) + coEvery { mockProblemReportRepository.problemReport } returns problemReportFlow + viewModel = ReportProblemViewModel(mockMullvadProblemReport, mockProblemReportRepository) } @After @@ -114,4 +122,43 @@ class ReportProblemModelTest { ) } } + + @Test + fun testUpdateEmail() = runTest { + // Arrange + val email = "my@email.com" + + // Act + viewModel.updateEmail(email) + + // Assert + verify { mockProblemReportRepository.setEmail(email) } + } + + @Test + fun testUpdateDescription() = runTest { + // Arrange + val description = "My description" + + // Act + viewModel.updateDescription(description) + + // Assert + verify { mockProblemReportRepository.setDescription(description) } + } + + @Test + fun testUpdateProblemReport() = runTest { + // Arrange + val userReport = UserReport("my@email.com", "My description") + + // Act, Assert + viewModel.uiState.test { + awaitItem() + problemReportFlow.value = userReport + val result = awaitItem() + assertEquals(userReport.email, result.email) + assertEquals(userReport.description, result.description) + } + } }