Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit da71e7e9 authored by Ellen Poe's avatar Ellen Poe
Browse files

test: test nearby filter logic

parent 35e8cca8
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -110,6 +110,10 @@ class NearbyViewModel @Inject constructor(
        }
    }

    fun getSelectedCategoriesSet(): Set<String> {
        return selectedCategories.toSet()
    }

    suspend fun fetchNearby(latitude: Double, longitude: Double) {
        _isLoading.value = true
        _error.value = null
+59 −0
Original line number Diff line number Diff line
@@ -244,4 +244,63 @@ class NearbyViewModelTest {
                )
            }
        }

    @Test
    fun `setCategorySelection should add and remove categories correctly`() = runTest {
        val category1 = "food"
        val category2 = "health"
        
        // Initially no categories should be selected
        assertTrue(viewModel.getSelectedCategoriesSet().isEmpty())
        
        // Add categories
        viewModel.setCategorySelection(category1, true)
        viewModel.setCategorySelection(category2, true)
        
        // Verify categories were added
        assertEquals(setOf(category1, category2), viewModel.getSelectedCategoriesSet())
        
        // Remove one category
        viewModel.setCategorySelection(category1, false)
        
        // Verify remaining category
        assertEquals(setOf(category2), viewModel.getSelectedCategoriesSet())
    }

    @Test
    fun `setCategorySelection should refresh data with selected categories`() = runTest {
        val testLocation = android.location.Location("test").apply {
            latitude = 37.7749
            longitude = -122.4194
        }
        val locationFlow = MutableStateFlow(testLocation)

        // Set up the location repository to return a location
        coEvery { mockLocationRepository.locationFlow } returns locationFlow

        // Mock the geocoding service
        coEvery { mockGeocodingService.nearby(any(), any(), any()) } returns emptyList()

        viewModel.startLocationObservation()
        advanceUntilIdle()

        // Clear previous verification calls
        io.mockk.clearMocks(mockGeocodingService)

        // Add categories
        viewModel.setCategorySelection("food", true)
        viewModel.setCategorySelection("health", true)

        // Allow coroutines to complete
        advanceUntilIdle()

        // Verify that fetchNearby was called with both selected categories
        coVerify {
            mockGeocodingService.nearby(
                testLocation.latitude,
                testLocation.longitude,
                listOf("food", "health")
            )
        }
    }
}