Skip to content

Commit

Permalink
fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odaridavid committed Mar 11, 2024
1 parent 8283909 commit 3e69d14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import com.github.odaridavid.weatherapp.core.api.Logger
import com.github.odaridavid.weatherapp.core.api.SettingsRepository
import com.github.odaridavid.weatherapp.core.api.WeatherRepository
import com.github.odaridavid.weatherapp.core.model.DefaultLocation
import com.github.odaridavid.weatherapp.core.model.ExcludedData
import com.github.odaridavid.weatherapp.core.model.SupportedLanguage
import com.github.odaridavid.weatherapp.core.model.TimeFormat
import com.github.odaridavid.weatherapp.core.model.Units
import com.github.odaridavid.weatherapp.data.weather.DefaultWeatherRepository
import com.github.odaridavid.weatherapp.data.weather.remote.DefaultRemoteWeatherDataSource
import com.github.odaridavid.weatherapp.data.weather.remote.OpenWeatherService
Expand Down Expand Up @@ -42,7 +45,10 @@ class HomeViewModelIntegrationTest {
val mockLogger = mockk<Logger>(relaxed = true)

@MockK
val mockSettingsRepository = mockk<SettingsRepository>(relaxed = true)
val mockSettingsRepository = mockk<SettingsRepository>(relaxed = true).apply {
coEvery { getFormat() } returns flowOf(TimeFormat.TWELVE_HOUR.value)
coEvery { getExcludedData() } returns flowOf(ExcludedData.NONE.value)
}

@get:Rule
val coroutineRule = MainCoroutineRule()
Expand All @@ -62,8 +68,6 @@ class HomeViewModelIntegrationTest {
fakeSuccessWeatherResponse
)

coEvery { mockSettingsRepository.getFormat() } returns flowOf(TimeFormat.TWELVE_HOUR.value)

val weatherRepository = createWeatherRepository()

val viewModel = createViewModel(
Expand Down Expand Up @@ -192,16 +196,19 @@ class HomeViewModelIntegrationTest {
private fun createViewModel(
weatherRepository: WeatherRepository,
settingsRepository: SettingsRepository = mockk<SettingsRepository>() {
coEvery { getUnits() } returns flowOf("metric")
coEvery { getUnits() } returns flowOf(Units.METRIC.value)
coEvery { getDefaultLocation() } returns flowOf(
DefaultLocation(
longitude = 0.0, latitude = 0.0
)
)
coEvery { getLanguage() } returns flowOf("English")
coEvery { getLanguage() } returns flowOf(SupportedLanguage.ENGLISH.languageName)
coEvery { getAppVersion() } returns "1.0.0"
coEvery { getAvailableLanguages() } returns listOf("English")
coEvery { getAvailableUnits() } returns listOf("metric")
coEvery { getAvailableLanguages() } returns listOf(SupportedLanguage.ENGLISH.languageName)
coEvery { getAvailableUnits() } returns listOf(Units.METRIC.value)
coEvery { getFormats() } returns listOf(TimeFormat.TWENTY_FOUR_HOUR.value)
coEvery { getFormat() } returns flowOf(TimeFormat.TWENTY_FOUR_HOUR.value)
coEvery { getExcludedData() } returns flowOf(ExcludedData.CURRENT.value)
}
): HomeViewModel =
HomeViewModel(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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.core.model.ExcludedData
import com.github.odaridavid.weatherapp.core.model.SupportedLanguage
import com.github.odaridavid.weatherapp.core.model.TimeFormat
import com.github.odaridavid.weatherapp.core.model.Units
Expand All @@ -10,6 +12,8 @@ import com.github.odaridavid.weatherapp.rules.MainCoroutineRule
import com.github.odaridavid.weatherapp.ui.settings.SettingsScreenIntent
import com.github.odaridavid.weatherapp.ui.settings.SettingsScreenViewState
import com.github.odaridavid.weatherapp.ui.settings.SettingsViewModel
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.Rule
Expand All @@ -20,6 +24,9 @@ class SettingsViewModelIntegrationTest {

private val settingsRepository: SettingsRepository = FakeSettingsRepository()

@MockK
private val logger: Logger = mockk()

@get:Rule
val coroutineRule = MainCoroutineRule()

Expand All @@ -36,7 +43,10 @@ class SettingsViewModelIntegrationTest {
availableUnits = Units.entries.map { it.value },
selectedTimeFormat = TimeFormat.TWENTY_FOUR_HOUR.name,
availableFormats = TimeFormat.entries.map { it.value },
versionInfo = "1.0.0"
versionInfo = "1.0.0",
selectedExcludedData = listOf(ExcludedData.MINUTELY, ExcludedData.ALERTS),
excludedData = ExcludedData.entries,
selectedExcludedDataDisplayValue = "minutely,alerts"
)

settingsViewModel.state.test {
Expand Down Expand Up @@ -90,6 +100,7 @@ class SettingsViewModelIntegrationTest {
}

private fun createSettingsScreenViewModel(): SettingsViewModel = SettingsViewModel(
settingsRepository = settingsRepository
settingsRepository = settingsRepository,
logger = logger,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.odaridavid.weatherapp.core.api.Logger
import com.github.odaridavid.weatherapp.core.api.SettingsRepository
import com.github.odaridavid.weatherapp.core.api.WeatherRepository
import com.github.odaridavid.weatherapp.core.model.DefaultLocation
import com.github.odaridavid.weatherapp.core.model.ExcludedData
import com.github.odaridavid.weatherapp.core.model.TimeFormat
import com.github.odaridavid.weatherapp.data.weather.DefaultWeatherRepository
import com.github.odaridavid.weatherapp.data.weather.remote.DefaultRemoteWeatherDataSource
Expand Down Expand Up @@ -33,7 +34,10 @@ class WeatherRepositoryUnitTest {
val mockOpenWeatherService = mockk<OpenWeatherService>(relaxed = true)

@MockK
val mockSettingsRepository = mockk<SettingsRepository>(relaxed = true)
val mockSettingsRepository = mockk<SettingsRepository>(relaxed = true).apply {
coEvery { getFormat() } returns flowOf(TimeFormat.TWELVE_HOUR.value)
coEvery { getExcludedData() } returns flowOf(ExcludedData.NONE.value)
}

@MockK
val mockLogger = mockk<Logger>(relaxed = true)
Expand All @@ -60,7 +64,6 @@ class WeatherRepositoryUnitTest {
} returns Response.success<WeatherResponse>(
fakeSuccessWeatherResponse
)
coEvery { mockSettingsRepository.getFormat() } returns flowOf(TimeFormat.TWELVE_HOUR.value)

val weatherRepository = createWeatherRepository()

Expand Down

0 comments on commit 3e69d14

Please sign in to comment.