Fix unit tests

This commit is contained in:
Florian Renaud 2023-02-20 14:02:28 +01:00
parent 13866c62bf
commit cd648a0b50

View File

@ -18,7 +18,7 @@ package im.vector.app.features.settings.notifications
import com.airbnb.mvrx.test.MavericksTestRule
import im.vector.app.features.settings.notifications.usecase.GetPushRulesOnInvalidStateUseCase
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.fixtures.PushRuleFixture
import im.vector.app.test.test
import im.vector.app.test.testDispatcher
@ -34,21 +34,20 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.matrix.android.sdk.api.session.pushrules.RuleIds
import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind
internal class VectorSettingsPushRuleNotificationViewModelTest {
@get:Rule
val mavericksTestRule = MavericksTestRule(testDispatcher = testDispatcher)
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakePushRuleService = fakeActiveSessionHolder.fakeSession.fakePushRuleService
private val fakeSession = FakeSession()
private val fakePushRuleService = fakeSession.fakePushRuleService
private val fakeGetPushRulesOnInvalidStateUseCase = mockk<GetPushRulesOnInvalidStateUseCase>()
private val initialState = VectorSettingsPushRuleNotificationViewState()
private fun createViewModel() = VectorSettingsPushRuleNotificationViewModel(
initialState = initialState,
activeSessionHolder = fakeActiveSessionHolder.instance,
session = fakeSession,
fakeGetPushRulesOnInvalidStateUseCase,
)
@ -70,12 +69,14 @@ internal class VectorSettingsPushRuleNotificationViewModelTest {
val firstRuleId = RuleIds.RULE_ID_ONE_TO_ONE_ROOM
val secondRuleId = RuleIds.RULE_ID_ALL_OTHER_MESSAGES_ROOMS
givenARuleId(firstRuleId)
givenARuleId(secondRuleId)
fakePushRuleService.givenUpdatePushRuleActionsSucceed()
// When
val viewModelTest = viewModel.test()
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(givenARuleId(firstRuleId), true))
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(givenARuleId(secondRuleId), false))
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(firstRuleId, true))
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(secondRuleId, false))
// Then
coVerifyOrder {
@ -116,10 +117,12 @@ internal class VectorSettingsPushRuleNotificationViewModelTest {
val failure = mockk<Throwable>()
val firstRuleId = RuleIds.RULE_ID_ONE_TO_ONE_ROOM
givenARuleId(firstRuleId)
fakePushRuleService.givenUpdatePushRuleActionsSucceed()
fakePushRuleService.givenUpdatePushRuleActionsFail(RuleIds.RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, failure)
val secondRuleId = RuleIds.RULE_ID_ALL_OTHER_MESSAGES_ROOMS
givenARuleId(secondRuleId)
fakePushRuleService.givenUpdatePushRuleActionsFail(secondRuleId, failure)
fakePushRuleService.givenUpdatePushRuleActionsFail(RuleIds.RULE_ID_POLL_START, failure)
fakePushRuleService.givenUpdatePushRuleActionsFail(RuleIds.RULE_ID_POLL_START_UNSTABLE, failure)
@ -129,9 +132,9 @@ internal class VectorSettingsPushRuleNotificationViewModelTest {
// When
val viewModelTest = viewModel.test()
// One rule failed to update
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(givenARuleId(firstRuleId), true))
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(firstRuleId, true))
// All the rules failed to update
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(givenARuleId(secondRuleId), true))
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(secondRuleId, true))
// Then
coVerifyOrder {
@ -154,7 +157,7 @@ internal class VectorSettingsPushRuleNotificationViewModelTest {
.assertStatesChanges(
initialState,
{ copy(isLoading = true) },
{ copy(isLoading = false, setOf(firstRuleId)) },
{ copy(isLoading = false, rulesOnError = setOf(firstRuleId)) },
{ copy(isLoading = true) },
{ copy(isLoading = false) },
)
@ -172,10 +175,12 @@ internal class VectorSettingsPushRuleNotificationViewModelTest {
val failure = mockk<Throwable>()
val firstRuleId = RuleIds.RULE_ID_ONE_TO_ONE_ROOM
givenARuleId(firstRuleId)
fakePushRuleService.givenUpdatePushRuleActionsSucceed()
fakePushRuleService.givenUpdatePushRuleActionsFail(RuleIds.RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, failure)
val secondRuleId = RuleIds.RULE_ID_ALL_OTHER_MESSAGES_ROOMS
givenARuleId(secondRuleId)
fakePushRuleService.givenUpdatePushRuleActionsFail(secondRuleId, failure)
fakePushRuleService.givenUpdatePushRuleActionsFail(RuleIds.RULE_ID_POLL_START, failure)
fakePushRuleService.givenUpdatePushRuleActionsFail(RuleIds.RULE_ID_POLL_START_UNSTABLE, failure)
@ -185,9 +190,9 @@ internal class VectorSettingsPushRuleNotificationViewModelTest {
// When
val viewModelTest = viewModel.test()
// One rule failed to update
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(givenARuleId(firstRuleId), false))
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(firstRuleId, false))
// All the rules failed to update
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(givenARuleId(secondRuleId), false))
viewModel.handle(VectorSettingsPushRuleNotificationViewAction.UpdatePushRule(secondRuleId, false))
// Then
coVerifyOrder {
@ -249,13 +254,11 @@ internal class VectorSettingsPushRuleNotificationViewModelTest {
secondResult shouldBe false
}
private fun givenARuleId(ruleId: String, notificationIndex: NotificationIndex = NotificationIndex.NOISY): PushRuleAndKind {
private fun givenARuleId(ruleId: String, notificationIndex: NotificationIndex = NotificationIndex.NOISY) {
val pushRule = PushRuleFixture.aPushRule(ruleId)
every { pushRule.notificationIndex } returns notificationIndex
val ruleAndKind = PushRuleFixture.aPushRuleAndKind(pushRule)
every { fakePushRuleService.getPushRules().findDefaultRule(ruleId) } returns ruleAndKind
return ruleAndKind
}
}