Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing for fully implemented search components, fix bug in debounce #603

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading