Fix unit tests

This commit is contained in:
Florian Renaud 2023-02-20 14:02:28 +01:00
parent 13866c62bf
commit cd648a0b50
1 changed files with 18 additions and 15 deletions

View File

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