|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import { PageToggleButton } from '../PageToggleButton'; |
| 3 | +import { PageContextProvider } from '../PageContext'; |
| 4 | + |
| 5 | +test('Renders without children', () => { |
| 6 | + render( |
| 7 | + <div data-testid="container"> |
| 8 | + <PageToggleButton /> |
| 9 | + </div> |
| 10 | + ); |
| 11 | + |
| 12 | + expect(screen.getByTestId('container').firstChild).toBeVisible(); |
| 13 | +}); |
| 14 | + |
| 15 | +test('Renders with children', () => { |
| 16 | + render(<PageToggleButton>Test</PageToggleButton>); |
| 17 | + |
| 18 | + expect(screen.getByText('Test')).toBeVisible(); |
| 19 | +}); |
| 20 | + |
| 21 | +test('Throws console error when isHamburger is true and isSidebarOpen is not passed', () => { |
| 22 | + const consoleError = jest.spyOn(console, 'error').mockImplementation(); |
| 23 | + |
| 24 | + render(<PageToggleButton isHamburger aria-label="Test" />); |
| 25 | + |
| 26 | + expect(consoleError).toHaveBeenCalledWith( |
| 27 | + 'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content.' |
| 28 | + ); |
| 29 | +}); |
| 30 | + |
| 31 | +test('Does not throw console error when isHamburger is true and isSidebarOpen is false', () => { |
| 32 | + const consoleError = jest.spyOn(console, 'error').mockImplementation(); |
| 33 | + |
| 34 | + render(<PageToggleButton isSidebarOpen={false} isHamburger aria-label="Test" />); |
| 35 | + |
| 36 | + expect(consoleError).not.toHaveBeenCalledWith( |
| 37 | + 'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content..' |
| 38 | + ); |
| 39 | +}); |
| 40 | + |
| 41 | +test('Does not throw console error when isHamburger is true and isSidebarOpen is true', () => { |
| 42 | + const consoleError = jest.spyOn(console, 'error').mockImplementation(); |
| 43 | + |
| 44 | + render(<PageToggleButton isSidebarOpen isHamburger aria-label="Test" />); |
| 45 | + |
| 46 | + expect(consoleError).not.toHaveBeenCalledWith( |
| 47 | + 'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content..' |
| 48 | + ); |
| 49 | +}); |
| 50 | + |
| 51 | +// assisted by AI/Cursor |
| 52 | +test('Does not throw console error when isHamburger is true, isSidebarOpen is not passed, but managedIsSidebarOpen is true', () => { |
| 53 | + const consoleError = jest.spyOn(console, 'error').mockImplementation(); |
| 54 | + |
| 55 | + const mockPageContext = { |
| 56 | + isManagedSidebar: true, |
| 57 | + onSidebarToggle: () => null, |
| 58 | + isSidebarOpen: true, // managedIsSidebarOpen is true |
| 59 | + width: 1024, |
| 60 | + height: 768, |
| 61 | + getBreakpoint: () => 'lg' as const, |
| 62 | + getVerticalBreakpoint: () => 'lg' as const |
| 63 | + }; |
| 64 | + |
| 65 | + render( |
| 66 | + <PageContextProvider value={mockPageContext}> |
| 67 | + <PageToggleButton isHamburger aria-label="Test" /> |
| 68 | + </PageContextProvider> |
| 69 | + ); |
| 70 | + |
| 71 | + expect(consoleError).not.toHaveBeenCalledWith( |
| 72 | + 'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content..' |
| 73 | + ); |
| 74 | +}); |
0 commit comments