1+ package com.mparticle.example.higgsshopsampleapp
2+
3+ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
4+ import androidx.test.core.app.ApplicationProvider
5+ import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
6+ import com.mparticle.example.higgsshopsampleapp.repositories.CartRepository
7+ import com.mparticle.example.higgsshopsampleapp.repositories.ProductsRepository
8+ import com.mparticle.example.higgsshopsampleapp.viewmodels.ShopViewModel
9+ import kotlinx.coroutines.Dispatchers
10+ import kotlinx.coroutines.test.StandardTestDispatcher
11+ import kotlinx.coroutines.test.resetMain
12+ import kotlinx.coroutines.test.runTest
13+ import kotlinx.coroutines.test.setMain
14+ import org.junit.*
15+ import org.junit.runner.RunWith
16+
17+ @RunWith(AndroidJUnit4ClassRunner ::class )
18+ class TestShopViewModel {
19+
20+ @get:Rule
21+ val instantExecutorRule = InstantTaskExecutorRule ()
22+ private val dispatcher = StandardTestDispatcher ()
23+
24+ private val productsRepository = ProductsRepository ()
25+ private val cartRepository = CartRepository ()
26+ private val viewModel = ShopViewModel ()
27+
28+ @Before
29+ fun setup () {
30+ Dispatchers .setMain(dispatcher)
31+ }
32+
33+ @After
34+ fun tearDown () {
35+ Dispatchers .resetMain()
36+ }
37+
38+ @Test
39+ fun `testGetProducts` () = runTest {
40+ val products = productsRepository.getProducts(ApplicationProvider .getApplicationContext())
41+ viewModel.getProducts(ApplicationProvider .getApplicationContext())
42+ Assert .assertEquals(products, viewModel.inventoryResponseLiveData.getValueFromLiveData())
43+ }
44+
45+ @Test
46+ fun `testGetTotalCartItems` () = runTest {
47+ val cartItems = cartRepository.getCartItems(ApplicationProvider .getApplicationContext())
48+ viewModel.getTotalCartItems(ApplicationProvider .getApplicationContext())
49+ Assert .assertEquals(
50+ cartItems.sumOf { it.quantity },
51+ viewModel.cartTotalSizeResponseLiveData.getValueFromLiveData()
52+ )
53+ }
54+
55+ }
0 commit comments