Skip to content

Commit 6a5d253

Browse files
committed
Merge remote-tracking branch 'origin/development' into feat/update-sample-app
2 parents 6bc3df2 + 385ec18 commit 6a5d253

File tree

4 files changed

+86
-51
lines changed

4 files changed

+86
-51
lines changed

core-sdk-samples/higgs-shop-sample-app/app/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ dependencies {
121121
androidTestImplementation(libs.androidx.junit)
122122
androidTestImplementation(libs.androidx.junit.ktx)
123123
androidTestImplementation(libs.androidx.espresso.core)
124+
125+
androidTestImplementation(libs.androidx.ui.test.junit4)
126+
androidTestImplementation(libs.androidx.core)
127+
androidTestImplementation(libs.core.ktx)
128+
androidTestImplementation(libs.androidx.espresso.intents)
129+
androidTestImplementation(libs.androidx.rules)
130+
androidTestImplementation(libs.androidx.truth)
131+
androidTestImplementation(libs.androidx.runner)
132+
androidTestImplementation(libs.truth)
133+
androidTestImplementation(libs.kotlinx.coroutines.test)
134+
androidTestImplementation(libs.androidx.core.testing)
135+
androidTestImplementation(libs.kotlinx.coroutines.test)
136+
androidTestImplementation(libs.mockito.kotlin)
124137
}
125138

126139
fun buildVersionCode(): Int {

core-sdk-samples/higgs-shop-sample-app/app/src/androidTest/kotlin/com/mparticle/example/higgsshopsampleapp/ExampleInstrumentedTests.kt

Lines changed: 0 additions & 51 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mparticle.example.higgsshopsampleapp
2+
3+
import androidx.lifecycle.LiveData
4+
import androidx.lifecycle.Observer
5+
import kotlin.coroutines.resume
6+
import kotlin.coroutines.suspendCoroutine
7+
8+
/**
9+
* Get value from LiveData using suspend coroutine
10+
*/
11+
suspend fun <T> LiveData<T?>.getValueFromLiveData() = suspendCoroutine<T?> {
12+
var observer: Observer<T?>? = null
13+
observer = Observer<T?> { t ->
14+
observer?.let { this.removeObserver(it) }
15+
it.resume(t)
16+
}
17+
this.observeForever(observer)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)