Skip to content

Commit

Permalink
Merge pull request #25 from CS428-CT/fix-offerings
Browse files Browse the repository at this point in the history
Fix offerings
  • Loading branch information
Jagaskak authored Apr 10, 2021
2 parents 5c397d3 + d0ac3c6 commit 10de6a1
Show file tree
Hide file tree
Showing 14 changed files with 344 additions and 159 deletions.
57 changes: 57 additions & 0 deletions __tests__/frontend/HomeContainer-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import axios from 'axios'
import React from 'react'
import MockAdapter from 'axios-mock-adapter'
import { render } from '@testing-library/react-native'
import { setUserData } from '../../src/api/auth'
import { ENDPOINTS as UNI_ENDPOINTS } from '../../src/api/universities'
import { ENDPOINTS as OFFER_ENDPOINTS } from '../../src/api/offerings'
import { format } from '../../src/utils/string'

import { HTTP_STATUS_CODES } from '../../src/api'
import { UNIVERSITY_RESPONSE } from '../mock_responses/mock-university-response'
import Home from '../../src/containers/HomeContainer/Home'
import { OFFERINGS_RESPONSE_1 } from '../mock_responses/mock-offerings-response'

// const assertNoButtonsRendered = async () => {
// const { queryAllByA11yRole } = render(<UniversityListContainer />)
// const universityList = await waitFor(() => queryAllByA11yRole('button'))
// expect(universityList.length).toBe(0)
// }

