Skip to content

Commit

Permalink
add shipListViewModel test case
Browse files Browse the repository at this point in the history
  • Loading branch information
mutukuian committed May 15, 2024
1 parent 30cea8b commit 53188d1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 20 deletions.
3 changes: 0 additions & 3 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.compose.preview.renderer)
//implementation(libs.androidx.material3.android)
//implementation(libs.androidx.paging.compose)
// implementation(libs.androidx.material3.android)
testImplementation(libs.junit)
testImplementation("junit:junit:4.12")
testImplementation("junit:junit:4.12")
testImplementation("junit:junit:4.12")
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
Expand Down Expand Up @@ -134,6 +136,8 @@ dependencies {
testImplementation("io.mockk:mockk:1.12.0")
androidTestImplementation("io.mockk:mockk-android:1.12.0")

testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.2")




Expand Down
17 changes: 0 additions & 17 deletions app/src/test/java/com/example/kocelainterview/ExampleUnitTest.kt

This file was deleted.

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)
}
}


2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ playServicesLocation = "21.2.0"
material3Android = "1.2.1"
pagingCompose = "3.2.1"
material3AndroidVersion = "1.2.1"
composePreviewRenderer = "0.0.1-alpha01"

[libraries]
accompanist-systemuicontroller = { module = "com.google.accompanist:accompanist-systemuicontroller", version.ref = "accompanistSystemuicontroller" }
Expand Down Expand Up @@ -58,6 +59,7 @@ okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "playServicesLocation" }
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "converterGson" }
androidx-paging-compose = { group = "androidx.paging", name = "paging-compose", version.ref = "pagingCompose" }
compose-preview-renderer = { group = "com.android.tools.compose", name = "compose-preview-renderer", version.ref = "composePreviewRenderer" }



Expand Down

0 comments on commit 53188d1

Please sign in to comment.