Skip to content

Commit

Permalink
Add UT for isAtcive flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Jan 26, 2025
1 parent 96772c6 commit 533f406
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -33,6 +34,7 @@ import xyz.ksharma.krail.trip.planner.ui.state.timetable.TimeTableUiEvent
import xyz.ksharma.krail.trip.planner.ui.state.timetable.Trip
import xyz.ksharma.krail.trip.planner.ui.timetable.TimeTableViewModel
import xyz.ksharma.krail.trip.planner.ui.timetable.TimeTableViewModel.Companion.JOURNEY_ENDED_CACHE_THRESHOLD_TIME
import xyz.ksharma.krail.trip.planner.ui.timetable.TimeTableViewModel.Companion.REFRESH_TIME_TEXT_DURATION
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand Down Expand Up @@ -91,6 +93,30 @@ class TimeTableViewModelTest {
}
}

// region Test isActive Flow

@Test
fun `GIVEN journeyList is empty in UI State WHEN REFRESH_TIME_TEXT_DURATION passes THEN updateTimeText is not called`() =
runTest {
// GIVEN No Journey list in UI State

// THEN
viewModel.isActive.test {

skipItems(1) // initial state

advanceTimeBy(REFRESH_TIME_TEXT_DURATION.inWholeMilliseconds)
expectNoEvents()

advanceTimeBy(REFRESH_TIME_TEXT_DURATION.inWholeMilliseconds)
expectNoEvents()

cancelAndConsumeRemainingEvents()
}
}

// endregion

// region Test for fetchTrip / Trip API call

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ class TimeTableViewModel(
log("DateTimeSelectionChanged: $item")
// Verify if date time selection has actually changed, otherwise, api will be called unnecessarily.
if (dateTimeSelectionItem != item) {
println("Loading True")
updateUiState { copy(isLoading = true) }
dateTimeSelectionItem = item
journeys.clear() // Clear cache trips when date time selection changed.
Expand Down Expand Up @@ -449,7 +448,8 @@ class TimeTableViewModel(

companion object {
private const val ANR_TIMEOUT = 5000L
private val REFRESH_TIME_TEXT_DURATION = 10.seconds
@VisibleForTesting
val REFRESH_TIME_TEXT_DURATION = 10.seconds
private val AUTO_REFRESH_TIME_TABLE_DURATION = 30.seconds
private val STOP_TIME_TEXT_UPDATES_THRESHOLD = 3.seconds

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,9 @@ private fun TripResponse.Leg.toUiModel(): TimeTableState.JourneyCardInfo.Leg? {
}

else -> { // Public Transport Leg
// log("FFF PTLeg: $displayDuration, leg: ${this.destination?.name} ")
if (transportMode != null && lineName != null && displayText != null &&
numberOfStops != null && stops != null && displayDuration != null
) {
println("Adding Transport Leg")
TimeTableState.JourneyCardInfo.Leg.TransportLeg(
transportModeLine = TransportModeLine(
transportMode = transportMode,
Expand All @@ -199,7 +197,7 @@ private fun TripResponse.Leg.toUiModel(): TimeTableState.JourneyCardInfo.Leg? {
tripId = transportation?.id + transportation?.properties?.realtimeTripId,
)
} else {
println("Something is null - NOT adding Transport LEG: " +
log("Something is null - NOT adding Transport LEG: " +
"TransportMode: $transportMode, lineName: $lineName, displayText: $displayText, " +
"numberOfStops: $numberOfStops, stops: $stops, displayDuration: $displayDuration",
)
Expand Down

0 comments on commit 533f406

Please sign in to comment.