Skip to content

Commit

Permalink
feat: 필터링 상태를 ViewModel 에서 관리하지 않는다
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHoonC committed Nov 13, 2023
1 parent f8f44a0 commit b5413d5
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sealed interface FestivalListUiState {
object Loading : FestivalListUiState

data class Success(
val festivals: List<FestivalItemUiState>
val festivals: List<FestivalItemUiState>,
) : FestivalListUiState {
val hasFestival get() = festivals.isNotEmpty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ class FestivalListViewModel @Inject constructor(
private val _event = MutableSharedFlow<FestivalListEvent>()
val event: SharedFlow<FestivalListEvent> = _event.asSharedFlow()

private val _festivalFilter = MutableStateFlow<FestivalFilter>(FestivalFilter.PROGRESS)
val festivalFilter: StateFlow<FestivalFilter> = _festivalFilter.asStateFlow()

fun loadFestivals(festivalFilter: FestivalFilter) {
viewModelScope.launch {
_festivalFilter.value = festivalFilter
festivalRepository.loadFestivals(festivalFilter)
.onSuccess {
_uiState.value = FestivalListUiState.Success(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ class FestivalListViewModelTest {
}

@Test
fun `진행 예정인 축제 목록 받아오기에 성공하면 성공 상태이고 축제 목록을 반환하고 축제 필터가 진행 예정 필터로 바뀐다`() {
fun `진행 예정인 축제 목록 받아오기에 성공하면 성공 상태이고 축제 목록을 반환한다`() {
// given
`축제 목록 요청 결과가 다음과 같을 때`(Result.success(fakeFestivals))
val beforeFilter = vm.festivalFilter.value

// when
vm.loadFestivals(FestivalFilter.PLANNED)
Expand All @@ -73,19 +72,14 @@ class FestivalListViewModelTest {
val actual = (vm.uiState.value as FestivalListUiState.Success).festivals
val expected = fakeFestivals.map { it.toUiState() }
assertThat(actual).isEqualTo(expected)

// and
assertThat(vm.festivalFilter.value).isNotEqualTo(beforeFilter)
assertThat(vm.festivalFilter.value).isEqualTo(FestivalFilter.PLANNED)
}
softly.assertAll()
}

@Test
fun `진행 예정 축제 목록 받아오기에 실패하면 에러 상태이고 진행 예정 필터로 바뀐다`() {
fun `진행 예정 축제 목록 받아오기에 실패하면 에러 상태이다`() {
// given
`축제 목록 요청 결과가 다음과 같을 때`(Result.failure(Exception()))
val beforeFilter = vm.festivalFilter.value

// when
vm.loadFestivals(FestivalFilter.PLANNED)
Expand All @@ -98,10 +92,6 @@ class FestivalListViewModelTest {
assertThat(vm.uiState.value.shouldShowSuccess).isEqualTo(false)
assertThat(vm.uiState.value.shouldShowLoading).isEqualTo(false)
assertThat(vm.uiState.value.shouldShowError).isEqualTo(true)

// and
assertThat(vm.festivalFilter.value).isNotEqualTo(beforeFilter)
assertThat(vm.festivalFilter.value).isEqualTo(FestivalFilter.PLANNED)
}
softly.assertAll()
}
Expand Down

0 comments on commit b5413d5

Please sign in to comment.