Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odaridavid committed Mar 12, 2024
1 parent 05c7c41 commit ab09419
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class SettingsViewModel @Inject constructor(
}

private fun mapExcludedDataToDisplayValue(excludedData: List<ExcludedData>): String {
return excludedData.joinToString { it.name }
return excludedData.joinToString(separator = ",") { it.value.trim() }
}

private fun mapStringToExcludedData(excludedData: String): List<ExcludedData> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.github.odaridavid.weatherapp

import app.cash.turbine.test
import com.github.odaridavid.weatherapp.core.api.Logger
import com.github.odaridavid.weatherapp.core.api.SettingsRepository
import com.github.odaridavid.weatherapp.rules.MainCoroutineRule
import io.mockk.coEvery
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test

class MainViewModelIntegrationTest {

@OptIn(ExperimentalCoroutinesApi::class)
@get:Rule
val coroutineRule = MainCoroutineRule()

@MockK
val logger = mockk<Logger>().apply {
every { logException(any()) } returns Unit
}

@MockK
val settingsRepository = mockk<SettingsRepository>().apply {
coEvery { setDefaultLocation(any()) } returns Unit
}

@Test
fun `when we grant permission, then the state is updated as expected`() = runTest {
val viewModel = createMainViewModel()

viewModel.processIntent(MainViewIntent.GrantPermission(true))

viewModel.state.test {
awaitItem().also { state ->
assert(state.isPermissionGranted)
}
}
}

@Test
fun `when we check location settings, then the state is updated as expected`() = runTest {
val viewModel = createMainViewModel()

viewModel.processIntent(MainViewIntent.CheckLocationSettings(true))

viewModel.state.test {
awaitItem().also { state ->
assert(state.isLocationSettingEnabled)
}
}
}

// TODO Use parameterized tests
@Test
fun `when we deny permission, then the state is updated as expected`() = runTest {
val viewModel = createMainViewModel()

viewModel.processIntent(MainViewIntent.GrantPermission(false))

viewModel.state.test {
awaitItem().also { state ->
assert(!state.isPermissionGranted)
}
}
}

@Test
fun `when we log an exception,then the right methods are called`() = runTest {
val viewModel = createMainViewModel(
logger = logger,
)
viewModel.state.test {
viewModel.processIntent(MainViewIntent.LogException(Exception("Test")))
awaitItem()
}

verify { logger.logException(any()) }
}

@Test
fun `when we receive a location, then the state is updated as expected`() = runTest {
val viewModel = createMainViewModel(
settingsRepository = settingsRepository,
)
viewModel.state.test {
viewModel.processIntent(MainViewIntent.ReceiveLocation(0.0, 0.0))
awaitItem().also { state ->
assert(state.defaultLocation?.latitude == 0.0)
assert(state.defaultLocation?.longitude == 0.0)
}
}
}

private fun createMainViewModel(
settingsRepository: SettingsRepository = mockk(),
logger: Logger = mockk(),
): MainViewModel {
return MainViewModel(settingsRepository = settingsRepository, logger = logger)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ class SettingsViewModelIntegrationTest {
}
}

@Test
fun `when we change excluded data, then the excluded data is updated `() = runBlocking {
val settingsViewModel = createSettingsScreenViewModel()

settingsViewModel.processIntent(SettingsScreenIntent.ChangeExcludedData(selectedExcludedData = listOf(ExcludedData.CURRENT, ExcludedData.DAILY)))

settingsViewModel.state.test {
awaitItem().also { state ->
assert(state.error == null)
assert(state.selectedExcludedData == listOf(ExcludedData.CURRENT, ExcludedData.DAILY))
assert(state.selectedExcludedDataDisplayValue == "current,daily")
}
}
}

private fun createSettingsScreenViewModel(): SettingsViewModel = SettingsViewModel(
settingsRepository = settingsRepository,
logger = logger,
Expand Down

0 comments on commit ab09419

Please sign in to comment.