const mock = new MockAdapter(axios)
describe('Check universities rendering', () => {
const USER_DATA = {
authToken: 'A',
universityId: '1001',
userId: 'test user',
emaildId: '[email protected]',
}

const offeringId = 'ac5b1727-629c-443b-8c1a-cc1bd541af6a'

afterEach(() => {
mock.reset()
})

test('Check that components render', async () => {
const mockNavigator = { push: jest.fn() }

mock.onGet(`${UNI_ENDPOINTS.UNIVERSITIES}`).reply(HTTP_STATUS_CODES.OK, UNIVERSITY_RESPONSE)
mock
.onGet(`${OFFER_ENDPOINTS.OFFERINGBYSTUDENT}`)
.reply(HTTP_STATUS_CODES.OK, OFFERINGS_RESPONSE_1)
mock
.onGet(`${format(OFFER_ENDPOINTS.OFFERING, offeringId)}`)
.reply(HTTP_STATUS_CODES.OK, OFFERINGS_RESPONSE_1)

setUserData(USER_DATA)

const { getByTestId } = render(<Home navigation={mockNavigator} />)

const picker = await getByTestId('picker')
expect(picker).not.toBe(null)

const courseList = await getByTestId('courseList')
expect(courseList).not.toBe(null)
})
})
8 changes: 4 additions & 4 deletions __tests__/frontend/PlaylistContainer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,23 @@ describe('Check videos rendering', () => {

describe('Check video navigation', () => {
const playlistId = '51519746-aa6c-485c-9894-549959c457b5'
const mockNaivgator = { push: jest.fn() }
const mockNavigator = { push: jest.fn() }

test('when clicking on first item', async () => {
mock
.onGet(`${format(ENDPOINTS.VIDEOS_BY_PLAYLIST, playlistId)}`)
.reply(HTTP_STATUS_CODES.OK, VIDEOS_BY_PLAYLIST_RESPONSE)

const { queryAllByA11yRole } = render(
<PlaylistContainer playlistId={playlistId} navigation={mockNaivgator} />
<PlaylistContainer playlistId={playlistId} navigation={mockNavigator} />
)
const videos = await waitFor(() => queryAllByA11yRole('button'))
expect(videos.length).not.toBe(0)

fireEvent.press(videos[0])

expect(mockNaivgator.push).toHaveBeenCalled()
expect(mockNaivgator.push).toHaveBeenCalledWith(STACK_SCREENS.VIDEO, {
expect(mockNavigator.push).toHaveBeenCalled()
expect(mockNavigator.push).toHaveBeenCalledWith(STACK_SCREENS.VIDEO, {
videos: VIDEOS_BY_PLAYLIST_RESPONSE.medias,
index: 0,
})
Expand Down
4 changes: 4 additions & 0 deletions __tests__/utils/string-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ describe('Check formatting', () => {
})

describe('Check truncation', () => {
test('check null string', () => {
expect(truncateString(null, 10)).toBe('')
})

test('already short enough', () => {
expect(truncateString('hello', 10)).toBe('hello')
})
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"dependencies": {
"@expo/vector-icons": "^12.0.4",
"@react-native-community/masked-view": "0.1.10",
"@react-native-community/picker": "^1.8.1",
"@react-native-picker/picker": "1.9.2",
"@react-navigation/bottom-tabs": "^5.11.8",
"@react-navigation/native": "^5.9.3",
Expand Down Expand Up @@ -35,7 +36,7 @@
"react-native-gesture-handler": "^1.10.3",
"react-native-navigation": "^7.11.2",
"react-native-paper": "^4.7.2",
"react-native-picker-select": "^8.0.4",
"react-native-picker-select": "8.0.0",
"react-native-reanimated": "~1.13.0",
"react-native-safe-area-context": "3.1.9",
"react-native-screens": "~2.15.2",
Expand Down Expand Up @@ -74,7 +75,7 @@
"build-ios": "expo build:ios",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject --non-interactive --eject-method bare",
"eject": "expo eject --non-interactive --ject-method bare",
"lint": "eslint ."
},
"private": true
Expand Down
15 changes: 15 additions & 0 deletions src/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,26 @@ export const getUserMetadata = async () => {
}
}

/**
* Set the authorization token for the current authenticated user
* @param authToken Pass in the auth token so user can watch CT videos
*/
export const setAuthToken = (token) => {
if (!currentAuthenticatedUser) currentAuthenticatedUser = {}
currentAuthenticatedUser.authToken = token
}

/**
* Initialize user's data using the metadata that is passed in during authorization
* @param userData Pass in userData to obtain each user's information
*/
export const setUserData = (userData) => {
if (!currentAuthenticatedUser) currentAuthenticatedUser = {}
currentAuthenticatedUser.userId = userData?.userId
currentAuthenticatedUser.universityId = userData?.universityId
currentAuthenticatedUser.emailId = userData?.emailId
}

/**
* Returns the signed in user's data or null if no one is signed in.
* See @currentAuthenticatedUser for the attributes returned in the object
Expand Down
46 changes: 46 additions & 0 deletions src/api/offerings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getCurrentAuthenticatedUser, isUserAuthenticated } from './auth'

export const ENDPOINTS = {
OFFERING: `${API_BASE_URL}Offerings/{0}`,
OFFERINGBYSTUDENT: `${API_BASE_URL}Offerings/ByStudent`,
}

/**
Expand All @@ -29,6 +30,51 @@ export const getOfferingData = async (offeringId) => {
return null
}

/// //////////////// STUDENT OFFERING FUNCTIONS ////////////////////

/**
* Gets the data for an offering from the CT API if the student is authenticated
* @returns The offering data
*/
export const getOfferingsByStudent = async () => {
if (!isUserAuthenticated()) return null

const url = ENDPOINTS.OFFERINGBYSTUDENT

try {
const resp = await axios.get(url)
if (resp?.status !== HTTP_STATUS_CODES.OK) {
return null
}
return resp.data
} catch (error) {
console.error(error)
}

return null
}

/**
* Returns an array of all the offering data for the current user.
* If no user is signed in, null is returned.
* @returns Array of offerings data
*/
export const getOfferingsData = async () => {
const offerings = []

const studentOfferings = await getOfferingsByStudent()
if (studentOfferings == null) return null

for (const entry of studentOfferings) {
const offeringData = await getOfferingData(entry.offering.id)
if (offeringData != null) offerings.push(offeringData)
}

return offerings
}

/// //////////////// STARRED OFFERING CALLS //////////////////////

/**
* Returns an array of all the starred offering data for the current user.
* If no user is signed in, null is returned.
Expand Down
37 changes: 37 additions & 0 deletions src/api/video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import axios from 'axios'
import { HTTP_STATUS_CODES } from '.'
import { API_BASE_URL } from '../constants'
import { format } from '../utils/string'

export const ENDPOINTS = {
TRANSCRIPT: `${API_BASE_URL}Captions/ByTranscription/{0}`,
MEDIA: `${API_BASE_URL}Media/{0}`,
}

export const getVideoTranscription = async (transcriptionId) => {
const url = format(ENDPOINTS.TRANSCRIPT, transcriptionId)

try {
const resp = await axios.get(url)
if (resp?.status !== HTTP_STATUS_CODES.OK) return null
return resp.data
} catch (error) {
console.error(error)
}

return null
}

export const getMedia = async (mediaId) => {
const url = format(ENDPOINTS.MEDIA, mediaId.mediaId)

try {
const resp = await axios.get(url)
if (resp?.status !== HTTP_STATUS_CODES.OK) return null
return resp.data
} catch (error) {
console.error(error)
}

return null
}
48 changes: 9 additions & 39 deletions src/containers/CTNavigationContainer/CTNavigationContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@ import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import { createStackNavigator } from '@react-navigation/stack'
import { MaterialCommunityIcons } from '@expo/vector-icons'
import { STACK_SCREENS } from './index'
import Home from '../HomeContainers/Home'
import Home from '../HomeContainer/Home'
import CoursePlaylistsContainer from '../CoursePlaylistsContainer/CoursePlaylistsContainer'
import VideoContainer from '../VideoContainer/VideoContainer'
import UniversityListContainer from '../UniversityListContainer/UniversityListContainer'
import PlaylistContainer from '../PlaylistContainer/PlaylistContainer'
import DepartmentListContainer from '../DepartmentListContainer/DepartmentListContainer'
import CourseListContainer from '../CourseListContainer/CourseListContainer'

const Tab = createBottomTabNavigator()
const Stack = createStackNavigator()

/**
* The navigator of the home tab. Contains a stack navigator.
*/
const HomeNaivgator = () => {
const HomeNavigator = () => {
return (
<Stack.Navigator initialRouteName={STACK_SCREENS.HOME}>
<Stack.Screen name={STACK_SCREENS.HOME} component={Home} />
<Stack.Screen name={STACK_SCREENS.HOME} component={HomeView} />
<Stack.Screen name={STACK_SCREENS.COURSE_PLAYLISTS} component={CoursePlaylistsView} />
<Stack.Screen name={STACK_SCREENS.PLAYLIST} component={PlaylistView} />
<Stack.Screen name={STACK_SCREENS.VIDEO} component={VideoView} />
Expand All @@ -31,35 +28,11 @@ const HomeNaivgator = () => {
}

/**
* Wraps the UniversityListContainer so that it can
* Wraps the Home container so that it can
* receive the proper props
*/
const UniversityListView = ({ navigation }) => {
return <UniversityListContainer navigation={navigation} />
}

/**
* Wraps the DepartmentListContainer so that it can
* receive the proper props
*/
const DepartmentListView = ({ navigation, route }) => {
return (
<DepartmentListContainer universityId={route.params.universityId} navigation={navigation} />
)
}

/**
* Wraps the CourseListContainer so that it can
* receive the proper props
*/
const CourseListView = ({ navigation, route }) => {
return (
<CourseListContainer
departmentId={route.params.departmentId}
acronym={route.params.acronym}
navigation={navigation}
/>
)
const HomeView = ({ navigation }) => {
return <Home navigation={navigation} />
}

/**
Expand Down Expand Up @@ -91,11 +64,8 @@ const VideoView = ({ route }) => {
*/
const CourseNavigator = () => {
return (
<Stack.Navigator initialRouteName={STACK_SCREENS.UNIVERSITY_LIST}>
<Stack.Screen name={STACK_SCREENS.UNIVERSITY_LIST} component={UniversityListView} />
<Stack.Screen name={STACK_SCREENS.DEPT_LIST} component={DepartmentListView} />
<Stack.Screen name={STACK_SCREENS.COURSE_LIST} component={CourseListView} />
<Stack.Screen name={STACK_SCREENS.HOME} component={Home} />
<Stack.Navigator initialRouteName={STACK_SCREENS.HOME}>
<Stack.Screen name={STACK_SCREENS.HOME} component={HomeView} />
<Stack.Screen name={STACK_SCREENS.COURSE_PLAYLISTS} component={CoursePlaylistsView} />
<Stack.Screen name={STACK_SCREENS.PLAYLIST} component={PlaylistView} />
<Stack.Screen name={STACK_SCREENS.VIDEO} component={VideoView} />
Expand Down Expand Up @@ -124,7 +94,7 @@ const CTNavigationContainer = () => {
/>
<Tab.Screen
name="Home"
component={HomeNaivgator}
component={HomeNavigator}
options={{
tabBarLabel: 'Home',
tabBarIcon: ({ color, size }) => (
Expand Down
Loading

0 comments on commit 10de6a1

Please sign in to comment.