-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87d187d
commit 6ca256b
Showing
3 changed files
with
84 additions
and
9 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...test/src/commonTest/kotlin/xyz/ksharma/core/test/viewmodels/ServiceAlertsViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters