Skip to content

Commit

Permalink
refactor: extract reused variables to companion object in `CreatePr…
Browse files Browse the repository at this point in the history
…ofileTest`
  • Loading branch information
charliemangano committed Nov 9, 2024
1 parent 0ea1fca commit 377bc8e
Showing 1 changed file with 30 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ class CreateProfileTest {

@get:Rule val composeTestRule = createComposeRule()

companion object {
private val email = "test@email.com"
private val name = "John Doe"
private val imageUrl = "https://example.com"
private val description = "A short description"
private val dob = "01/01/2000"
private val userState =
mutableStateOf(User(name = name, imageUrl = imageUrl, description = description, dob = dob))
}

@Before
fun setUp() {
navigationActions = mock(NavigationActions::class.java)
Expand Down Expand Up @@ -92,17 +102,15 @@ class CreateProfileTest {

@Test
fun createInvalidProfileNoEmail() {
val userState =
mutableStateOf(User("John Doe", "https://example.com", "A short bio", "01/01/2000"))
`when`(userViewModel.user).thenReturn(userState)

composeTestRule.setContent { CreateProfileScreen(userViewModel, navigationActions) }

composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput("01/01/2000")
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput("John Doe")
composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput(dob)
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput(name)
composeTestRule
.onNodeWithTag(CreateProfileScreen.DESCRIPTION_FIELD)
.performTextInput("A short bio")
.performTextInput(description)
composeTestRule.onNodeWithTag(CreateProfileScreen.SAVE_BUTTON).performClick()

verify(userViewModel, never()).saveUser(any())
Expand All @@ -113,19 +121,15 @@ class CreateProfileTest {

@Test
fun createInvalidProfileNoDob() {
val userState =
mutableStateOf(User("John Doe", "https://example.com", "A short bio", "01/01/2000"))
`when`(userViewModel.user).thenReturn(userState)

composeTestRule.setContent { CreateProfileScreen(userViewModel, navigationActions) }

composeTestRule
.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD)
.performTextInput("john.doe@example.com")
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput("John Doe")
composeTestRule.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD).performTextInput(email)
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput(name)
composeTestRule
.onNodeWithTag(CreateProfileScreen.DESCRIPTION_FIELD)
.performTextInput("A short bio")
.performTextInput(description)
composeTestRule.onNodeWithTag(CreateProfileScreen.SAVE_BUTTON).performClick()

verify(userViewModel, never()).saveUser(any())
Expand All @@ -136,19 +140,15 @@ class CreateProfileTest {

@Test
fun createInvalidProfileNoName() {
val userState =
mutableStateOf(User("John Doe", "https://example.com", "A short bio", "01/01/2000"))
`when`(userViewModel.user).thenReturn(userState)

composeTestRule.setContent { CreateProfileScreen(userViewModel, navigationActions) }

composeTestRule
.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD)
.performTextInput("john.doe@example.com")
composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput("01/01/2000")
composeTestRule.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD).performTextInput(email)
composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput(dob)
composeTestRule
.onNodeWithTag(CreateProfileScreen.DESCRIPTION_FIELD)
.performTextInput("A short bio")
.performTextInput(description)
composeTestRule.onNodeWithTag(CreateProfileScreen.SAVE_BUTTON).performClick()

verify(userViewModel, never()).saveUser(any())
Expand All @@ -159,17 +159,13 @@ class CreateProfileTest {

@Test
fun createInvalidProfileNoDescription() {
val userState =
mutableStateOf(User("John Doe", "https://example.com", "A short bio", "01/01/2000"))
`when`(userViewModel.user).thenReturn(userState)

composeTestRule.setContent { CreateProfileScreen(userViewModel, navigationActions) }

composeTestRule
.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD)
.performTextInput("john.doe@example.com")
composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput("01/01/2000")
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput("John Doe")
composeTestRule.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD).performTextInput(email)
composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput(dob)
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput(name)
composeTestRule.onNodeWithTag(CreateProfileScreen.SAVE_BUTTON).performClick()

verify(userViewModel, never()).saveUser(any())
Expand All @@ -184,14 +180,12 @@ class CreateProfileTest {

composeTestRule.setContent { CreateProfileScreen(userViewModel, navigationActions) }

composeTestRule
.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD)
.performTextInput("john.doe@example.com")
composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput("01/01/2000")
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput("John Doe")
composeTestRule.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD).performTextInput(email)
composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput(dob)
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput(name)
composeTestRule
.onNodeWithTag(CreateProfileScreen.DESCRIPTION_FIELD)
.performTextInput("A short bio")
.performTextInput(description)
composeTestRule.onNodeWithTag(CreateProfileScreen.SAVE_BUTTON).performClick()

verify(userViewModel).saveUser(any())
Expand All @@ -201,20 +195,16 @@ class CreateProfileTest {

@Test
fun createValidProfileVMSuccess() {
val userState =
mutableStateOf(User("John Doe", "https://example.com", "A short bio", "01/01/2000"))
`when`(userViewModel.user).thenReturn(userState)

composeTestRule.setContent { CreateProfileScreen(userViewModel, navigationActions) }

composeTestRule
.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD)
.performTextInput("john.doe@example.com")
composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput("01/01/2000")
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput("John Doe")
composeTestRule.onNodeWithTag(CreateProfileScreen.EMAIL_FIELD).performTextInput(email)
composeTestRule.onNodeWithTag(CreateProfileScreen.DOB_FIELD).performTextInput(dob)
composeTestRule.onNodeWithTag(CreateProfileScreen.NAME_FIELD).performTextInput(name)
composeTestRule
.onNodeWithTag(CreateProfileScreen.DESCRIPTION_FIELD)
.performTextInput("A short bio")
.performTextInput(description)
composeTestRule.onNodeWithTag(CreateProfileScreen.SAVE_BUTTON).performClick()

verify(userViewModel).saveUser(any())
Expand Down

0 comments on commit 377bc8e

Please sign in to comment.