|
| 1 | +import React from 'react' |
| 2 | +import AddNewLessonPage from '../../../../../pages/admin/lessons/new' |
| 3 | +import GET_APP from '../../../../../graphql/queries/getApp' |
| 4 | +import CREATE_LESSON from '../../../../../graphql/queries/createLesson' |
| 5 | +import dummySessionData from '../../../../../__dummy__/sessionData' |
| 6 | +import dummyLessonsData from '../../../../../__dummy__/lessonData' |
| 7 | +import dummyAlertData from '../../../../../__dummy__/alertData' |
| 8 | +import { act, render, screen } from '@testing-library/react' |
| 9 | +import { MockedProvider } from '@apollo/client/testing' |
| 10 | +import userEvent from '@testing-library/user-event' |
| 11 | +import { useRouter } from 'next/router' |
| 12 | +import * as Sentry from '@sentry/browser' |
| 13 | + |
| 14 | +// Imported to be able to use expect(...).toBeInTheDocument() |
| 15 | +import '@testing-library/jest-dom' |
| 16 | + |
| 17 | +const createdLesson = { |
| 18 | + id: null, |
| 19 | + title: 'New Lesson', |
| 20 | + description: 'New description', |
| 21 | + docUrl: '', |
| 22 | + githubUrl: '', |
| 23 | + videoUrl: '', |
| 24 | + order: 10, |
| 25 | + slug: 'js10', |
| 26 | + chatUrl: '' |
| 27 | +} |
| 28 | + |
| 29 | +const createLessonMock = { |
| 30 | + request: { |
| 31 | + query: CREATE_LESSON, |
| 32 | + variables: { |
| 33 | + title: 'New Lesson', |
| 34 | + description: 'New description', |
| 35 | + docUrl: '', |
| 36 | + githubUrl: '', |
| 37 | + videoUrl: '', |
| 38 | + order: 10, |
| 39 | + slug: 'js10', |
| 40 | + chatUrl: '' |
| 41 | + } |
| 42 | + }, |
| 43 | + result: { |
| 44 | + data: { |
| 45 | + createLesson: dummyLessonsData |
| 46 | + } |
| 47 | + }, |
| 48 | + newData: jest.fn(() => ({ |
| 49 | + data: { |
| 50 | + createLesson: dummyLessonsData |
| 51 | + } |
| 52 | + })) |
| 53 | +} |
| 54 | +const createLessonMockWithError = { |
| 55 | + request: { |
| 56 | + query: CREATE_LESSON, |
| 57 | + variables: { |
| 58 | + title: 'New Lesson', |
| 59 | + description: 'New description', |
| 60 | + docUrl: '', |
| 61 | + githubUrl: '', |
| 62 | + videoUrl: '', |
| 63 | + order: 10, |
| 64 | + slug: 'js10', |
| 65 | + chatUrl: '' |
| 66 | + } |
| 67 | + }, |
| 68 | + error: new Error('error') |
| 69 | +} |
| 70 | + |
| 71 | +const getAppQueryMock = { |
| 72 | + request: { query: GET_APP }, |
| 73 | + result: { |
| 74 | + data: { |
| 75 | + session: dummySessionData, |
| 76 | + lessons: dummyLessonsData, |
| 77 | + alerts: dummyAlertData |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +const mocks = [getAppQueryMock, createLessonMock] |
| 83 | +const mocksWithError = [getAppQueryMock, createLessonMockWithError] |
| 84 | + |
| 85 | +describe('AddNewLesson Page', () => { |
| 86 | + const { push } = useRouter() |
| 87 | + |
| 88 | + it('Should add a new lesson', async () => { |
| 89 | + expect.assertions(1) |
| 90 | + |
| 91 | + render( |
| 92 | + <MockedProvider mocks={mocks}> |
| 93 | + <AddNewLessonPage /> |
| 94 | + </MockedProvider> |
| 95 | + ) |
| 96 | + |
| 97 | + await act(() => new Promise(res => setTimeout(res, 0))) |
| 98 | + |
| 99 | + const title = screen.getByTestId('input0') |
| 100 | + await userEvent.type(title, createdLesson.title) |
| 101 | + |
| 102 | + const description = screen.getByTestId('textbox') |
| 103 | + await userEvent.type(description, createdLesson.description) |
| 104 | + |
| 105 | + const submitBtn = screen.getByText('Create Lesson') |
| 106 | + await userEvent.click(submitBtn) |
| 107 | + |
| 108 | + expect(createLessonMock.newData).toBeCalled() |
| 109 | + }) |
| 110 | + |
| 111 | + it('Should not add a new lesson if inputs are invalid', async () => { |
| 112 | + expect.assertions(1) |
| 113 | + |
| 114 | + render( |
| 115 | + <MockedProvider mocks={mocks}> |
| 116 | + <AddNewLessonPage /> |
| 117 | + </MockedProvider> |
| 118 | + ) |
| 119 | + |
| 120 | + await act(() => new Promise(res => setTimeout(res, 0))) |
| 121 | + |
| 122 | + const submitBtn = screen.getByText('Create Lesson') |
| 123 | + await userEvent.click(submitBtn) |
| 124 | + |
| 125 | + expect(createLessonMock.newData).not.toBeCalled() |
| 126 | + }) |
| 127 | + |
| 128 | + it('Should catch error if it could not add a new lesson', async () => { |
| 129 | + expect.assertions(1) |
| 130 | + |
| 131 | + jest.spyOn(Sentry, 'captureException') |
| 132 | + |
| 133 | + render( |
| 134 | + <MockedProvider mocks={mocksWithError}> |
| 135 | + <AddNewLessonPage /> |
| 136 | + </MockedProvider> |
| 137 | + ) |
| 138 | + |
| 139 | + await act(() => new Promise(res => setTimeout(res, 0))) |
| 140 | + |
| 141 | + const title = screen.getByTestId('input0') |
| 142 | + await userEvent.type(title, createdLesson.title) |
| 143 | + |
| 144 | + const description = screen.getByTestId('textbox') |
| 145 | + await userEvent.type(description, createdLesson.description) |
| 146 | + |
| 147 | + const submitBtn = screen.getByText('Create Lesson') |
| 148 | + await userEvent.click(submitBtn) |
| 149 | + |
| 150 | + // Waiting for the mutation to throw an error |
| 151 | + await act(() => new Promise(res => setTimeout(res, 0))) |
| 152 | + |
| 153 | + expect(Sentry.captureException).toBeCalled() |
| 154 | + }) |
| 155 | + |
| 156 | + it('Should close success alert', async () => { |
| 157 | + expect.assertions(1) |
| 158 | + |
| 159 | + render( |
| 160 | + <MockedProvider mocks={mocks}> |
| 161 | + <AddNewLessonPage /> |
| 162 | + </MockedProvider> |
| 163 | + ) |
| 164 | + |
| 165 | + await act(() => new Promise(res => setTimeout(res, 0))) |
| 166 | + |
| 167 | + const title = screen.getByTestId('input0') |
| 168 | + await userEvent.type(title, createdLesson.title) |
| 169 | + |
| 170 | + const description = screen.getByTestId('textbox') |
| 171 | + await userEvent.type(description, createdLesson.description) |
| 172 | + |
| 173 | + const submitBtn = screen.getByText('Create Lesson') |
| 174 | + await userEvent.click(submitBtn) |
| 175 | + |
| 176 | + await userEvent.click(await screen.findByLabelText('Close alert')) |
| 177 | + |
| 178 | + expect(screen.queryByRole('alert')).not.toBeInTheDocument() |
| 179 | + }) |
| 180 | + |
| 181 | + it('Should close error alert', async () => { |
| 182 | + expect.assertions(1) |
| 183 | + |
| 184 | + render( |
| 185 | + <MockedProvider mocks={mocksWithError}> |
| 186 | + <AddNewLessonPage /> |
| 187 | + </MockedProvider> |
| 188 | + ) |
| 189 | + |
| 190 | + await act(() => new Promise(res => setTimeout(res, 0))) |
| 191 | + |
| 192 | + const title = screen.getByTestId('input0') |
| 193 | + await userEvent.type(title, createdLesson.title) |
| 194 | + |
| 195 | + const description = screen.getByTestId('textbox') |
| 196 | + await userEvent.type(description, createdLesson.description) |
| 197 | + |
| 198 | + const submitBtn = screen.getByText('Create Lesson') |
| 199 | + await userEvent.click(submitBtn) |
| 200 | + |
| 201 | + await userEvent.click(await screen.findByLabelText('Close alert')) |
| 202 | + |
| 203 | + expect(screen.queryByRole('alert')).not.toBeInTheDocument() |
| 204 | + }) |
| 205 | +}) |
0 commit comments