-
Notifications
You must be signed in to change notification settings - Fork 32
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
1 parent
05c7c41
commit ab09419
Showing
3 changed files
with
122 additions
and
1 deletion.
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
106 changes: 106 additions & 0 deletions
106
app/src/test/java/com/github/odaridavid/weatherapp/MainViewModelIntegrationTest.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,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) | ||
} | ||
} |
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