Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 35 additions & 0 deletions __tests__/components/Layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react'
import { render } from '@testing-library/react'
import Layout from '../../components/Layout'
import SessionContext from '../../helpers/contexts/session'

describe('Layout Component', () => {
test('Should render correctly when no user in session', () => {
const session = { data: null }
const tree = (
<SessionContext.Provider value={session}>
<Layout />
</SessionContext.Provider>
)

const { container } = render(tree)
expect(container).toMatchSnapshot()
})

test('Should render correctly when user session found', () => {
const session = { data: { userInfo: { name:'tester', username: 'tester' } } }
const tree = (
<SessionContext.Provider value={session}>
<Layout />
</SessionContext.Provider>
)

const { container } = render(tree)
expect(container).toMatchSnapshot()
})

test('Should render correct with no session context', () => {
const { container } = render(<Layout />)
expect(container).toMatchSnapshot()
})
})
31 changes: 17 additions & 14 deletions __tests__/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
jest.mock('../../helpers/useSession')
jest.mock('../../pages/curriculum')
jest.mock('../../components/LandingPage')
import React from 'react'
import { render } from '@testing-library/react'
import Curriculum from '../../pages/curriculum'
import IndexPage, {fetcher} from '../../pages/index'
import IndexPage from '../../pages/index'
import LandingPage from '../../components/LandingPage'
import useSession from '../../helpers/useSession'
import SessionContext from '../../helpers/contexts/session'

describe('Index Page', () => {
test('Should render curriculum if user is identified', async () => {
useSession.mockReturnValue({
data: { userInfo: { name:'tester', username: 'tester' } }
})
const session = { data: { userInfo: { name:'tester', username: 'tester' } } }
const tree = (
<SessionContext.Provider value={session}>
<IndexPage />
</SessionContext.Provider>
)
Curriculum.mockReturnValue( <h1>Hello Curriculum</h1> )

const { getByText } = render(<IndexPage />)
const { getByText } = render(tree)
getByText('Hello Curriculum')
})
test('Should render landing page if user is not identified', async () => {
useSession.mockReturnValue({
error: undefined,
data: { success: false, errorMessage: 'unauthorized' }
})
const session = { data: { success: false, errorMessage: 'unauthorized' } }
const tree = (
<SessionContext.Provider value={session}>
<IndexPage />
</SessionContext.Provider>
)
LandingPage.mockReturnValue(<h1>Hello Landing</h1>)

const { getByText } = render(<IndexPage />)
const { getByText } = render(tree)
getByText('Hello Landing')
})
test('Should not render while page is loading', async () => {
useSession.mockReturnValue({})

// SessionContext default value is { data: null, error: null }
const { container } = render(<IndexPage />)
expect(container).toMatchSnapshot()
})
Expand Down
Loading