Skip to content

Commit

Permalink
added one test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagaskak committed Apr 10, 2021
1 parent 6ad9a08 commit d0ac3c6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 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)
})
})
6 changes: 4 additions & 2 deletions src/containers/HomeContainer/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import styles from './Home.style'
*/
const Home = ({ navigation }) => {
const currentUser = getCurrentAuthenticatedUser()
var universityId = currentUser.universityId
let universityId = currentUser.universityId

/**
* Helper function to filter courses by university id
Expand Down Expand Up @@ -88,7 +88,7 @@ const Home = ({ navigation }) => {
}, [setAllUniversities])

const universityItems = universities.map((uni) => {
return <Picker.Item accessibilityRole="button" key={uni.id} value={uni.id} label={uni.name} />
return <Picker.Item key={uni.id} value={uni.id} label={uni.name} />
})

const [university, setUniversity] = useState(universityId)
Expand All @@ -108,6 +108,7 @@ const Home = ({ navigation }) => {

return (
<Picker
testID="picker"
style={{ flex: 0, width: '100%' }}
selectedValue={university}
onValueChange={(newUniversityId) => onUniversitySelected(newUniversityId)}
Expand All @@ -125,6 +126,7 @@ const Home = ({ navigation }) => {

return (
<FlatList
testID="courseList"
keyExtractor={(idxCourses, index) => index.toString()}
data={courses}
renderItem={renderCourseItem}
Expand Down

0 comments on commit d0ac3c6

Please sign in to comment.