Skip to content

Commit

Permalink
Add ut for ServiceAlertVM
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Jan 11, 2025
1 parent 87d187d commit 6ca256b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package xyz.ksharma.core.test.viewmodels

import app.cash.turbine.test
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import xyz.ksharma.core.test.fakes.FakeAnalytics
import xyz.ksharma.core.test.fakes.FakeSandook
import xyz.ksharma.core.test.helpers.AnalyticsTestHelper.assertAnalyticsEventTracked
import xyz.ksharma.krail.sandook.SelectServiceAlertsByJourneyId
import xyz.ksharma.krail.trip.planner.ui.alerts.ServiceAlertsViewModel
import xyz.ksharma.krail.trip.planner.ui.alerts.toServiceAlert
import xyz.ksharma.krail.trip.planner.ui.state.alerts.ServiceAlertState
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalCoroutinesApi::class)
class ServiceAlertsViewModelTest {

private val fakeAnalytics = FakeAnalytics()
private val fakeSandook = FakeSandook()
private lateinit var viewModel: ServiceAlertsViewModel

private val testDispatcher = StandardTestDispatcher()

@BeforeTest
fun setUp() {
Dispatchers.setMain(testDispatcher)
viewModel = ServiceAlertsViewModel(
analytics = fakeAnalytics,
sandook = fakeSandook
)
}

@AfterTest
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun `GIVEN ServiceAlertsViewModel initial state WHEN uiState is collected THEN assert Initial State`() =
runTest {
viewModel.uiState.test {
awaitItem().run {
assertEquals(ServiceAlertState(), this)
}

advanceUntilIdle()
assertAnalyticsEventTracked(fakeAnalytics, "view_screen", "ServiceAlerts")

cancelAndIgnoreRemainingEvents()
}
}

@Test
fun `GIVEN journeyId WHEN fetchAlerts is called THEN alerts are returned`() =
runTest {
val journeyId = "testJourneyId"
val expectedAlerts = listOf(
SelectServiceAlertsByJourneyId(
heading = "Alert 1",
message = "Message 1",
journeyId = "1",
),
SelectServiceAlertsByJourneyId(
heading = "Alert 2",
message = "Message 2",
journeyId = "2",
)
)

fakeSandook.insertAlerts(journeyId, expectedAlerts)

val alerts = viewModel.fetchAlerts(journeyId)
assertEquals(expectedAlerts.map { it.toServiceAlert() }, alerts)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ class TimeTableViewModelTest {

// need to skip two items, because silentLoading will be toggled, as we manually call fetchTrip()
skipItems(2)
/*
awaitItem().run {
assertTrue(silentLoading)
}
awaitItem().run {
assertFalse(silentLoading)
}
*/

awaitItem().run {
assertFalse(isLoading)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ServiceAlertsViewModel(
}
}

private fun SelectServiceAlertsByJourneyId.toServiceAlert() = ServiceAlert(
fun SelectServiceAlertsByJourneyId.toServiceAlert() = ServiceAlert(
heading = heading,
message = message,
)

0 comments on commit 6ca256b

Please sign in to comment.