diff --git a/__tests__/pages/exercises/[lessonSlug].test.js b/__tests__/pages/exercises/[lessonSlug].test.js index db7e20921..ac2fb5cac 100644 --- a/__tests__/pages/exercises/[lessonSlug].test.js +++ b/__tests__/pages/exercises/[lessonSlug].test.js @@ -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() + const mocks = [ + { + request: { query: GET_APP }, + result: { + data: { + session, + lessons: dummyLessonData, + alerts: dummyAlertData + } + } + } + ] + + render( + + + + ) + + 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( + + + + ) + + 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( + + + + ) + + 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( + + + + ) - getByRole('heading', { name: /Foundations of JavaScript/i }) + await waitFor(() => screen.getByText('Loading...')) }) }) diff --git a/pages/exercises/[lessonSlug].tsx b/pages/exercises/[lessonSlug].tsx index edc52ab72..331d7006c 100644 --- a/pages/exercises/[lessonSlug].tsx +++ b/pages/exercises/[lessonSlug].tsx @@ -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

Foundations of JavaScript

+const Exercises: React.FC> = ({ queryData }) => { + const { lessons, alerts } = queryData + const router = useRouter() + if (!router.isReady) return + + const slug = router.query.lessonSlug as string + if (!lessons || !alerts) + return + + const currentLesson = lessons.find(lesson => lesson.slug === slug) + if (!currentLesson) + return + + return ( + +

{currentLesson.title}

+ {alerts && } +
+ ) } -export default Exercises +export default withQueryLoader( + { + query: GET_APP + }, + Exercises +)