Adding unit tests for new action in DevicesViewModel

This commit is contained in:
Maxime NATUREL 2022-09-14 17:39:41 +02:00
parent d826327773
commit 35db958279
2 changed files with 48 additions and 1 deletions

View file

@ -101,7 +101,6 @@ class DevicesViewModel @AssistedInject constructor(
}
}
// TODO add unit tests
private fun handleVerifyCurrentSessionAction() {
viewModelScope.launch {
val currentSessionCanBeVerified = checkIfCurrentSessionCanBeVerifiedUseCase.execute()

View file

@ -145,6 +145,54 @@ class DevicesViewModelTest {
coVerify { refreshDevicesOnCryptoDevicesChangeUseCase.execute() }
}
@Test
fun `given current session can be verified when handling verify current session action then self verification event is posted`() {
// Given
givenVerificationService()
givenCurrentSessionCrossSigningInfo()
givenDeviceFullInfoList()
givenRefreshDevicesOnCryptoDevicesChange()
val verifyCurrentSessionAction = DevicesAction.VerifyCurrentSession
coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns true
// When
val viewModel = createViewModel()
val viewModelTest = viewModel.test()
viewModel.handle(verifyCurrentSessionAction)
// Then
viewModelTest
.assertEvent { it is DevicesViewEvent.SelfVerification }
.finish()
coVerify {
checkIfCurrentSessionCanBeVerifiedUseCase.execute()
}
}
@Test
fun `given current session cannot be verified when handling verify current session action then reset secrets event is posted`() {
// Given
givenVerificationService()
givenCurrentSessionCrossSigningInfo()
givenDeviceFullInfoList()
givenRefreshDevicesOnCryptoDevicesChange()
val verifyCurrentSessionAction = DevicesAction.VerifyCurrentSession
coEvery { checkIfCurrentSessionCanBeVerifiedUseCase.execute() } returns false
// When
val viewModel = createViewModel()
val viewModelTest = viewModel.test()
viewModel.handle(verifyCurrentSessionAction)
// Then
viewModelTest
.assertEvent { it is DevicesViewEvent.PromptResetSecrets }
.finish()
coVerify {
checkIfCurrentSessionCanBeVerifiedUseCase.execute()
}
}
private fun givenVerificationService(): FakeVerificationService {
val fakeVerificationService = fakeActiveSessionHolder
.fakeSession