Skip to content

Commit

Permalink
basic tests for step and composable
Browse files Browse the repository at this point in the history
  • Loading branch information
cmathew committed Apr 26, 2024
1 parent 617a48e commit 588ce22
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 8 deletions.
3 changes: 3 additions & 0 deletions magellan-sample-migration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ dependencies {
testImplementation libs.mockK
testImplementation libs.robolectric
testImplementation libs.truth
// testImplementation libs.espressoCore
testImplementation libs.compose.junit4
testImplementation libs.compose.manifest

kaptAndroidTest libs.daggerCompiler
androidTestImplementation libs.extJunit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.wealthfront.magellan.sample.migration.AppComponentContainer
import com.wealthfront.magellan.sample.migration.CoroutineIdlingRule
import com.wealthfront.magellan.sample.migration.MainActivity
import com.wealthfront.magellan.sample.migration.R
import com.wealthfront.magellan.sample.migration.TestAppComponent
import com.wealthfront.magellan.sample.migration.api.DogApi
import com.wealthfront.magellan.sample.migration.api.DogBreedsResponse
import com.wealthfront.magellan.sample.migration.api.DogImageResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.HorizontalDivider
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag

@Composable
fun DogBreeds(dogBreeds: List<String>, onBreedClick: (name: String) -> Unit) {
LazyColumn {
LazyColumn(modifier = Modifier.testTag("DogBreeds")) {
itemsIndexed(dogBreeds) { index, item ->
DogBreedListItem(item, onBreedClick)
if (index != (dogBreeds.size - 1)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.wealthfront.magellan.sample.migration

import com.wealthfront.magellan.sample.migration.api.DogApi
import com.wealthfront.magellan.sample.migration.tide.DogListStepTest
import com.wealthfront.magellan.sample.migration.tide.DogListStepFactory
import com.wealthfront.magellan.sample.migration.toolbar.ToolbarHelper
import dagger.Component
import javax.inject.Singleton
Expand All @@ -12,6 +12,5 @@ interface TestAppComponent : AppComponent {

val toolbarHelper: ToolbarHelper
val api: DogApi

fun inject(test: DogListStepTest)
val dogListStepFactory: DogListStepFactory
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import javax.inject.Inject

@RunWith(RobolectricTestRunner::class)
class DogListStepTest {

private lateinit var dogListStep: DogListStep
@Inject lateinit var dogListStepFactory: DogListStepFactory
private val activityController = Robolectric.buildActivity(ComponentActivity::class.java)

private var chosenBreed: String? = null
Expand All @@ -31,8 +29,7 @@ class DogListStepTest {
fun setUp() {
val context = ApplicationProvider.getApplicationContext<Application>()
val component = ((context as AppComponentContainer).injector() as TestAppComponent)
component.inject(this)
dogListStep = dogListStepFactory.create { chosenBreed = it }
dogListStep = component.dogListStepFactory.create { chosenBreed = it }
coEvery { component.api.getAllBreeds() } returns
DogBreedsResponse(message = mapOf("akita" to emptyList()), status = "success")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sdk=28
application=com.wealthfront.magellan.sample.migration.TestSampleApplication
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.wealthfront.magellan.sample.migration.tide

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onChildAt
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class DogBreedsTest {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun goesToSelectedDogBreed() {
var clicked = false
composeTestRule.setContent {
DogBreeds(dogBreeds = listOf("akita"), onBreedClick = { clicked = true })
}

composeTestRule.onNodeWithTag("DogBreeds").onChildAt(0).performClick()

assertThat(clicked).isTrue()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.wealthfront.magellan.sample.migration.tide

import android.app.Application
import android.os.Looper.getMainLooper
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onChildAt
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.wealthfront.magellan.lifecycle.setContentScreen
import com.wealthfront.magellan.sample.migration.AppComponentContainer
import com.wealthfront.magellan.sample.migration.TestAppComponent
import com.wealthfront.magellan.sample.migration.api.DogBreedsResponse
import io.mockk.coEvery
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf

@RunWith(RobolectricTestRunner::class)
class DogListStepTest {

@get:Rule
val composeTestRule = createComposeRule()

private lateinit var dogListStep: DogListStep
private val activityController = Robolectric.buildActivity(ComponentActivity::class.java)

private var chosenBreed: String? = null

@Before
fun setUp() {
val context = ApplicationProvider.getApplicationContext<Application>()
val component = ((context as AppComponentContainer).injector() as TestAppComponent)
dogListStep = component.dogListStepFactory.create { chosenBreed = it }
coEvery { component.api.getAllBreeds() } returns
DogBreedsResponse(message = mapOf("akita" to emptyList()), status = "success")
}

@Test
fun goesToSelectedDogBreed() {
activityController.get().setContentScreen(dogListStep)
activityController.setup()
shadowOf(getMainLooper()).idle()

composeTestRule.onNodeWithTag("DogBreeds").onChildAt(0).performClick()
assertThat(chosenBreed).isEqualTo("akita")
}
}

0 comments on commit 588ce22

Please sign in to comment.