Skip to content

Commit

Permalink
feat: add event-detail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isfaaghyth authored Aug 25, 2024
1 parent 82f0d7c commit d01e01e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
9 changes: 7 additions & 2 deletions app/src/commonMain/kotlin/id/gdg/app/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AppViewModel(
ChapterUiModel.Default
)

private var _eventDetailUiState = MutableStateFlow(EventDetailUiModel())
private var _eventDetailUiState = MutableStateFlow(EventDetailUiModel.Empty)
val eventDetailUiState get() = _eventDetailUiState.asStateFlow()

init {
Expand Down Expand Up @@ -135,12 +135,17 @@ class AppViewModel(
}

private fun fetchEventDetail(eventId: Int) {
_eventDetailUiState.update { it.copy(state = UiState.Loading) }

viewModelScope.launch {
val result = eventDetailUseCase(eventId)

withContext(Dispatchers.Main) {
_eventDetailUiState.update {
it.copy(detail = result.getOrNull())
it.copy(
state = result.asUiState(),
detail = result.getOrNull()
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package id.gdg.app.ui.state

import id.gdg.app.ui.state.common.UiState
import id.gdg.event.model.EventDetailModel

data class EventDetailUiModel(
val state: UiState,
val detail: EventDetailModel? = null
)
) {

companion object {
val Empty get() = EventDetailUiModel(
state = UiState.Loading,
detail = null
)
}
}
47 changes: 47 additions & 0 deletions app/src/commonTest/kotlin/id/gdg/app/AppViewModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,51 @@ class AppViewModelTest : KoinTest {
}
}
}

@Test
fun `when EventDetail is invoked then the the event details is returned`() {
val expectedValue = EventDetailModel(
title = "DevFest!"
)

robot.eventDetailUseCase.setData(Result.success(expectedValue))

viewModel.sendEvent(AppEvent.EventDetail(1))

runBlocking {
viewModel.eventDetailUiState.test {
val actualValue = expectMostRecentItem()
assertTrue { actualValue.state is UiState.Success }
assertTrue { actualValue.detail != null }
}
}
}

@Test
fun `when EventDetail is invoked and invalid event id then an empty event detail is returned`() {
robot.eventDetailUseCase.setData(Result.success(null))

viewModel.sendEvent(AppEvent.EventDetail(-1))

runBlocking {
viewModel.eventDetailUiState.test {
val actualValue = expectMostRecentItem()
assertTrue { actualValue.state is UiState.Success }
assertTrue { actualValue.detail == null }
}
}
}

@Test
fun `when EventDetail is invoked and a network error occurs then a Fail state is returned`() {
robot.eventDetailUseCase.setData(Result.failure(Throwable("network error")))

viewModel.sendEvent(AppEvent.EventDetail(0))

runBlocking {
viewModel.eventDetailUiState.test {
assertTrue { expectMostRecentItem().state is UiState.Fail }
}
}
}
}

0 comments on commit d01e01e

Please sign in to comment.