Skip to content

Commit

Permalink
fix: ProductCompareViewModelTest
Browse files Browse the repository at this point in the history
Closes #4923
  • Loading branch information
VaiTon committed Mar 18, 2023
1 parent 74f2f7c commit 4879d20
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Product : SearchProduct() {
* @return The additivesTags
*/
@JsonProperty(ApiFields.Keys.ADDITIVES_TAGS)
val additivesTags = arrayListOf<String>()
val additivesTags: MutableList<String> = mutableListOf()

/**
* @return The allergens
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -47,11 +69,15 @@ class ProductCompareViewModelTest {
@Test
fun addProductToCompare_shouldEmitAlreadyExistEvent_whenTheSameProductAdded() = runBlockingTest {
// GIVEN
val product: Product = mock {
on { code } doReturn "qwerty"
val product = mockk<Product> {
every { barcode } returns Barcode("qwerty")
every { additivesTags } returns mutableListOf()
}
whenever(taxonomiesRepository.getAdditive("qwerty", "en"))
.doReturn(AdditiveName("test-name"))

every { matomoAnalytics.trackEvent(ofType<AnalyticsEvent.AddProductToComparison>()) } returns Unit

coEvery { taxonomiesRepository.getAdditive("qwerty", "en") }
.returns(AdditiveName("test-name"))

val flowItems = mutableListOf<SideEffect>()
val job = launch {
Expand All @@ -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<AnalyticsEvent.AddProductToComparison>()) }
job.cancel()
}

@Test
fun addProductToCompare_shouldAddProducts_whenProductsAreDifferent() = runBlockingTest {
// GIVEN
val product1 = mock<Product> {
on { code } doReturn "qwerty1"
val barcode1 = Barcode("qwerty1")
val product1 = mockk<Product> {
every { barcode } returns barcode1
every { additivesTags } returns mutableListOf()
}
val product2 = mock<Product> {
on { code } doReturn "qwerty2"
val barcode2 = Barcode("qwerty2")
val product2 = mockk<Product> {
every { barcode } returns barcode2
every { additivesTags } returns mutableListOf()
}
val product3 = mock<Product> {
on { code } doReturn "qwerty3"
val barcode3 = Barcode("qwerty3")
val product3 = mockk<Product> {
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<AnalyticsEvent.AddProductToComparison>()) } returns Unit

val flowItems = mutableListOf<List<CompareProduct>>()
val job = launch {
Expand All @@ -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<AnalyticsEvent>()
every { matomoAnalytics.trackEvent(capture(argumentCapture)) } returns Unit

// WHEN
viewModel.addProductToCompare(product1)
viewModel.addProductToCompare(product2)

// THEN
val argumentCapture = argumentCaptor<AnalyticsEvent>()
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)
}
}

0 comments on commit 4879d20

Please sign in to comment.