-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2263 from bryanjenningz/add-header-and-alerts-to-…
…exercises-page Add header and alerts and errors to the DOJO exercises page
- Loading branch information
Showing
2 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,117 @@ | ||
import React from 'react' | ||
import { render, waitFor } from '@testing-library/react' | ||
import { render, waitFor, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
import Exercises from '../../../pages/exercises/[lessonSlug]' | ||
import { useRouter } from 'next/router' | ||
import { MockedProvider } from '@apollo/client/testing' | ||
import dummyLessonData from '../../../__dummy__/lessonData' | ||
import dummySessionData from '../../../__dummy__/sessionData' | ||
import dummyAlertData from '../../../__dummy__/alertData' | ||
import GET_APP from '../../../graphql/queries/getApp' | ||
|
||
const session = { | ||
...dummySessionData, | ||
submissions: [], | ||
lessonStatus: [] | ||
} | ||
|
||
describe('Exercises page', () => { | ||
const { query } = useRouter() | ||
query['lessonSlug'] = 'js0' | ||
test('Should render correctly', async () => { | ||
const { getByRole } = render(<Exercises />) | ||
const mocks = [ | ||
{ | ||
request: { query: GET_APP }, | ||
result: { | ||
data: { | ||
session, | ||
lessons: dummyLessonData, | ||
alerts: dummyAlertData | ||
} | ||
} | ||
} | ||
] | ||
|
||
render( | ||
<MockedProvider mocks={mocks} addTypename={false}> | ||
<Exercises /> | ||
</MockedProvider> | ||
) | ||
|
||
await waitFor(() => | ||
screen.getByRole('heading', { name: /Foundations of JavaScript/i }) | ||
) | ||
}) | ||
|
||
test('Should render a 500 error page if the lesson data is null', async () => { | ||
const mocks = [ | ||
{ | ||
request: { query: GET_APP }, | ||
result: { | ||
data: { | ||
session, | ||
lessons: null, | ||
alerts: dummyAlertData | ||
} | ||
} | ||
} | ||
] | ||
|
||
render( | ||
<MockedProvider mocks={mocks} addTypename={false}> | ||
<Exercises /> | ||
</MockedProvider> | ||
) | ||
|
||
await waitFor(() => screen.getByRole('heading', { name: /500 Error/i })) | ||
}) | ||
|
||
test('Should render a 404 error page if the lesson is not found', async () => { | ||
const mocks = [ | ||
{ | ||
request: { query: GET_APP }, | ||
result: { | ||
data: { | ||
session, | ||
lessons: [], | ||
alerts: dummyAlertData | ||
} | ||
} | ||
} | ||
] | ||
|
||
render( | ||
<MockedProvider mocks={mocks} addTypename={false}> | ||
<Exercises /> | ||
</MockedProvider> | ||
) | ||
|
||
await waitFor(() => screen.getByRole('heading', { name: /404 Error/i })) | ||
}) | ||
|
||
test('Should render a loading spinner if useRouter is not ready', async () => { | ||
useRouter.mockImplementation(() => ({ | ||
isReady: false | ||
})) | ||
const mocks = [ | ||
{ | ||
request: { query: GET_APP }, | ||
result: { | ||
data: { | ||
session, | ||
lessons: dummyLessonData, | ||
alerts: dummyAlertData | ||
} | ||
} | ||
} | ||
] | ||
|
||
render( | ||
<MockedProvider mocks={mocks} addTypename={false}> | ||
<Exercises /> | ||
</MockedProvider> | ||
) | ||
|
||
getByRole('heading', { name: /Foundations of JavaScript/i }) | ||
await waitFor(() => screen.getByText('Loading...')) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,39 @@ | ||
import { useRouter } from 'next/router' | ||
import React from 'react' | ||
import Layout from '../../components/Layout' | ||
import withQueryLoader, { | ||
QueryDataProps | ||
} from '../../containers/withQueryLoader' | ||
import { GetAppQuery } from '../../graphql' | ||
import Error, { StatusCode } from '../../components/Error' | ||
import LoadingSpinner from '../../components/LoadingSpinner' | ||
import GET_APP from '../../graphql/queries/getApp' | ||
import AlertsDisplay from '../../components/AlertsDisplay' | ||
|
||
const Exercises = () => { | ||
return <h1>Foundations of JavaScript</h1> | ||
const Exercises: React.FC<QueryDataProps<GetAppQuery>> = ({ queryData }) => { | ||
const { lessons, alerts } = queryData | ||
const router = useRouter() | ||
if (!router.isReady) return <LoadingSpinner /> | ||
|
||
const slug = router.query.lessonSlug as string | ||
if (!lessons || !alerts) | ||
return <Error code={StatusCode.INTERNAL_SERVER_ERROR} message="Bad data" /> | ||
|
||
const currentLesson = lessons.find(lesson => lesson.slug === slug) | ||
if (!currentLesson) | ||
return <Error code={StatusCode.NOT_FOUND} message="Lesson not found" /> | ||
|
||
return ( | ||
<Layout title={currentLesson.title}> | ||
<h1>{currentLesson.title}</h1> | ||
{alerts && <AlertsDisplay alerts={alerts} />} | ||
</Layout> | ||
) | ||
} | ||
|
||
export default Exercises | ||
export default withQueryLoader<GetAppQuery>( | ||
{ | ||
query: GET_APP | ||
}, | ||
Exercises | ||
) |
9912c74
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
c0d3-app – ./
c0d3-app-git-master-c0d3-prod.vercel.app
c0d3-app.vercel.app
c0d3-app-c0d3-prod.vercel.app
v2.c0d3.app
www.c0d3.com