-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from andrewafanasenko/develop
Develop
- Loading branch information
Showing
7 changed files
with
590 additions
and
4 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
...est/java/com/example/tvschedule/data/show_details/repository/ShowDetailsRepositoryTest.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,47 @@ | ||
package com.example.tvschedule.data.show_details.repository | ||
|
||
import com.example.tvschedule.data.search.mapper.ShowMapper | ||
import com.example.tvschedule.data.show_details.api.ShowDetailsApi | ||
import com.example.tvschedule.data.util.ModelUtil | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
import org.mockito.Mockito | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
|
||
|
||
class ShowDetailsRepositoryTest { | ||
|
||
private val api = mock<ShowDetailsApi>() | ||
|
||
private val showMapper = mock<ShowMapper>() | ||
|
||
private val showDetailsRepository = ShowDetailsRepositoryImpl( | ||
api = api, | ||
showMapper = showMapper | ||
) | ||
|
||
@Test | ||
fun `get show details success`() = runTest { | ||
whenever(api.getShowDetails(ModelUtil.showId)).thenReturn(ModelUtil.showResponse) | ||
whenever(showMapper.map(ModelUtil.showResponse)).thenReturn(ModelUtil.show) | ||
val result = showDetailsRepository.getShowDetails(ModelUtil.showId) | ||
verify(api, Mockito.times(1)).getShowDetails(ModelUtil.showId) | ||
verify(showMapper, Mockito.times(1)).map(ModelUtil.showResponse) | ||
assertThat(result).isEqualTo(ModelUtil.show) | ||
} | ||
|
||
@Test | ||
fun `get show details failed`() = runTest { | ||
val exception = IllegalStateException("Failed to get show details") | ||
whenever(api.getShowDetails(ModelUtil.showId)).thenThrow(exception) | ||
runCatching { | ||
showDetailsRepository.getShowDetails(ModelUtil.showId) | ||
}.onFailure { e -> | ||
verify(api, Mockito.times(1)).getShowDetails(ModelUtil.showId) | ||
assertThat(e).isEqualTo(exception) | ||
} | ||
} | ||
} |
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
114 changes: 114 additions & 0 deletions
114
app/src/test/java/com/example/tvschedule/presentation/favorite/FavoriteViewModelTest.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,114 @@ | ||
package com.example.tvschedule.presentation.favorite | ||
|
||
import com.example.tvschedule.MainDispatcherRule | ||
import com.example.tvschedule.data.util.ModelUtil | ||
import com.example.tvschedule.domain.favorite.use_case.GetFavoritesUseCase | ||
import com.example.tvschedule.domain.favorite.use_case.RemoveFromFavoritesUseCase | ||
import com.example.tvschedule.presentation.favorite.model.FavoriteUiEvent | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.mockito.Mockito.mock | ||
import org.mockito.kotlin.times | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
|
||
|
||
class FavoriteViewModelTest { | ||
|
||
private val getFavoritesUseCase = mock<GetFavoritesUseCase>() | ||
|
||
private val removeFromFavoritesUseCase = mock<RemoveFromFavoritesUseCase>() | ||
|
||
private lateinit var viewModel : FavoriteViewModel | ||
|
||
@get:Rule | ||
val mainCoroutineRule = MainDispatcherRule() | ||
|
||
@Test | ||
fun `get favorite shows success`() = runTest { | ||
val flow = flow { emit(listOf(ModelUtil.show)) } | ||
whenever(getFavoritesUseCase.invoke()).thenReturn(Result.success(flow)) | ||
viewModel = FavoriteViewModel( | ||
getFavoritesUseCase = getFavoritesUseCase, | ||
removeFromFavoritesUseCase = removeFromFavoritesUseCase | ||
) | ||
verify(getFavoritesUseCase, times(1)).invoke() | ||
val state = viewModel.viewState.value | ||
assertThat(state.isLoading).isFalse() | ||
assertThat(state.isError).isFalse() | ||
assertThat(state.searchQuery).isEmpty() | ||
assertThat(state.shows).isNotEmpty() | ||
} | ||
|
||
@Test | ||
fun `get favorite shows failed`() = runTest { | ||
whenever(getFavoritesUseCase.invoke()) | ||
.thenReturn(Result.failure(IllegalStateException("Failed to get favorites"))) | ||
viewModel = FavoriteViewModel( | ||
getFavoritesUseCase = getFavoritesUseCase, | ||
removeFromFavoritesUseCase = removeFromFavoritesUseCase | ||
) | ||
verify(getFavoritesUseCase, times(1)).invoke() | ||
val state = viewModel.viewState.value | ||
assertThat(state.isLoading).isFalse() | ||
assertThat(state.isError).isTrue() | ||
assertThat(state.searchQuery).isEmpty() | ||
assertThat(state.shows).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `remove from favorite shows success`() = runTest { | ||
val flow = flow { emit(listOf(ModelUtil.show)) } | ||
whenever(getFavoritesUseCase.invoke()).thenReturn(Result.success(flow)) | ||
whenever(removeFromFavoritesUseCase.invoke(ModelUtil.showId)) | ||
.thenReturn(Result.success(Unit)) | ||
viewModel = FavoriteViewModel( | ||
getFavoritesUseCase = getFavoritesUseCase, | ||
removeFromFavoritesUseCase = removeFromFavoritesUseCase | ||
) | ||
viewModel.setEvent(FavoriteUiEvent.OnFavoriteClick(ModelUtil.showId)) | ||
verify(removeFromFavoritesUseCase, times(1)).invoke(ModelUtil.showId) | ||
verify(getFavoritesUseCase, times(1)).invoke() | ||
val state = viewModel.viewState.value | ||
assertThat(state.isLoading).isFalse() | ||
assertThat(state.isError).isFalse() | ||
assertThat(state.searchQuery).isEmpty() | ||
assertThat(state.shows).isNotEmpty() | ||
} | ||
|
||
@Test | ||
fun `handle retry event success`() = runTest { | ||
val flow = flow { emit(listOf(ModelUtil.show)) } | ||
whenever(getFavoritesUseCase.invoke()).thenReturn(Result.success(flow)) | ||
viewModel = FavoriteViewModel( | ||
getFavoritesUseCase = getFavoritesUseCase, | ||
removeFromFavoritesUseCase = removeFromFavoritesUseCase | ||
) | ||
whenever(viewModel.setEvent(FavoriteUiEvent.Retry)).thenReturn(Unit) | ||
verify(getFavoritesUseCase, times(1)).invoke() | ||
val state = viewModel.viewState.value | ||
assertThat(state.isLoading).isFalse() | ||
assertThat(state.isError).isFalse() | ||
assertThat(state.searchQuery).isEmpty() | ||
assertThat(state.shows).isNotEmpty() | ||
} | ||
|
||
@Test | ||
fun `handle query change event success`() = runTest { | ||
val flow = flow { emit(listOf(ModelUtil.show)) } | ||
whenever(getFavoritesUseCase.invoke()).thenReturn(Result.success(flow)) | ||
viewModel = FavoriteViewModel( | ||
getFavoritesUseCase = getFavoritesUseCase, | ||
removeFromFavoritesUseCase = removeFromFavoritesUseCase | ||
) | ||
whenever(viewModel.setEvent(FavoriteUiEvent.OnQueryChange(ModelUtil.searchQuery))) | ||
.thenReturn(Unit) | ||
verify(getFavoritesUseCase, times(0)).invoke() | ||
val state = viewModel.viewState.value | ||
assertThat(state.isError).isFalse() | ||
assertThat(state.searchQuery).isNotEmpty() | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
app/src/test/java/com/example/tvschedule/presentation/schedule/ScheduleViewModelTest.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,78 @@ | ||
package com.example.tvschedule.presentation.schedule | ||
|
||
import com.example.tvschedule.MainDispatcherRule | ||
import com.example.tvschedule.data.util.ModelUtil | ||
import com.example.tvschedule.domain.schedule.use_case.GetScheduleUseCase | ||
import com.example.tvschedule.presentation.schedule.model.ScheduleUiEvent | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.times | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
|
||
|
||
class ScheduleViewModelTest { | ||
|
||
private val getScheduleUseCase = mock<GetScheduleUseCase>() | ||
|
||
private lateinit var viewModel: ScheduleViewModel | ||
|
||
@get:Rule | ||
val mainCoroutineRule = MainDispatcherRule() | ||
|
||
@Test | ||
fun `load schedule success`() = runTest { | ||
whenever(getScheduleUseCase.invoke(any())) | ||
.thenReturn(Result.success(ModelUtil.schedules)) | ||
viewModel = ScheduleViewModel(getScheduleUseCase) | ||
verify(getScheduleUseCase, times(1)).invoke(any()) | ||
val state = viewModel.viewState.value | ||
assertThat(state.isLoading).isFalse() | ||
assertThat(state.isError).isFalse() | ||
assertThat(state.schedule).isNotEmpty() | ||
} | ||
|
||
@Test | ||
fun `load schedule failed`() = runTest { | ||
whenever(getScheduleUseCase.invoke(any())) | ||
.thenReturn(Result.failure(IllegalStateException("Failed to load schedule"))) | ||
viewModel = ScheduleViewModel(getScheduleUseCase) | ||
verify(getScheduleUseCase, times(1)).invoke(any()) | ||
val state = viewModel.viewState.value | ||
assertThat(state.isLoading).isFalse() | ||
assertThat(state.isError).isTrue() | ||
assertThat(state.schedule).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `handle select date event success`() = runTest { | ||
whenever(getScheduleUseCase.invoke(any())) | ||
.thenReturn(Result.success(ModelUtil.schedules)) | ||
viewModel = ScheduleViewModel(getScheduleUseCase) | ||
whenever(viewModel.setEvent(ScheduleUiEvent.SelectDate(ModelUtil.scheduleDate))) | ||
.thenReturn(Unit) | ||
verify(getScheduleUseCase, times(1)).invoke(any()) | ||
val state = viewModel.viewState.value | ||
assertThat(state.isLoading).isFalse() | ||
assertThat(state.isError).isFalse() | ||
assertThat(state.schedule).isNotEmpty() | ||
assertThat(state.selectedDate).isEqualTo(ModelUtil.scheduleDate) | ||
} | ||
|
||
@Test | ||
fun `handle retry event success`() = runTest { | ||
whenever(getScheduleUseCase.invoke(any())) | ||
.thenReturn(Result.success(ModelUtil.schedules)) | ||
viewModel = ScheduleViewModel(getScheduleUseCase) | ||
whenever(viewModel.setEvent(ScheduleUiEvent.Retry)).thenReturn(Unit) | ||
verify(getScheduleUseCase, times(1)).invoke(any()) | ||
val state = viewModel.viewState.value | ||
assertThat(state.isLoading).isFalse() | ||
assertThat(state.isError).isFalse() | ||
assertThat(state.schedule).isNotEmpty() | ||
} | ||
} |
Oops, something went wrong.