From 3bba9dea25fb8adada71205bb96098a4b52bfaa4 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 19 Oct 2022 16:30:40 +0200 Subject: [PATCH] Adding unit test for filter action --- .../OtherSessionsViewModelTest.kt | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModelTest.kt index c1018003a5..41c54f9255 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModelTest.kt @@ -80,7 +80,7 @@ class OtherSessionsViewModelTest { fun `given the viewModel has been initialized then viewState is updated with devices list`() { // Given val devices = mockk>() - givenGetDeviceFullInfoListReturns(devices) + givenGetDeviceFullInfoListReturns(filterType = defaultArgs.defaultFilter, devices) val expectedState = OtherSessionsViewState( devices = Success(devices), currentFilter = defaultArgs.defaultFilter, @@ -98,8 +98,41 @@ class OtherSessionsViewModelTest { verifyAll { fakeGetDeviceFullInfoListUseCase.execute(defaultArgs.defaultFilter, defaultArgs.excludeCurrentDevice) } } - private fun givenGetDeviceFullInfoListReturns(devices: List) { - every { fakeGetDeviceFullInfoListUseCase.execute(any(), any()) } returns flowOf(devices) + @Test + fun `given filter devices action when handling the action then viewState is updated with filter option and devices are filtered`() { + // Given + val filterType = DeviceManagerFilterType.UNVERIFIED + val devices = mockk>() + val filteredDevices = mockk>() + givenGetDeviceFullInfoListReturns(filterType = defaultArgs.defaultFilter, devices) + givenGetDeviceFullInfoListReturns(filterType = filterType, filteredDevices) + val expectedState = OtherSessionsViewState( + devices = Success(filteredDevices), + currentFilter = filterType, + excludeCurrentDevice = defaultArgs.excludeCurrentDevice, + isSelectModeEnabled = false, + ) + + // When + val viewModel = createViewModel() + val viewModelTest = viewModel.test() + viewModel.handle(OtherSessionsAction.FilterDevices(filterType)) + + // Then + viewModelTest + .assertLatestState { state -> state == expectedState } + .finish() + verifyAll { + fakeGetDeviceFullInfoListUseCase.execute(defaultArgs.defaultFilter, defaultArgs.excludeCurrentDevice) + fakeGetDeviceFullInfoListUseCase.execute(filterType, defaultArgs.excludeCurrentDevice) + } + } + + private fun givenGetDeviceFullInfoListReturns( + filterType: DeviceManagerFilterType, + devices: List, + ) { + every { fakeGetDeviceFullInfoListUseCase.execute(filterType, any()) } returns flowOf(devices) } private fun givenVerificationService(): FakeVerificationService {