Skip to content

Commit

Permalink
Added unit tests for SyncStatusFragment (#2572)
Browse files Browse the repository at this point in the history
  • Loading branch information
anandwana001 authored Aug 7, 2024
1 parent 50790ea commit b6c558e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTag
import com.google.android.ground.databinding.SyncStatusFragBinding
import com.google.android.ground.ui.common.AbstractFragment
import com.google.android.ground.ui.theme.AppTheme
Expand Down Expand Up @@ -59,9 +62,12 @@ class SyncStatusFragment : AbstractFragment() {
@Composable
private fun ShowSyncItems() {
val list by viewModel.mutations.observeAsState()
if (list == null) return
LazyColumn(Modifier.fillMaxSize()) {
items(list!!) { SyncListItem(modifier = Modifier, detail = it) }
list?.let {
LazyColumn(Modifier.fillMaxSize().testTag("sync list")) {
items(it) {
SyncListItem(modifier = Modifier.semantics { testTag = "item ${it.user}" }, detail = it)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.ground.ui.syncstatus

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.onNodeWithTag
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import com.google.android.ground.BaseHiltTest
import com.google.android.ground.R
import com.google.android.ground.launchFragmentInHiltContainer
import com.google.android.ground.persistence.local.stores.LocalSurveyStore
import com.google.android.ground.repository.SurveyRepository
import com.sharedtest.FakeData.SURVEY
import com.sharedtest.persistence.remote.FakeRemoteDataStore
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlinx.coroutines.test.advanceUntilIdle
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@HiltAndroidTest
@RunWith(RobolectricTestRunner::class)
class SyncStatusFragmentTest : BaseHiltTest() {

@Inject lateinit var fakeRemoteDataStore: FakeRemoteDataStore
@Inject lateinit var localSurveyStore: LocalSurveyStore
@Inject lateinit var surveyRepository: SurveyRepository

override fun setUp() {
super.setUp()
launchFragmentInHiltContainer<SyncStatusFragment>()
}

@Test
fun `Toolbar should be displayed`() {
onView(withId(R.id.sync_status_toolbar)).check(matches(isDisplayed()))
}

@Test
fun `Sync items should be displayed`() = runWithTestDispatcher {
fakeRemoteDataStore.surveys = listOf(SURVEY)
localSurveyStore.insertOrUpdateSurvey(SURVEY)
surveyRepository.selectedSurveyId = SURVEY.id
advanceUntilIdle()

composeTestRule.onNodeWithTag("sync list").assertIsDisplayed()
}

@Test
fun `Sync items should not be displayed`() {
composeTestRule.onNodeWithTag("sync list").assertIsNotDisplayed()
}
}

0 comments on commit b6c558e

Please sign in to comment.