Rak
02/08/2022, 2:18 PMRak
02/08/2022, 2:48 PMRak
02/08/2022, 2:49 PMclass AnalyticsConsentViewModel(val prefs: Preferences) : ViewModel(),
ContainerHost<AnalyticsConsentState, NavigationEvent> {
override val container: Container<AnalyticsConsentState, NavigationEvent> =
container(AnalyticsConsentState()) {
getInitialState()
}
private fun getInitialState() = intent {
val hasAnalyticsConsentGiven = prefs.hasAnalyticsConsentGiven()
if (hasAnalyticsConsentGiven == null || hasAnalyticsConsentGiven) {
reduce {
state.copy(consentGivenChecked = true)
}
} else {
reduce {
state.copy(consentGivenChecked = false)
}
}
}
}
data class AnalyticsConsentState(
val consentGivenChecked: Boolean? = null
)
Rak
02/08/2022, 2:49 PMRak
02/08/2022, 2:49 PM@Test
fun `nothing in shared prefs so asking during registration`() = runTest {
// given
val initialState = AnalyticsConsentState()
every { prefs.hasAnalyticsConsentGiven() } returns null
val viewModel = AnalyticsConsentViewModel(prefs).test(initialState)
// when
viewModel.runOnCreate()
// then
viewModel.assert(initialState) {
states(
{ copy(consentGivenChecked = true) }
)
}
}
@Test
fun `analytics consent given`() = runTest {
// given
val initialState = AnalyticsConsentState()
every { prefs.hasAnalyticsConsentGiven() } returns true
val viewModel = AnalyticsConsentViewModel(prefs).test(initialState)
// when
viewModel.runOnCreate()
// then
viewModel.assert(initialState) {
states(
{ copy(consentGivenChecked = true) }
)
}
}
@Test
fun `analytics consent denied`() = runTest {
// given
val initialState = AnalyticsConsentState()
every { prefs.hasAnalyticsConsentGiven() } returns false
val viewModel = AnalyticsConsentViewModel(prefs).test(initialState)
// when
viewModel.runOnCreate()
// then
viewModel.assert(initialState) {
states(
{ copy(consentGivenChecked = false) }
)
}
}