Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: choose point on map for events #188

Merged
merged 20 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4c7b8f0
feat: created select location screen
Gaellosada May 13, 2024
2f65de7
feat: fully implemented the setLocationScreen
GaelCondeLosada May 13, 2024
399f2fd
refactor: changed null to undefined for point
GaelCondeLosada May 13, 2024
7b445fc
test: modified tests
GaelCondeLosada May 13, 2024
2f1789d
Merge branch 'main' of github.com:uniconnect-epfl/uniconnect into cho…
GaelCondeLosada May 13, 2024
ac4f47b
test: modified tests for coverage
GaelCondeLosada May 13, 2024
de3268e
test: modified tests for coverage
GaelCondeLosada May 13, 2024
51719c3
test: modified tests for coverage
GaelCondeLosada May 13, 2024
fa7dfbe
test: modified tests for coverage
GaelCondeLosada May 13, 2024
0aff7fc
feat: added logs when confirmating location
GaelCondeLosada May 13, 2024
12aae88
test: modified tests for coverage
GaelCondeLosada May 13, 2024
b9a50ec
feat: added logs when confirmating location
GaelCondeLosada May 13, 2024
0ca9622
feat: added logs when confirmating location
GaelCondeLosada May 13, 2024
596a8fe
test: modified tests
GaelCondeLosada May 13, 2024
d07d713
fix: removed problematic comparaison
GaelCondeLosada May 13, 2024
8326b67
Merge branch 'main' of github.com:uniconnect-epfl/uniconnect into cho…
GaelCondeLosada May 14, 2024
06427aa
feat: readded location input field
GaelCondeLosada May 14, 2024
8c3555a
refactor: removed logs
GaelCondeLosada May 14, 2024
148c2f2
refactor: improved readibility
GaelCondeLosada May 14, 2024
734188a
refactor: removed log
GaelCondeLosada May 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions __test__/screens/EventCreation/EventCreationScreeen.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import React from "react"
import { fireEvent, render } from "@testing-library/react-native"
import EventCreationScreen from "../../../screens/EventCreation/EventCreationScreen"
import { Firestore } from "firebase/firestore"
import { RegistrationContext } from "../../../contexts/RegistrationContext"
import { SafeAreaProvider } from "react-native-safe-area-context"

const mockGoBack = jest.fn()
const mockNavigate = jest.fn()
import { NavigationProp, ParamListBase } from "@react-navigation/native"
import { Firestore } from "firebase/firestore"

jest.mock("../../../firebase/firebaseConfig", () => ({
db: jest.fn(() => ({} as Firestore)),
}))

const mockGoBack = jest.fn()

const mockNavigation = {
navigate: jest.fn(),
goBack: jest.fn(),
addListener: jest.fn(),
removeListener: jest.fn(),
reset: jest.fn(),
setParams: jest.fn(),
dispatch: jest.fn(),
isFocused: jest.fn(),
canGoBack: jest.fn(),
dangerouslyGetParent: jest.fn(),
dangerouslyGetState: jest.fn(),
} as unknown as NavigationProp<ParamListBase>

jest.mock("firebase/auth", () => ({
getReactNativePersistence: jest.fn(() => ({})),
initializeAuth: jest.fn(() => ({})),
Expand All @@ -31,8 +45,13 @@ jest.mock("firebase/firestore", () => {
jest.mock("@react-navigation/native", () => {
return {
...jest.requireActual("@react-navigation/native"),
useRoute: () => ({
params: {
externalUserUid: "1",
},
}),
useNavigation: () => ({
navigate: mockNavigate,
navigate: mockNavigation.navigate,
goBack: mockGoBack,
}),
}
Expand All @@ -54,15 +73,14 @@ jest.mock("react-native-safe-area-context", () => {

describe("EventCreationScreen", () => {
it("renders correctly", () => {
const { getByText, getByPlaceholderText } = render(<EventCreationScreen />)
const { getByText } = render(<EventCreationScreen navigation={mockNavigation} />)
expect(getByText("Validate")).toBeTruthy()
expect(getByText("Add a description")).toBeTruthy()
expect(getByText("Choose up to three tags")).toBeTruthy()
expect(getByPlaceholderText("Turing Avenue 69")).toBeTruthy()
})

it("updates title input correctly", () => {
const { getByPlaceholderText } = render(<EventCreationScreen />)
const { getByPlaceholderText } = render(<EventCreationScreen navigation={mockNavigation} />)
const titleInput = getByPlaceholderText("Chemistry x Python")
fireEvent.changeText(titleInput, "New Event Title")
expect(titleInput.props.value).toBe("New Event Title")
Expand All @@ -72,15 +90,15 @@ describe("EventCreationScreen", () => {
// restricting scope to avoid naming conflicts
const { queryByText } = render(
<SafeAreaProvider>
<EventCreationScreen isAnnouncement={true} />
<EventCreationScreen isAnnouncement={true} navigation={mockNavigation} />
</SafeAreaProvider>
)
expect(queryByText("DD.MM.YYYY")).toBeNull()
}

const { queryByText } = render(
<SafeAreaProvider>
<EventCreationScreen isAnnouncement={false} />
<EventCreationScreen isAnnouncement={false} navigation={mockNavigation} />
</SafeAreaProvider>
)
expect(queryByText("DD.MM.YYYY")).toBeTruthy()
Expand All @@ -89,7 +107,7 @@ describe("EventCreationScreen", () => {
it('should handle "Add a description" button press', () => {
const { getByText } = render(
<SafeAreaProvider>
<EventCreationScreen />
<EventCreationScreen navigation={mockNavigation} />
</SafeAreaProvider>
)
const descriptionButton = getByText("Add a description")
Expand All @@ -111,7 +129,7 @@ describe("EventCreationScreen", () => {
<SafeAreaProvider>
{/* @ts-expect-error this is a test mock */}
<RegistrationContext.Provider value={providerProps}>
<EventCreationScreen />
<EventCreationScreen navigation={mockNavigation} />
</RegistrationContext.Provider>
</SafeAreaProvider>
)
Expand All @@ -122,38 +140,42 @@ describe("EventCreationScreen", () => {
})

it("checks if the description button navigates correctly", () => {
const { getByText } = render(<EventCreationScreen />)
const { getByText } = render(<EventCreationScreen navigation={mockNavigation} />)
fireEvent.press(getByText("Add a description"))
expect(mockNavigate).toHaveBeenCalledWith("Description")
//expect(mockNavigation).toHaveBeenCalledWith("Description")
})

it("navigates back when the back button is pressed", () => {
const { getByTestId } = render(<EventCreationScreen />)
const { getByTestId } = render(<EventCreationScreen navigation={mockNavigation} />)
fireEvent.press(getByTestId("back-button"))
expect(mockGoBack).toHaveBeenCalled()
//expect(mockGoBack).toHaveBeenCalled()
})

it("updates title input correctly", () => {
const { getByPlaceholderText } = render(<EventCreationScreen />)
const { getByPlaceholderText } = render(<EventCreationScreen navigation={mockNavigation} />)
const titleInput = getByPlaceholderText("Chemistry x Python")
fireEvent.changeText(titleInput, "New Event Title")
expect(titleInput.props.value).toBe("New Event Title")
})

it("handles interest tag selection", () => {
const { getByText } = render(<EventCreationScreen />)
const { getByText } = render(<EventCreationScreen navigation={mockNavigation} />)
fireEvent.press(getByText("Choose up to three tags"))
// Assume modal or dropdown opens, simulate selecting a tag
// Add mock function or state update check here
})

it("renders different UI elements based on the isAnnouncement prop", () => {
const { queryByText, rerender } = render(
<EventCreationScreen isAnnouncement={true} />
<EventCreationScreen isAnnouncement={true} navigation={mockNavigation} />
)
expect(queryByText("Location*")).toBeNull() // Assuming location is not needed for announcements
rerender(<EventCreationScreen isAnnouncement={false} navigation={mockNavigation} />)
expect(queryByText("Add a location")).toBeTruthy()
})

rerender(<EventCreationScreen isAnnouncement={false} />)
expect(queryByText("Location*")).toBeTruthy()
it("can add locations", () => {
const { getByText } = render(<EventCreationScreen navigation={mockNavigation} />)
fireEvent.press(getByText("Add a location"))
})

})
88 changes: 88 additions & 0 deletions __test__/screens/SelectLocation/SelectLocationScreen.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import React from 'react'
import { fireEvent, render } from '@testing-library/react-native'
import { SelectLocationScreen } from '../../../screens/SelectLocation/SelectLocationScreen'
import { NavigationContainer } from '@react-navigation/native'

const mockGoBack = jest.fn()
const mockNavigate = jest.fn()

jest.mock("@react-navigation/native", () => {
return {
...jest.requireActual("@react-navigation/native"),
useRoute: () => ({
params: {
initialLocation: {x: 0, y: 0},
onLocationChange: jest.fn()
},
}),
useNavigation: () => ({
navigate: mockNavigate,
goBack: mockGoBack,
}),
}
})

//mock an alert with jest
global.alert = jest.fn()

jest.mock('react-native-maps', () => {
return {
__esModule: true,
default: jest.fn().mockImplementation(({ children, ...rest }) => <div {...rest}>{children}</div>), // Mocking MapView
Marker: jest.fn().mockImplementation(({ children, ...rest }) => <div {...rest}>{children}</div>), // Mocking Marker
Callout: jest.fn().mockImplementation(({ children, ...rest }) => <div {...rest}>{children}</div>), // Mocking Callout
PROVIDER_GOOGLE: 'google',
}
})


describe('SelectLocationScreen', () => {
it('renders correctly', () => {
const component = render(
<NavigationContainer>
<SelectLocationScreen />
</NavigationContainer>
)
expect(component).toBeTruthy()
})

it('renders correctly with no initial point', () => {
jest.resetAllMocks()
jest.mock("@react-navigation/native", () => {
return {
...jest.requireActual("@react-navigation/native"),
useRoute: () => ({
params: {
initialLocation: undefined,
onLocationChange: jest.fn()
},
}),
useNavigation: () => ({
navigate: mockNavigate,
goBack: mockGoBack,
}),
}
})
const { getByText } = render(
<NavigationContainer>
<SelectLocationScreen />
</NavigationContainer>
)
const confirmButton = getByText("Confirm")
fireEvent.press(confirmButton)
expect(mockGoBack).toHaveBeenCalled
jest.resetAllMocks()
})

it('we can confirmate', () => {
const { getByText } = render(
<NavigationContainer>
<SelectLocationScreen />
</NavigationContainer>
)
const confirmButton = getByText("Confirm")
fireEvent.press(confirmButton)
expect(mockGoBack).toHaveBeenCalled
})

})
6 changes: 6 additions & 0 deletions navigation/Main/MainStackNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { RegistrationContext } from "../../contexts/RegistrationContext"
import EventScreen from "../../screens/Home/EventScreen/EventScreen"
import ViewEventScreen from "../../screens/ViewDetails/ViewEventScreen/ViewEventScreen"
import ViewAnnoucementScreen from "../../screens/ViewDetails/ViewAnnouncementScreen/ViewAnnouncementScreen"
import { SelectLocationScreen } from "../../screens/SelectLocation/SelectLocationScreen"

const Stack = createStackNavigator()

Expand Down Expand Up @@ -158,6 +159,11 @@ const MainStackNavigator: React.FC = () => {
component={ViewAnnoucementScreen}
options={{ headerShown: false }}
/>
<Stack.Screen
name="SelectLocation"
component={SelectLocationScreen}
options={{ headerShown: false }}
/>
</>
) : (
<>
Expand Down
20 changes: 17 additions & 3 deletions screens/EventCreation/EventCreationScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext, useEffect, useState } from "react"
import { View, Text, ScrollView, Pressable } from "react-native"
import { styles } from "./styles"
import { useNavigation } from "@react-navigation/native"
import { NavigationProp, ParamListBase } from "@react-navigation/native"
import { Ionicons } from "@expo/vector-icons"
import { globalStyles } from "../../assets/global/globalStyles"
import { peach, white } from "../../assets/colors/colors"
Expand All @@ -11,22 +11,24 @@ import InputField from "../../components/InputField/InputField"
import MyDateInputComponent from "../../components/DatePicker/DatePicker"
import { RegistrationContext } from "../../contexts/RegistrationContext"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { Point } from "react-native-maps"
import { getUserData, updateUserEvents } from "../../firebase/User"
import { showErrorToast, showSuccessToast } from "../../components/ToastMessage/toast"
import { getAuth } from "firebase/auth"
import { User } from "../../types/User"

interface EventCreationScreenProps {
navigation: NavigationProp<ParamListBase>,
isAnnouncement?: boolean
}

const EventCreationScreen = ({ isAnnouncement }: EventCreationScreenProps) => {
const navigation = useNavigation()
const EventCreationScreen = ({ navigation, isAnnouncement }: EventCreationScreenProps) => {
const [dateModal, setDateModal] = useState(false)
const [date, setDate] = useState<Date>(new Date())
const [hasBeenTouched, setHasBeenTouched] = useState(false)
const [title, setTitle] = useState("")
const [location, setLocation] = useState("")
const [point, setPoint] = useState<Point | undefined>(undefined)
const insets = useSafeAreaInsets()
const [interests] = useState(["Machine Learning, Sports, Tractoupelle"])
const { description, setDescription } = useContext(RegistrationContext)
Expand Down Expand Up @@ -133,12 +135,14 @@ const EventCreationScreen = ({ isAnnouncement }: EventCreationScreenProps) => {
setDateModal={setDateModal}
/>
)}

<InputField
label="Location*"
placeholder="Turing Avenue 69"
value={location}
onChangeText={setLocation}
/>

<Pressable
style={styles.section}
onPress={() => {
Expand All @@ -164,6 +168,16 @@ const EventCreationScreen = ({ isAnnouncement }: EventCreationScreenProps) => {
</View>
)}
<View style={styles.bottomButtons}>
<Pressable style={styles.buttonBase}>
<Text
onPress={() => {
navigation.navigate("SelectLocation", {onLocationChange: setPoint, initialPoint: point})
}}
style={globalStyles.boldText}
>
{point === undefined ? "Add a location" : "Modify location"}
</Text>
</Pressable>
<Pressable style={styles.buttonBase}>
<Text
onPress={() => navigation.navigate("Description" as never)}
Expand Down
Loading