Skip to content

Commit

Permalink
test(planet-selection): add unit tests forPlanetSelectionScreen compo…
Browse files Browse the repository at this point in the history
…sable
  • Loading branch information
Kenzoud committed Dec 19, 2024
1 parent 8487e36 commit 264724c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.github.lookupgroup27.lookup.ui.planetselection

import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createComposeRule
import com.github.lookupgroup27.lookup.model.map.planets.PlanetsRepository
import com.github.lookupgroup27.lookup.ui.navigation.NavigationActions
import com.github.lookupgroup27.lookup.ui.navigation.Screen
import io.mockk.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class PlanetSelectionScreenTest {

@get:Rule val composeTestRule = createComposeRule()

private lateinit var viewModel: PlanetSelectionViewModel
private lateinit var navigationActions: NavigationActions
private lateinit var planetsRepository: PlanetsRepository

@Before
fun setUp() {

planetsRepository = PlanetsRepository(mockk(), mockk(), "")

// Mock the ViewModel with PlanetsRepository
viewModel = spyk(PlanetSelectionViewModel(planetsRepository))

// Mock NavigationActions
navigationActions = mockk(relaxed = true)
}

@Test
fun testBackButtonNavigatesToMenu() {
composeTestRule.setContent {
PlanetSelectionScreen(viewModel = viewModel, navigationActions = navigationActions)
}

composeTestRule.onNodeWithTag("go_back_button").performClick()

verify { navigationActions.navigateTo(Screen.MENU) }
}

@Test
fun testPlanetSelectionUpdatesPlanetName() {
composeTestRule.setContent {
PlanetSelectionScreen(viewModel = viewModel, navigationActions = navigationActions)
}

// Select Mars
composeTestRule.onNodeWithContentDescription("Mars button").performClick()

// Check if the planet name is updated
composeTestRule.onNodeWithTag("planet_name").assertTextEquals("Mars")
}

@Test
fun testInitialPlanetDisplayedCorrectly() {
composeTestRule.setContent {
PlanetSelectionScreen(viewModel = viewModel, navigationActions = navigationActions)
}

// Check if the initially selected planet (Moon) is displayed
composeTestRule.onNodeWithTag("planet_name").assertTextEquals("Moon")
}

@Test
fun testPlanetSurfaceViewUpdatesOnPlanetChange() {
composeTestRule.setContent {
PlanetSelectionScreen(viewModel = viewModel, navigationActions = navigationActions)
}

// Select Jupiter
composeTestRule.onNodeWithContentDescription("Jupiter button").performClick()

// Verify if the planet name is updated to Jupiter
composeTestRule.onNodeWithTag("planet_name").assertTextEquals("Jupiter")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ fun PlanetSelectionScreen(
color = White,
fontSize = 50.sp,
fontWeight = FontWeight.Light,
modifier = Modifier.padding(20.dp).align(Alignment.CenterHorizontally))
modifier =
Modifier.padding(20.dp)
.align(Alignment.CenterHorizontally)
.testTag("planet_name"))
}
},
content = {
Expand Down

0 comments on commit 264724c

Please sign in to comment.