-
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.
- Loading branch information
Showing
5 changed files
with
78 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
17 changes: 0 additions & 17 deletions
17
app/src/test/java/com/example/kocelainterview/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
.../test/java/com/example/kocelainterview/presentation/ships_screen/ShipListViewModelTest.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,72 @@ | ||
package com.example.kocelainterview.presentation.ships_screen | ||
|
||
import com.example.kocelainterview.common.core.Resource | ||
import com.example.kocelainterview.domain.model.Ship | ||
import com.example.kocelainterview.domain.use_case.get_ships.GetShipsUseCase | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import kotlinx.coroutines.test.setMain | ||
import org.junit.Assert.* | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class ShipListViewModelTest { | ||
|
||
private lateinit var viewModel: ShipListViewModel | ||
private lateinit var getShipsUseCase: GetShipsUseCase | ||
|
||
@Before | ||
fun setup() { | ||
Dispatchers.setMain(Dispatchers.Unconfined) | ||
getShipsUseCase = mockk(relaxed = true) // Use relaxed mode to handle final classes | ||
viewModel = ShipListViewModel(getShipsUseCase) | ||
} | ||
|
||
@Test | ||
fun testLoadingState(): Unit = runBlockingTest { | ||
// Given | ||
val loadingStateFlow = flow<Resource<List<Ship>>> { emit(Resource.Loading()) } | ||
every { getShipsUseCase() } returns loadingStateFlow | ||
|
||
// When | ||
viewModel.getShips() | ||
|
||
// Then | ||
assertEquals(ShipListState(isLoading = true), viewModel.state.value) | ||
} | ||
|
||
@Test | ||
fun testSuccessState(): Unit = runBlockingTest { | ||
// Given | ||
val mockShips = listOf(Ship( | ||
true, "image1.jpg", "ship_id_1", "Ship 1", 1000, 2020 | ||
)) | ||
val successStateFlow = flow<Resource<List<Ship>>> { emit(Resource.Success(mockShips)) } | ||
every { getShipsUseCase() } returns successStateFlow | ||
|
||
// When | ||
viewModel.getShips() | ||
|
||
// Then | ||
assertEquals(ShipListState(ships = mockShips), viewModel.state.value) | ||
} | ||
|
||
@Test | ||
fun testErrorState(): Unit = runBlockingTest { | ||
// Given | ||
val errorMessage = "An error occurred" | ||
val errorStateFlow = flow<Resource<List<Ship>>> { emit(Resource.Error(errorMessage)) } | ||
every { getShipsUseCase() } returns errorStateFlow | ||
|
||
// When | ||
viewModel.getShips() | ||
|
||
// Then | ||
assertEquals(ShipListState(error = errorMessage), viewModel.state.value) | ||
} | ||
} | ||
|
||
|
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