Skip to content

Commit

Permalink
[#3916] Ensure that base changes happens only once
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaozhan committed Sep 14, 2024
1 parent eea55e3 commit f22ce8f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ internal class WidgetViewModelTest {
verifySuspend(VerifyMode.not) { currencyDataSource.getActiveCurrencies() }

verify(VerifyMode.not) { calculationStorage.currentBase = any<String>() }

verify(VerifyMode.not) { analyticsManager.trackEvent(Event.BaseChange(Param.Base(any<String>()))) }
verify(VerifyMode.not) { analyticsManager.setUserProperty(UserProperty.BaseCurrency(any<String>())) }
}
Expand All @@ -242,10 +241,23 @@ internal class WidgetViewModelTest {

verifySuspend { currencyDataSource.getActiveCurrencies() }

verify { calculationStorage.currentBase = lastBase }

verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(lastBase))) }
verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(lastBase)) }
verify(VerifyMode.exactly(1)) { calculationStorage.currentBase = lastBase }
verify(VerifyMode.exactly(1)) {
analyticsManager.trackEvent(
Event.BaseChange(
Param.Base(
lastBase
)
)
)
}
verify(VerifyMode.exactly(1)) {
analyticsManager.setUserProperty(
UserProperty.BaseCurrency(
lastBase
)
)
}

every { calculationStorage.currentBase }
.returns(lastBase)
Expand All @@ -254,10 +266,23 @@ internal class WidgetViewModelTest {

verifySuspend { currencyDataSource.getActiveCurrencies() }

verify { calculationStorage.currentBase = firstBase }

verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(firstBase))) }
verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(firstBase)) }
verify(VerifyMode.exactly(1)) { calculationStorage.currentBase = firstBase }
verify(VerifyMode.exactly(1)) {
analyticsManager.trackEvent(
Event.BaseChange(
Param.Base(
firstBase
)
)
)
}
verify(VerifyMode.exactly(1)) {
analyticsManager.setUserProperty(
UserProperty.BaseCurrency(
firstBase
)
)
}
}

@Test
Expand All @@ -270,9 +295,23 @@ internal class WidgetViewModelTest {

verifySuspend { currencyDataSource.getActiveCurrencies() }

verify { calculationStorage.currentBase = firstBase }
verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(firstBase))) }
verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(firstBase)) }
verify(VerifyMode.exactly(1)) { calculationStorage.currentBase = firstBase }
verify(VerifyMode.exactly(1)) {
analyticsManager.trackEvent(
Event.BaseChange(
Param.Base(
firstBase
)
)
)
}
verify(VerifyMode.exactly(1)) {
analyticsManager.setUserProperty(
UserProperty.BaseCurrency(
firstBase
)
)
}

every { calculationStorage.currentBase }
.returns(firstBase)
Expand All @@ -281,9 +320,23 @@ internal class WidgetViewModelTest {

verifySuspend { currencyDataSource.getActiveCurrencies() }

verify { calculationStorage.currentBase = lastBase }
verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(lastBase))) }
verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(lastBase)) }
verify(VerifyMode.exactly(1)) { calculationStorage.currentBase = lastBase }
verify(VerifyMode.exactly(1)) {
analyticsManager.trackEvent(
Event.BaseChange(
Param.Base(
lastBase
)
)
)
}
verify(VerifyMode.exactly(1)) {
analyticsManager.setUserProperty(
UserProperty.BaseCurrency(
lastBase
)
)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class CalculatorViewModel(
calculationStorage.currentBase
.takeIf { it != state.value.base }
?.let {
currentBaseChanged(it)
setState { copy(base = it) }
calculateOutput(state.value.input)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import dev.mokkery.every
import dev.mokkery.everySuspend
import dev.mokkery.mock
import dev.mokkery.verify
import dev.mokkery.verify.VerifyMode
import dev.mokkery.verifySuspend
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.firstOrNull
Expand Down Expand Up @@ -492,6 +493,7 @@ internal class CalculatorViewModelTest {
}
}

@Suppress("LongMethod")
@Test
fun onSheetDismissed() = runTest {
// when base in state is equal to base in storage
Expand All @@ -509,11 +511,25 @@ internal class CalculatorViewModelTest {
assertEquals(currency1.code, viewModel.data.conversion!!.base)
assertEquals(currency1.code, it.base)

verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(currency1.code))) }
verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(currency1.code)) }
verify(VerifyMode.exactly(1)) {
analyticsManager.trackEvent(
Event.BaseChange(
Param.Base(
currency1.code
)
)
)
}
verify(VerifyMode.exactly(1)) {
analyticsManager.setUserProperty(
UserProperty.BaseCurrency(
currency1.code
)
)
}

// from initial state
verify { calculationStorage.currentBase = currency1.code }
verify(VerifyMode.exactly(1)) { calculationStorage.currentBase = currency1.code }
}

// when base in state is not equal to base in storage
Expand All @@ -534,9 +550,23 @@ internal class CalculatorViewModelTest {
assertEquals(currency2.code, viewModel.data.conversion!!.base)
assertEquals(currency2.code, it.base)

verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(currency2.code))) }
verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(currency2.code)) }
verify { calculationStorage.currentBase = currency2.code }
verify(VerifyMode.exactly(1)) {
analyticsManager.trackEvent(
Event.BaseChange(
Param.Base(
currency2.code
)
)
)
}
verify(VerifyMode.exactly(1)) {
analyticsManager.setUserProperty(
UserProperty.BaseCurrency(
currency2.code
)
)
}
verify(VerifyMode.exactly(1)) { calculationStorage.currentBase = currency2.code }
}
}
}

0 comments on commit f22ce8f

Please sign in to comment.