-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #450 from nimblehq/feature/299-Add-ui-test-home-se…
…cond-screen [#299] [Sample] Add UI tests for XML sample code
- Loading branch information
Showing
9 changed files
with
224 additions
and
50 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
sample-xml/app/src/main/java/co/nimblehq/sample/xml/extension/NavArgsExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package co.nimblehq.sample.xml.extension | ||
|
||
import androidx.annotation.MainThread | ||
import androidx.fragment.app.Fragment | ||
import androidx.navigation.NavArgs | ||
import androidx.navigation.fragment.navArgs | ||
|
||
@MainThread | ||
inline fun <reified Args : NavArgs> Fragment.provideNavArgs(): Lazy<Args> = | ||
OverridableLazy(navArgs()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
sample-xml/app/src/test/java/co/nimblehq/sample/xml/test/NavArgsExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package co.nimblehq.sample.xml.test | ||
|
||
import androidx.navigation.NavArgs | ||
import co.nimblehq.sample.xml.extension.OverridableLazy | ||
import kotlin.reflect.KProperty1 | ||
import kotlin.reflect.jvm.isAccessible | ||
|
||
fun <Arg : NavArgs, T> T.replace( | ||
argumentDelegate: KProperty1<T, Arg>, | ||
argument: Arg | ||
) { | ||
argumentDelegate.isAccessible = true | ||
(argumentDelegate.getDelegate(this) as OverridableLazy<Arg>).implementation = lazy { argument } | ||
} |
108 changes: 108 additions & 0 deletions
108
sample-xml/app/src/test/java/co/nimblehq/sample/xml/ui/screens/home/HomeFragmentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package co.nimblehq.sample.xml.ui.screens.home | ||
|
||
import androidx.core.view.isVisible | ||
import co.nimblehq.sample.xml.databinding.FragmentHomeBinding | ||
import co.nimblehq.sample.xml.model.UiModel | ||
import co.nimblehq.sample.xml.test.TestNavigatorModule.mockMainNavigator | ||
import co.nimblehq.sample.xml.test.getPrivateProperty | ||
import co.nimblehq.sample.xml.test.replace | ||
import co.nimblehq.sample.xml.ui.BaseFragmentTest | ||
import co.nimblehq.sample.xml.ui.base.NavigationEvent | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import io.kotest.matchers.booleans.shouldBeFalse | ||
import io.kotest.matchers.booleans.shouldBeTrue | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.* | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import org.junit.* | ||
import org.robolectric.shadows.ShadowToast | ||
import java.util.* | ||
|
||
@HiltAndroidTest | ||
class HomeFragmentTest : BaseFragmentTest<HomeFragment, FragmentHomeBinding>() { | ||
|
||
private val mockViewModel = mockk<HomeViewModel>(relaxed = true) | ||
|
||
@get:Rule | ||
var hiltRule = HiltAndroidRule(this) | ||
|
||
@Before | ||
fun setUp() { | ||
hiltRule.inject() | ||
} | ||
|
||
@Test | ||
fun `When launching fragment, it displays the recycler view`() { | ||
launchFragment() | ||
fragment.binding.rvHome.isVisible.shouldBeTrue() | ||
} | ||
|
||
@Test | ||
fun `When launching fragment and view model emits loading, it displays the progress bar`() { | ||
every { mockViewModel.isLoading } returns MutableStateFlow(true) | ||
|
||
launchFragment() | ||
fragment.binding.pbHome.isVisible.shouldBeTrue() | ||
} | ||
|
||
@Test | ||
fun `When launching fragment and view model does not emit loading, it does not display the progress bar`() { | ||
every { mockViewModel.isLoading } returns MutableStateFlow(false) | ||
|
||
launchFragment() | ||
fragment.binding.pbHome.isVisible.shouldBeFalse() | ||
} | ||
|
||
@Test | ||
fun `When launching fragment and view model emits list of item, it displays the recycler view with items`() { | ||
val items = arrayListOf( | ||
UiModel(UUID.randomUUID().toString()), | ||
UiModel(UUID.randomUUID().toString()), | ||
UiModel(UUID.randomUUID().toString()) | ||
) | ||
every { mockViewModel.uiModels } returns MutableStateFlow(items) | ||
|
||
launchFragment() | ||
fragment.binding.rvHome.adapter?.itemCount shouldBe items.size | ||
} | ||
|
||
@Test | ||
fun `When launching fragment and view model emits first time launch, it displays a toast message`() { | ||
every { mockViewModel.isFirstTimeLaunch } returns MutableStateFlow(true) | ||
|
||
launchFragment() | ||
ShadowToast.getTextOfLatestToast() shouldBe "This is the first time launch" | ||
} | ||
|
||
@Test | ||
fun `When launching fragment and view model does not emit first time launch, it does not display a toast message`() { | ||
every { mockViewModel.isFirstTimeLaunch } returns MutableStateFlow(false) | ||
|
||
launchFragment() | ||
ShadowToast.getTextOfLatestToast() shouldBe null | ||
} | ||
|
||
@Test | ||
fun `When view model emits navigation event to second fragment, it should navigate to second screen`() { | ||
val uiModel = UiModel(UUID.randomUUID().toString()) | ||
every { mockViewModel.navigator } returns MutableStateFlow(NavigationEvent.Second(uiModel)) | ||
every { mockMainNavigator.navigate(any()) } returns Unit | ||
|
||
launchFragment() | ||
verify { mockMainNavigator.navigate(NavigationEvent.Second(uiModel)) } | ||
} | ||
|
||
private fun launchFragment() { | ||
launchFragmentInHiltContainer<HomeFragment>( | ||
onInstantiate = { | ||
replace(getPrivateProperty("viewModel"), mockViewModel) | ||
navigator = mockMainNavigator | ||
} | ||
) { | ||
fragment = this | ||
fragment.navigator.shouldNotBeNull() | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
sample-xml/app/src/test/java/co/nimblehq/sample/xml/ui/screens/second/SecondFragmentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package co.nimblehq.sample.xml.ui.screens.second | ||
|
||
import androidx.core.view.isVisible | ||
import co.nimblehq.sample.xml.R | ||
import co.nimblehq.sample.xml.databinding.FragmentSecondBinding | ||
import co.nimblehq.sample.xml.model.UiModel | ||
import co.nimblehq.sample.xml.test.getPrivateProperty | ||
import co.nimblehq.sample.xml.test.replace | ||
import co.nimblehq.sample.xml.ui.BaseFragmentTest | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import io.kotest.matchers.booleans.shouldBeTrue | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import org.junit.* | ||
import java.util.* | ||
|
||
@HiltAndroidTest | ||
class SecondFragmentTest : BaseFragmentTest<SecondFragment, FragmentSecondBinding>() { | ||
|
||
private val mockViewModel = mockk<SecondViewModel>(relaxed = true) | ||
private val mockArgs = mockk<SecondFragmentArgs>(relaxed = true) | ||
|
||
private val uiModel = UiModel(UUID.randomUUID().toString()) | ||
|
||
@get:Rule | ||
var hiltRule = HiltAndroidRule(this) | ||
|
||
@Before | ||
fun setUp() { | ||
hiltRule.inject() | ||
|
||
every { mockArgs.uiModel } returns uiModel | ||
} | ||
|
||
@Test | ||
fun `When launching fragment, it displays the text view`() { | ||
launchFragment() | ||
fragment.binding.tvSecondId.isVisible.shouldBeTrue() | ||
} | ||
|
||
@Test | ||
fun `When launching fragment and view model with id, it displays the text view with id content`() { | ||
every { mockViewModel.id } returns MutableStateFlow(uiModel.id) | ||
|
||
launchFragment() | ||
fragment.binding.tvSecondId.text shouldBe fragment.getString( | ||
R.string.second_id_title, | ||
uiModel.id | ||
) | ||
} | ||
|
||
private fun launchFragment() { | ||
launchFragmentInHiltContainer<SecondFragment>( | ||
onInstantiate = { | ||
replace(getPrivateProperty("viewModel"), mockViewModel) | ||
replace(getPrivateProperty("args"), mockArgs) | ||
} | ||
) { | ||
fragment = this | ||
} | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
sample-xml/app/src/test/java/co/nimblehq/sample/xml/ui/screens/xml/HomeFragmentTest.kt
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
template-xml/app/src/main/java/co/nimblehq/template/xml/extension/NavArgsExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package co.nimblehq.template.xml.extension | ||
|
||
import androidx.annotation.MainThread | ||
import androidx.fragment.app.Fragment | ||
import androidx.navigation.NavArgs | ||
import androidx.navigation.fragment.navArgs | ||
|
||
@MainThread | ||
inline fun <reified Args : NavArgs> Fragment.provideNavArgs(): Lazy<Args> = | ||
OverridableLazy(navArgs()) |
14 changes: 14 additions & 0 deletions
14
template-xml/app/src/test/java/co/nimblehq/template/xml/test/NavArgsExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package co.nimblehq.template.xml.test | ||
|
||
import androidx.navigation.NavArgs | ||
import co.nimblehq.template.xml.extension.OverridableLazy | ||
import kotlin.reflect.KProperty1 | ||
import kotlin.reflect.jvm.isAccessible | ||
|
||
fun <Arg : NavArgs, T> T.replace( | ||
argumentDelegate: KProperty1<T, Arg>, | ||
argument: Arg | ||
) { | ||
argumentDelegate.isAccessible = true | ||
(argumentDelegate.getDelegate(this) as OverridableLazy<Arg>).implementation = lazy { argument } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters