Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Add ServiceAlertsViewModel tests and make toServiceAlert public #530

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)
Loading