Skip to content

Commit

Permalink
Merge branch 'main' into feature/add-friends-to-travel
Browse files Browse the repository at this point in the history
  • Loading branch information
RemIsMyWaifuu authored Dec 11, 2024
2 parents e44f964 + 1b46017 commit a2aaa4d
Show file tree
Hide file tree
Showing 33 changed files with 740 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import androidx.compose.ui.test.performTextInput
import com.github.se.travelpouch.model.activity.Activity
import com.github.se.travelpouch.model.activity.ActivityRepository
import com.github.se.travelpouch.model.activity.ActivityViewModel
import com.github.se.travelpouch.model.events.EventRepository
import com.github.se.travelpouch.model.events.EventViewModel
import com.github.se.travelpouch.model.location.LocationRepository
import com.github.se.travelpouch.model.location.LocationViewModel
import com.github.se.travelpouch.model.travels.Location
import com.github.se.travelpouch.ui.navigation.NavigationActions
import com.google.firebase.Timestamp
import com.google.firebase.firestore.DocumentReference
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -26,12 +29,15 @@ import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.never
import org.mockito.kotlin.whenever

class AddActivityScreenTest {
private lateinit var mockActivityRepositoryFirebase: ActivityRepository
private lateinit var mockActivityModelView: ActivityViewModel
private lateinit var navigationActions: NavigationActions
private lateinit var mockLocationViewModel: LocationViewModel
private lateinit var eventRepository: EventRepository
private lateinit var eventViewModel: EventViewModel

class FakeLocationRepository : LocationRepository {

Expand Down Expand Up @@ -67,13 +73,17 @@ class AddActivityScreenTest {
mockActivityRepositoryFirebase = mock(ActivityRepository::class.java)
mockActivityModelView = ActivityViewModel(mockActivityRepositoryFirebase)
mockLocationViewModel = LocationViewModel(FakeLocationRepository())
eventRepository = mock()
eventViewModel = EventViewModel(eventRepository)

`when`(mockActivityModelView.getNewUid()).thenReturn("uid")
}

@Test
fun everythingIsDisplayed() {
composeTestRule.setContent { AddActivityScreen(navigationActions, mockActivityModelView) }
composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, eventViewModel = eventViewModel)
}

composeTestRule.onNodeWithTag("AddActivityScreen").isDisplayed()
composeTestRule.onNodeWithTag("titleField").isDisplayed()
Expand Down Expand Up @@ -101,7 +111,9 @@ class AddActivityScreenTest {

@Test
fun doesNotSaveWhenFieldsAreEmpty() {
composeTestRule.setContent { AddActivityScreen(navigationActions, mockActivityModelView) }
composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, eventViewModel = eventViewModel)
}

completeAllFields(composeTestRule)
// todo: add a test for location. Not now because location defined later.
Expand All @@ -110,34 +122,43 @@ class AddActivityScreenTest {
composeTestRule.onNodeWithTag("titleField").performTextClearance()
composeTestRule.onNodeWithTag("saveButton").performClick()
verify(mockActivityRepositoryFirebase, never())
.addActivity(anyOrNull(), anyOrNull(), anyOrNull())
.addActivity(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())

// verify no saving when blank description
completeAllFields(composeTestRule)
composeTestRule.onNodeWithTag("descriptionField").performTextClearance()
composeTestRule.onNodeWithTag("saveButton").performClick()
verify(mockActivityRepositoryFirebase, never())
.addActivity(anyOrNull(), anyOrNull(), anyOrNull())
.addActivity(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())

// verify no saving when blank date
completeAllFields(composeTestRule)
composeTestRule.onNodeWithTag("dateField").performTextClearance()
composeTestRule.onNodeWithTag("saveButton").performClick()
verify(mockActivityRepositoryFirebase, never())
.addActivity(anyOrNull(), anyOrNull(), anyOrNull())
.addActivity(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
}

@Test
fun doesSaveWhenFieldsAreFull() {
composeTestRule.setContent { AddActivityScreen(navigationActions, mockActivityModelView) }
composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, eventViewModel = eventViewModel)
}
completeAllFields(composeTestRule)

whenever(eventViewModel.getNewDocumentReference())
.thenReturn(mock(DocumentReference::class.java))

composeTestRule.onNodeWithTag("saveButton").performClick()
verify(mockActivityRepositoryFirebase).addActivity(anyOrNull(), anyOrNull(), anyOrNull())
verify(mockActivityRepositoryFirebase)
.addActivity(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
}

@Test
fun dateFormattingWorksCorrectly() {
composeTestRule.setContent { AddActivityScreen(navigationActions, mockActivityModelView) }
composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, eventViewModel = eventViewModel)
}
composeTestRule.onNodeWithTag("dateField").performTextClearance()
composeTestRule.onNodeWithTag("dateField").performTextInput("00000000")
val result =
Expand All @@ -147,19 +168,23 @@ class AddActivityScreenTest {

@Test
fun doesNotSaveWhenDateIsWrong() {
composeTestRule.setContent { AddActivityScreen(navigationActions, mockActivityModelView) }
composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, eventViewModel = eventViewModel)
}
completeAllFields(composeTestRule)
composeTestRule.onNodeWithTag("dateField").performTextClearance()
composeTestRule.onNodeWithTag("dateField").performTextInput("00000000")

composeTestRule.onNodeWithTag("saveButton").performClick()
verify(mockActivityRepositoryFirebase, never())
.addActivity(anyOrNull(), anyOrNull(), anyOrNull())
.addActivity(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
}

