From 4879d20322ae002456bc51c45e3c3f4399e89a2d Mon Sep 17 00:00:00 2001 From: VaiTon Date: Sat, 18 Mar 2023 15:10:15 +0100 Subject: [PATCH] fix: ProductCompareViewModelTest Closes #4923 --- .../github/scrachx/openfood/models/Product.kt | 2 +- .../compare/ProductCompareViewModelTest.kt | 124 ++++++++++++------ 2 files changed, 84 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/openfoodfacts/github/scrachx/openfood/models/Product.kt b/app/src/main/java/openfoodfacts/github/scrachx/openfood/models/Product.kt index 6d09690886a4..58a119b08173 100644 --- a/app/src/main/java/openfoodfacts/github/scrachx/openfood/models/Product.kt +++ b/app/src/main/java/openfoodfacts/github/scrachx/openfood/models/Product.kt @@ -22,7 +22,7 @@ class Product : SearchProduct() { * @return The additivesTags */ @JsonProperty(ApiFields.Keys.ADDITIVES_TAGS) - val additivesTags = arrayListOf() + val additivesTags: MutableList = mutableListOf() /** * @return The allergens diff --git a/app/src/test/java/openfoodfacts/github/scrachx/openfood/features/compare/ProductCompareViewModelTest.kt b/app/src/test/java/openfoodfacts/github/scrachx/openfood/features/compare/ProductCompareViewModelTest.kt index 34fd755d36ed..a65ecb4cd6e2 100644 --- a/app/src/test/java/openfoodfacts/github/scrachx/openfood/features/compare/ProductCompareViewModelTest.kt +++ b/app/src/test/java/openfoodfacts/github/scrachx/openfood/features/compare/ProductCompareViewModelTest.kt @@ -1,6 +1,12 @@ package openfoodfacts.github.scrachx.openfood.features.compare import com.google.common.truth.Truth.assertThat +import io.mockk.coEvery +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.junit5.MockKExtension +import io.mockk.mockk +import io.mockk.verify import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch @@ -9,6 +15,7 @@ import openfoodfacts.github.scrachx.openfood.analytics.AnalyticsEvent import openfoodfacts.github.scrachx.openfood.analytics.MatomoAnalytics import openfoodfacts.github.scrachx.openfood.features.compare.ProductCompareViewModel.CompareProduct import openfoodfacts.github.scrachx.openfood.features.compare.ProductCompareViewModel.SideEffect +import openfoodfacts.github.scrachx.openfood.models.Barcode import openfoodfacts.github.scrachx.openfood.models.Product import openfoodfacts.github.scrachx.openfood.models.entities.additive.AdditiveName import openfoodfacts.github.scrachx.openfood.repositories.ProductRepository @@ -19,24 +26,39 @@ import openfoodfacts.github.scrachx.openfood.utils.LocaleManager import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith -import org.mockito.kotlin.* -@ExtendWith(InstantTaskExecutorExtension::class) +@ExtendWith( + InstantTaskExecutorExtension::class, + MockKExtension::class +) @ExperimentalCoroutinesApi class ProductCompareViewModelTest { - private val taxonomiesRepository: TaxonomiesRepository = mock() - private val localeManager: LocaleManager = mock() - private val matomoAnalytics: MatomoAnalytics = mock() - private val coroutineDispatcher = CoroutineDispatchersTest() - private val productRepository: ProductRepository = mock() + @MockK + lateinit var taxonomiesRepository: TaxonomiesRepository + + @MockK + lateinit var localeManager: LocaleManager + + @MockK + lateinit var matomoAnalytics: MatomoAnalytics + @MockK + lateinit var productRepository: ProductRepository + + private val coroutineDispatcher = CoroutineDispatchersTest() private lateinit var viewModel: ProductCompareViewModel @BeforeEach fun setup() { - whenever(localeManager.getLanguage()).doReturn("en") - viewModel = ProductCompareViewModel(taxonomiesRepository, localeManager, matomoAnalytics, coroutineDispatcher, productRepository) + every { localeManager.getLanguage() } returns "en" + viewModel = ProductCompareViewModel( + taxonomiesRepository, + localeManager, + matomoAnalytics, + coroutineDispatcher, + productRepository + ) } @Test @@ -47,11 +69,15 @@ class ProductCompareViewModelTest { @Test fun addProductToCompare_shouldEmitAlreadyExistEvent_whenTheSameProductAdded() = runBlockingTest { // GIVEN - val product: Product = mock { - on { code } doReturn "qwerty" + val product = mockk { + every { barcode } returns Barcode("qwerty") + every { additivesTags } returns mutableListOf() } - whenever(taxonomiesRepository.getAdditive("qwerty", "en")) - .doReturn(AdditiveName("test-name")) + + every { matomoAnalytics.trackEvent(ofType()) } returns Unit + + coEvery { taxonomiesRepository.getAdditive("qwerty", "en") } + .returns(AdditiveName("test-name")) val flowItems = mutableListOf() val job = launch { @@ -65,27 +91,36 @@ class ProductCompareViewModelTest { // THEN assertThat(flowItems.size).isEqualTo(1) assertThat(flowItems[0] is SideEffect.ProductAlreadyAdded).isTrue() + verify(exactly = 1) { matomoAnalytics.trackEvent(ofType()) } job.cancel() } @Test fun addProductToCompare_shouldAddProducts_whenProductsAreDifferent() = runBlockingTest { // GIVEN - val product1 = mock { - on { code } doReturn "qwerty1" + val barcode1 = Barcode("qwerty1") + val product1 = mockk { + every { barcode } returns barcode1 + every { additivesTags } returns mutableListOf() } - val product2 = mock { - on { code } doReturn "qwerty2" + val barcode2 = Barcode("qwerty2") + val product2 = mockk { + every { barcode } returns barcode2 + every { additivesTags } returns mutableListOf() } - val product3 = mock { - on { code } doReturn "qwerty3" + val barcode3 = Barcode("qwerty3") + val product3 = mockk { + every { barcode } returns barcode3 + every { additivesTags } returns mutableListOf() } - whenever(taxonomiesRepository.getAdditive("qwerty1", "en")) - .doReturn(AdditiveName("test-name1")) - whenever(taxonomiesRepository.getAdditive("qwerty2", "en")) - .doReturn(AdditiveName("test-name2")) - whenever(taxonomiesRepository.getAdditive("qwerty3", "en")) - .doReturn(AdditiveName("test-name3")) + coEvery { taxonomiesRepository.getAdditive("qwerty1", "en") } + .returns(AdditiveName("test-name1")) + coEvery { taxonomiesRepository.getAdditive("qwerty2", "en") } + .returns(AdditiveName("test-name2")) + coEvery { taxonomiesRepository.getAdditive("qwerty3", "en") } + .returns(AdditiveName("test-name3")) + + every { matomoAnalytics.trackEvent(ofType()) } returns Unit val flowItems = mutableListOf>() val job = launch { @@ -99,35 +134,42 @@ class ProductCompareViewModelTest { // THEN assertThat(flowItems.last().size).isEqualTo(3) - assertThat(flowItems.last()[0].product.code).isEqualTo("qwerty1") - assertThat(flowItems.last()[1].product.code).isEqualTo("qwerty2") - assertThat(flowItems.last()[2].product.code).isEqualTo("qwerty3") + assertThat(flowItems.last()[0].product.barcode).isEqualTo(barcode1) + assertThat(flowItems.last()[1].product.barcode).isEqualTo(barcode2) + assertThat(flowItems.last()[2].product.barcode).isEqualTo(barcode3) job.cancel() } @Test fun addProductToCompare_shouldTrackRightAnalytics_whenProductsAddedToCompare() = runBlockingTest { // GIVEN - val product1: Product = mock { - on { code } doReturn "qwerty1" + val barcode1 = Barcode("qwerty1") + val product1: Product = mockk { + every { barcode } returns barcode1 + every { additivesTags } returns mutableListOf() } - val product2: Product = mock { - on { code } doReturn "qwerty2" + coEvery { taxonomiesRepository.getAdditive("qwerty1", "en") } + .returns(AdditiveName("test-name1")) + + val barcode2 = Barcode("qwerty2") + val product2: Product = mockk { + every { barcode } returns barcode2 + every { additivesTags } returns mutableListOf() } - whenever(taxonomiesRepository.getAdditive("qwerty1", "en")) - .doReturn(AdditiveName("test-name1")) - whenever(taxonomiesRepository.getAdditive("qwerty2", "en")) - .doReturn(AdditiveName("test-name2")) + coEvery { taxonomiesRepository.getAdditive("qwerty2", "en") } + .returns(AdditiveName("test-name2")) + + val argumentCapture = mutableListOf() + every { matomoAnalytics.trackEvent(capture(argumentCapture)) } returns Unit // WHEN viewModel.addProductToCompare(product1) viewModel.addProductToCompare(product2) // THEN - val argumentCapture = argumentCaptor() - verify(matomoAnalytics, times(3)).trackEvent(argumentCapture.capture()) - assertThat((argumentCapture.allValues[0] as AnalyticsEvent.AddProductToComparison).barcode).isEqualTo("qwerty1") - assertThat((argumentCapture.allValues[1] as AnalyticsEvent.AddProductToComparison).barcode).isEqualTo("qwerty2") - assertThat((argumentCapture.allValues[2] as AnalyticsEvent.CompareProducts).count).isEqualTo(2f) + verify(exactly = 3) { matomoAnalytics.trackEvent(any()) } + assertThat((argumentCapture[0] as AnalyticsEvent.AddProductToComparison).barcode).isEqualTo(barcode1) + assertThat((argumentCapture[1] as AnalyticsEvent.AddProductToComparison).barcode).isEqualTo(barcode2) + assertThat((argumentCapture[2] as AnalyticsEvent.CompareProducts).count).isEqualTo(2f) } }