|
| 1 | +package com.woocommerce.android.ui.mystore |
| 2 | + |
| 3 | +import com.woocommerce.android.tools.SelectedSite |
| 4 | +import com.woocommerce.android.ui.mystore.data.StatsRepository |
| 5 | +import com.woocommerce.android.viewmodel.BaseUnitTest |
| 6 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 7 | +import org.assertj.core.api.Assertions.assertThat |
| 8 | +import org.junit.Before |
| 9 | +import org.junit.Test |
| 10 | +import org.mockito.kotlin.any |
| 11 | +import org.mockito.kotlin.eq |
| 12 | +import org.mockito.kotlin.mock |
| 13 | +import org.mockito.kotlin.whenever |
| 14 | +import org.wordpress.android.fluxc.action.WCStatsAction |
| 15 | +import org.wordpress.android.fluxc.model.SiteModel |
| 16 | +import org.wordpress.android.fluxc.model.WCRevenueStatsModel |
| 17 | +import org.wordpress.android.fluxc.store.WCLeaderboardsStore |
| 18 | +import org.wordpress.android.fluxc.store.WCOrderStore |
| 19 | +import org.wordpress.android.fluxc.store.WCStatsStore |
| 20 | +import org.wordpress.android.fluxc.store.WooCommerceStore |
| 21 | + |
| 22 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 23 | +class StatsRepositoryTests : BaseUnitTest() { |
| 24 | + |
| 25 | + private val selectedSite: SelectedSite = mock() |
| 26 | + private val wcStatsStore: WCStatsStore = mock() |
| 27 | + private val wcOrderStore: WCOrderStore = mock() |
| 28 | + private val wcLeaderboardsStore: WCLeaderboardsStore = mock() |
| 29 | + private val wooCommerceStore: WooCommerceStore = mock() |
| 30 | + |
| 31 | + private lateinit var sut: StatsRepository |
| 32 | + |
| 33 | + private val defaultSiteModel = SiteModel() |
| 34 | + |
| 35 | + @Before |
| 36 | + fun setup() { |
| 37 | + sut = StatsRepository( |
| 38 | + selectedSite = selectedSite, |
| 39 | + wcStatsStore = wcStatsStore, |
| 40 | + wcOrderStore = wcOrderStore, |
| 41 | + wcLeaderboardsStore = wcLeaderboardsStore, |
| 42 | + wooCommerceStore = wooCommerceStore |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `when visitors and revenue requests succeed then a success response is returned containing both value`() = testBlocking { |
| 48 | + val granularity = WCStatsStore.StatsGranularity.DAYS |
| 49 | + val startDate = "2024-01-25 00:00:00" |
| 50 | + val endDate = "2024-01-25 23:59:59" |
| 51 | + val visitorStatsResponse = WCStatsStore.OnWCStatsChanged( |
| 52 | + rowsAffected = 2, |
| 53 | + granularity = granularity, |
| 54 | + quantity = "5", |
| 55 | + date = startDate |
| 56 | + ) |
| 57 | + |
| 58 | + val revenueStatsResponse = WCStatsStore.OnWCRevenueStatsChanged( |
| 59 | + rowsAffected = 2, |
| 60 | + granularity = granularity, |
| 61 | + startDate = startDate, |
| 62 | + endDate = endDate |
| 63 | + ) |
| 64 | + |
| 65 | + whenever(selectedSite.get()).thenReturn(defaultSiteModel) |
| 66 | + whenever(wooCommerceStore.getSiteSettings(any())).thenReturn(null) |
| 67 | + whenever(wcStatsStore.fetchNewVisitorStats(any())).thenReturn(visitorStatsResponse) |
| 68 | + whenever(wcStatsStore.fetchRevenueStats(any())).thenReturn(revenueStatsResponse) |
| 69 | + whenever(wcStatsStore.getRawRevenueStats(eq(defaultSiteModel), eq(granularity), eq(startDate), eq(endDate))) |
| 70 | + .thenReturn(WCRevenueStatsModel()) |
| 71 | + |
| 72 | + val result = sut.fetchStats( |
| 73 | + granularity = WCStatsStore.StatsGranularity.DAYS, |
| 74 | + forced = true, |
| 75 | + includeVisitorStats = true |
| 76 | + ) |
| 77 | + |
| 78 | + assertThat(result.isError).isEqualTo(false) |
| 79 | + assertThat(result.model).isNotNull |
| 80 | + assertThat(result.model!!.revenue).isNotNull |
| 81 | + assertThat(result.model!!.visitors).isNotNull |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `when visitors requests fails then a success response is returned with visitors null`() = testBlocking { |
| 86 | + val granularity = WCStatsStore.StatsGranularity.DAYS |
| 87 | + val startDate = "2024-01-25 00:00:00" |
| 88 | + val endDate = "2024-01-25 23:59:59" |
| 89 | + val visitorStatsResponse = WCStatsStore.OnWCStatsChanged(0, granularity).also { |
| 90 | + it.error = WCStatsStore.OrderStatsError() |
| 91 | + it.causeOfChange = WCStatsAction.FETCH_NEW_VISITOR_STATS |
| 92 | + } |
| 93 | + |
| 94 | + val revenueStatsResponse = WCStatsStore.OnWCRevenueStatsChanged( |
| 95 | + rowsAffected = 2, |
| 96 | + granularity = granularity, |
| 97 | + startDate = startDate, |
| 98 | + endDate = endDate |
| 99 | + ) |
| 100 | + |
| 101 | + whenever(selectedSite.get()).thenReturn(defaultSiteModel) |
| 102 | + whenever(wooCommerceStore.getSiteSettings(any())).thenReturn(null) |
| 103 | + whenever(wcStatsStore.fetchNewVisitorStats(any())).thenReturn(visitorStatsResponse) |
| 104 | + whenever(wcStatsStore.fetchRevenueStats(any())).thenReturn(revenueStatsResponse) |
| 105 | + whenever(wcStatsStore.getRawRevenueStats(eq(defaultSiteModel), eq(granularity), eq(startDate), eq(endDate))) |
| 106 | + .thenReturn(WCRevenueStatsModel()) |
| 107 | + |
| 108 | + val result = sut.fetchStats( |
| 109 | + granularity = WCStatsStore.StatsGranularity.DAYS, |
| 110 | + forced = true, |
| 111 | + includeVisitorStats = true |
| 112 | + ) |
| 113 | + |
| 114 | + assertThat(result.isError).isEqualTo(false) |
| 115 | + assertThat(result.model).isNotNull |
| 116 | + assertThat(result.model!!.revenue).isNotNull |
| 117 | + assertThat(result.model!!.visitors).isNull() |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun `when revenue requests fails then an error is returned`() = testBlocking { |
| 122 | + val granularity = WCStatsStore.StatsGranularity.DAYS |
| 123 | + val startDate = "2024-01-25 00:00:00" |
| 124 | + val visitorStatsResponse = WCStatsStore.OnWCStatsChanged( |
| 125 | + rowsAffected = 2, |
| 126 | + granularity = granularity, |
| 127 | + quantity = "5", |
| 128 | + date = startDate |
| 129 | + ) |
| 130 | + |
| 131 | + val revenueStatsResponse = WCStatsStore.OnWCRevenueStatsChanged(0, granularity) |
| 132 | + .also { it.error = WCStatsStore.OrderStatsError() } |
| 133 | + |
| 134 | + whenever(selectedSite.get()).thenReturn(defaultSiteModel) |
| 135 | + whenever(wooCommerceStore.getSiteSettings(any())).thenReturn(null) |
| 136 | + whenever(wcStatsStore.fetchNewVisitorStats(any())).thenReturn(visitorStatsResponse) |
| 137 | + whenever(wcStatsStore.fetchRevenueStats(any())).thenReturn(revenueStatsResponse) |
| 138 | + |
| 139 | + val result = sut.fetchStats( |
| 140 | + granularity = WCStatsStore.StatsGranularity.DAYS, |
| 141 | + forced = true, |
| 142 | + includeVisitorStats = true |
| 143 | + ) |
| 144 | + |
| 145 | + assertThat(result.isError).isEqualTo(true) |
| 146 | + assertThat(result.model).isNull() |
| 147 | + } |
| 148 | +} |
0 commit comments