Skip to content

Commit

Permalink
Fix and add tests in ReportProblemModelTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun authored and albin-mullvad committed Nov 17, 2023
1 parent 72afbbf commit a3502f5
Showing 1 changed file with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,14 +24,18 @@ 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

@Before
fun setUp() {
MockKAnnotations.init(this)
coEvery { mockMullvadProblemReport.collectLogs() } returns true
viewModel = ReportProblemViewModel(mockMullvadProblemReport)
coEvery { mockProblemReportRepository.problemReport } returns problemReportFlow
viewModel = ReportProblemViewModel(mockMullvadProblemReport, mockProblemReportRepository)
}

@After
Expand Down Expand Up @@ -114,4 +122,43 @@ class ReportProblemModelTest {
)
}
}

@Test
fun testUpdateEmail() = runTest {
// Arrange
val email = "[email protected]"

// 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("[email protected]", "My description")

// Act, Assert
viewModel.uiState.test {
awaitItem()
problemReportFlow.value = userReport
val result = awaitItem()
assertEquals(userReport.email, result.email)
assertEquals(userReport.description, result.description)
}
}
}

0 comments on commit a3502f5

Please sign in to comment.