@Test
fun noCharacterAllowedInDateField() {
composeTestRule.setContent { AddActivityScreen(navigationActions, mockActivityModelView) }
composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, eventViewModel = eventViewModel)
}
composeTestRule.onNodeWithTag("dateField").performTextClearance()
composeTestRule.onNodeWithTag("dateField").performTextInput("mdkdk")
var result =
Expand All @@ -178,7 +203,11 @@ class AddActivityScreenTest {
@Test
fun limitOfEightCharactersInDateField() {
composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, mockLocationViewModel)
AddActivityScreen(
navigationActions,
mockActivityModelView,
mockLocationViewModel,
eventViewModel = eventViewModel)
}
composeTestRule.onNodeWithTag("dateField").performTextClearance()
composeTestRule.onNodeWithTag("dateField").performTextInput("01234567")
Expand All @@ -192,7 +221,11 @@ class AddActivityScreenTest {
fun locationDropdownAppearsAndSelectionWorks() {
val testQuery = "Paris"
composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, mockLocationViewModel)
AddActivityScreen(
navigationActions,
mockActivityModelView,
mockLocationViewModel,
eventViewModel = eventViewModel)
}

// Type in the location field
Expand All @@ -219,7 +252,11 @@ class AddActivityScreenTest {
// TimePickerDialog.

composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, mockLocationViewModel)
AddActivityScreen(
navigationActions,
mockActivityModelView,
mockLocationViewModel,
eventViewModel = eventViewModel)
}

// The time field should be displayed
Expand All @@ -232,7 +269,11 @@ class AddActivityScreenTest {
fun datePickerDialogOpensOnClick() {

composeTestRule.setContent {
AddActivityScreen(navigationActions, mockActivityModelView, mockLocationViewModel)
AddActivityScreen(
navigationActions,
mockActivityModelView,
mockLocationViewModel,
eventViewModel = eventViewModel)
}

// The date field should be displayed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class EditActivityScreenTest {

composeTestRule.onNodeWithTag("saveButton").performClick()
verify(mockActivityRepositoryFirebase, never())
.addActivity(anyOrNull(), anyOrNull(), anyOrNull())
.updateActivity(anyOrNull(), anyOrNull(), anyOrNull())
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.github.se.travelpouch.model.events.Event
import com.github.se.travelpouch.model.events.EventRepository
import com.github.se.travelpouch.model.events.EventType
import com.github.se.travelpouch.model.events.EventViewModel
import com.github.se.travelpouch.ui.navigation.NavigationActions
import com.google.firebase.Timestamp
import org.junit.Before
import org.junit.Rule
Expand All @@ -22,6 +23,7 @@ class TimelineScreenTest {

private lateinit var mockEventRepository: EventRepository
private lateinit var mockEventViewModel: EventViewModel
private lateinit var navigationActions: NavigationActions

@get:Rule val composeTestRule = createComposeRule()

Expand All @@ -31,35 +33,31 @@ class TimelineScreenTest {
*/
val events_test =
listOf(
Event(
"1",
EventType.NEW_DOCUMENT,
Timestamp(0, 0),
"eventTitle",
"eventDescription",
null,
null),
Event("2", EventType.START_OF_JOURNEY, Timestamp(0, 0), "it", "it", null, null),
Event("3", EventType.NEW_PARTICIPANT, Timestamp(0, 0), "it", "it", null, null),
Event("3", EventType.OTHER_EVENT, Timestamp(0, 0), "it", "it", null, null))
Event("1", EventType.NEW_ACTIVITY, Timestamp(0, 0), "eventTitle", "eventDescription"),
Event("2", EventType.START_OF_JOURNEY, Timestamp(0, 0), "it", "it"),
Event("3", EventType.NEW_PARTICIPANT, Timestamp(0, 0), "it", "it"),
Event("3", EventType.PARTICIPANT_REMOVED, Timestamp(0, 0), "it", "it"))

@Before
fun setUp() {
mockEventRepository = mock(EventRepository::class.java)
mockEventViewModel = EventViewModel(mockEventRepository)

navigationActions = mock()
// `when`(mockEventViewModel.events.value).thenReturn(events_test)

}

@Test
fun everythingDisplayed() {
composeTestRule.setContent { TimelineScreen(mockEventViewModel) }
composeTestRule.setContent { TimelineScreen(mockEventViewModel, navigationActions) }

`when`(mockEventRepository.getEvents(any(), any())).then {
it.getArgument<(List<Event>) -> Unit>(0)(events_test)
}

composeTestRule.onNodeWithTag("screenTitle").assertTextEquals("Your travel Milestone")
composeTestRule.onNodeWithTag("goBackButton").assertIsDisplayed()

mockEventViewModel.getEvents()

composeTestRule.onAllNodes(hasTestTag("boxContainingEvent")).apply {
Expand All @@ -76,12 +74,12 @@ class TimelineScreenTest {
composeTestRule.setContent { TimelineItem(events_test[0], Modifier) }

composeTestRule.onNodeWithTag("eventType").assertIsDisplayed()
composeTestRule.onNodeWithTag("eventType").assertTextEquals("NEW_DOCUMENT")
composeTestRule.onNodeWithTag("eventType").assertTextEquals("NEW_ACTIVITY")

composeTestRule.onNodeWithTag("eventTitle").assertIsDisplayed()
composeTestRule.onNodeWithTag("eventTitle").assertTextEquals("eventTitle")
composeTestRule.onNodeWithTag("eventTitle").assertTextEquals("eventDescription")

composeTestRule.onNodeWithTag("eventDate").assertIsDisplayed()
composeTestRule.onNodeWithTag("eventDate").assertTextEquals("1/1/1970")
composeTestRule.onNodeWithTag("eventDate").assertTextEquals("01/01/1970 at 12:00:00 AM")
}
}
Loading

0 comments on commit a2aaa4d

Please sign in to comment.