Skip to content

Commit

Permalink
Merge branch 'main' into mth-android-slide-up
Browse files Browse the repository at this point in the history
  • Loading branch information
boringcactus authored Dec 23, 2024
2 parents b5f6c38 + 68d199c commit 37a4cb0
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class NearbyTransitPageTest : KoinTest {
composeTestRule.waitUntilDoesNotExist(hasText("Loading..."))

composeTestRule.onNodeWithText("Nearby Transit").assertIsDisplayed()
composeTestRule.onNodeWithText("Search by stop").assertIsDisplayed()

composeTestRule.onNodeWithText("Green Line Long Name").assertExists()
composeTestRule.onNodeWithText("Green Line Stop").assertExists()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.mbta.tid.mbta_app.android.search

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.test.requestFocus
import com.mbta.tid.mbta_app.model.RouteType
import com.mbta.tid.mbta_app.model.SearchResults
import com.mbta.tid.mbta_app.model.StopResult
import com.mbta.tid.mbta_app.model.StopResultRoute
import com.mbta.tid.mbta_app.model.response.ApiResult
import com.mbta.tid.mbta_app.repositories.ISearchResultRepository
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import org.koin.compose.KoinContext
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import org.koin.test.KoinTest

@ExperimentalTestApi
@ExperimentalMaterial3Api
class SearchBarOverlayTest : KoinTest {
val koinApplication = koinApplication {
modules(
module {
single<ISearchResultRepository> {
object : ISearchResultRepository {
override suspend fun getSearchResults(
query: String
): ApiResult<SearchResults>? {
return ApiResult.Ok(
SearchResults(
routes = emptyList(),
stops =
listOf(
StopResult(
id = "stopId",
rank = 2,
name = "stopName",
zone = "stopZone",
isStation = false,
routes =
listOf(
StopResultRoute(
type = RouteType.BUS,
icon = "routeIcon",
)
)
)
)
)
)
}
}
}
}
)
}

@get:Rule var composeTestRule = createComposeRule()

@Test
fun testSearchBarOverlayBehavesCorrectly() = runTest {
val navigated = mutableStateOf(false)

composeTestRule.setContent {
KoinContext(koinApplication.koin) {
val focusRequester = remember { FocusRequester() }
SearchBarOverlay(
onStopNavigation = { navigated.value = true },
currentNavEntry = null,
inputFieldFocusRequester = focusRequester,
) {
Text("Content")
}
}
}

composeTestRule.onNodeWithText("Content").assertExists()
val searchNode = composeTestRule.onNodeWithText("Search by stop")
searchNode.assertExists()
searchNode.requestFocus()
composeTestRule.awaitIdle()

searchNode.performTextInput("sto")
composeTestRule.waitUntilAtLeastOneExists(hasText("stopName"))
composeTestRule.onNodeWithText("stopName").performClick()
composeTestRule.waitUntil { navigated.value }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.mbta.tid.mbta_app.android.state

import androidx.compose.ui.test.junit4.createComposeRule
import com.mbta.tid.mbta_app.model.RouteType
import com.mbta.tid.mbta_app.model.SearchResults
import com.mbta.tid.mbta_app.model.StopResult
import com.mbta.tid.mbta_app.model.StopResultRoute
import com.mbta.tid.mbta_app.model.response.ApiResult
import com.mbta.tid.mbta_app.repositories.ISearchResultRepository
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test

class GetSearchResultTest {
val searchResults =
SearchResults(
routes = emptyList(),
stops =
listOf(
StopResult(
id = "stopId",
rank = 2,
name = "stopName",
zone = "stopZone",
isStation = false,
routes =
listOf(
StopResultRoute(
type = RouteType.BUS,
icon = "routeIcon",
)
)
)
)
)

@get:Rule val composeTestRule = createComposeRule()

@Test
fun testSearchResults() = runTest {
var actualSearchResultsViewModel: SearchResultsViewModel? = null

composeTestRule.setContent {
actualSearchResultsViewModel =
getSearchResultsVm(
object : ISearchResultRepository {
override suspend fun getSearchResults(
query: String
): ApiResult<SearchResults>? {
return ApiResult.Ok(searchResults)
}
}
)
}

composeTestRule.waitUntil { actualSearchResultsViewModel != null }

actualSearchResultsViewModel?.getSearchResults("query")

composeTestRule.waitUntil { actualSearchResultsViewModel?.searchResults?.value != null }

assert(actualSearchResultsViewModel?.searchResults?.value == searchResults)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class SearchResultsViewModel(
null -> {}
}
}
} else if (lastClickTime == null) {
job =
CoroutineScope(Dispatchers.IO).launch {
when (val data = searchResultRepository.getSearchResults(query)) {
is ApiResult.Ok -> _searchResults.emit(data.data)
is ApiResult.Error -> _searchResults.emit(null)
null -> {}
}
}
}
lastClickTime = currentTime
}
Expand Down

0 comments on commit 37a4cb0

Please sign in to comment.