|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +/* eslint-disable import/no-extraneous-dependencies */ |
| 5 | +import { render, screen, fireEvent, act } from '@testing-library/react' |
| 6 | +import { ErrorOverlayLayout } from './error-overlay-layout' |
| 7 | +import '@testing-library/jest-dom' |
| 8 | + |
| 9 | +// Mock maintain--tab-focus module |
| 10 | +jest.mock('../../../components/Overlay/maintain--tab-focus', () => ({ |
| 11 | + __esModule: true, |
| 12 | + default: jest.fn(() => ({ |
| 13 | + disengage: jest.fn(), |
| 14 | + })), |
| 15 | +})) |
| 16 | + |
| 17 | +const renderTestComponent = () => { |
| 18 | + return render( |
| 19 | + <ErrorOverlayLayout |
| 20 | + errorType="Build Error" |
| 21 | + errorMessage="Failed to compile" |
| 22 | + errorCode="E001" |
| 23 | + error={new Error('Sample error')} |
| 24 | + isBuildError={true} |
| 25 | + onClose={() => {}} |
| 26 | + > |
| 27 | + Module not found: Cannot find module './missing-module' |
| 28 | + </ErrorOverlayLayout> |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +describe('ErrorOverlayLayout Component', () => { |
| 33 | + beforeEach(() => { |
| 34 | + // Mock fetch |
| 35 | + global.fetch = jest.fn(() => { |
| 36 | + return Promise.resolve({ |
| 37 | + ok: true, |
| 38 | + headers: new Headers(), |
| 39 | + redirected: false, |
| 40 | + status: 200, |
| 41 | + statusText: 'OK', |
| 42 | + type: 'basic', |
| 43 | + url: '', |
| 44 | + json: () => Promise.resolve({}), |
| 45 | + text: () => Promise.resolve(''), |
| 46 | + blob: () => Promise.resolve(new Blob()), |
| 47 | + arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)), |
| 48 | + formData: () => Promise.resolve(new FormData()), |
| 49 | + clone: () => new Response(), |
| 50 | + } as Response) |
| 51 | + }) as jest.Mock |
| 52 | + }) |
| 53 | + |
| 54 | + test('renders ErrorOverlayLayout with provided props', () => { |
| 55 | + renderTestComponent() |
| 56 | + expect(screen.getByText('Failed to compile')).toBeInTheDocument() |
| 57 | + expect( |
| 58 | + screen.getByText( |
| 59 | + "Module not found: Cannot find module './missing-module'" |
| 60 | + ) |
| 61 | + ).toBeInTheDocument() |
| 62 | + }) |
| 63 | + |
| 64 | + test('sends feedback when clicking helpful button', async () => { |
| 65 | + renderTestComponent() |
| 66 | + |
| 67 | + expect( |
| 68 | + screen.queryByText('Thanks for your feedback!') |
| 69 | + ).not.toBeInTheDocument() |
| 70 | + |
| 71 | + // Click helpful button |
| 72 | + await act(async () => { |
| 73 | + fireEvent.click(screen.getByLabelText('Mark as helpful')) |
| 74 | + }) |
| 75 | + |
| 76 | + expect(fetch).toHaveBeenCalledWith( |
| 77 | + '/__nextjs_error_feedback?errorCode=E001&wasHelpful=true' |
| 78 | + ) |
| 79 | + |
| 80 | + expect(screen.getByText('Thanks for your feedback!')).toBeInTheDocument() |
| 81 | + }) |
| 82 | + |
| 83 | + test('sends feedback when clicking not helpful button', async () => { |
| 84 | + renderTestComponent() |
| 85 | + |
| 86 | + expect( |
| 87 | + screen.queryByText('Thanks for your feedback!') |
| 88 | + ).not.toBeInTheDocument() |
| 89 | + |
| 90 | + await act(async () => { |
| 91 | + fireEvent.click(screen.getByLabelText('Mark as not helpful')) |
| 92 | + }) |
| 93 | + |
| 94 | + expect(fetch).toHaveBeenCalledWith( |
| 95 | + '/__nextjs_error_feedback?errorCode=E001&wasHelpful=false' |
| 96 | + ) |
| 97 | + |
| 98 | + expect(screen.getByText('Thanks for your feedback!')).toBeInTheDocument() |
| 99 | + }) |
| 100 | +}) |
0 commit comments