|
| 1 | +import {render, screen} from '@testing-library/react'; |
| 2 | +import {describe, expect, it, vi} from 'vitest'; |
| 3 | + |
| 4 | +import Lab from '../components/Lab'; |
| 5 | +import {useLevelProperties} from '../contexts/LevelPropertiesContext'; |
| 6 | +import type {LevelPropertiesMap} from '../types'; |
| 7 | + |
| 8 | +const LEVEL_MAP: LevelPropertiesMap = { |
| 9 | + '29091': {appName: 'fish', mode: 'fishvtrash'}, |
| 10 | + '29092': {appName: 'fish', mode: 'short'}, |
| 11 | +}; |
| 12 | + |
| 13 | +function LevelDisplay() { |
| 14 | + const props = useLevelProperties(); |
| 15 | + return <div data-testid="level-info">{JSON.stringify(props)}</div>; |
| 16 | +} |
| 17 | + |
| 18 | +describe('Lab', () => { |
| 19 | + it('provides level context to children', () => { |
| 20 | + render( |
| 21 | + <Lab levelId={29091} levelPropertiesMap={LEVEL_MAP}> |
| 22 | + <LevelDisplay /> |
| 23 | + </Lab>, |
| 24 | + ); |
| 25 | + |
| 26 | + const info = screen.getByTestId('level-info'); |
| 27 | + expect(JSON.parse(info.textContent!)).toEqual({ |
| 28 | + appName: 'fish', |
| 29 | + mode: 'fishvtrash', |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('updates children when levelId changes without remounting shell', () => { |
| 34 | + const shellMountSpy = vi.fn(); |
| 35 | + |
| 36 | + function ShellSentinel() { |
| 37 | + shellMountSpy(); |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + const {rerender} = render( |
| 42 | + <Lab levelId={29091} levelPropertiesMap={LEVEL_MAP}> |
| 43 | + <ShellSentinel /> |
| 44 | + <LevelDisplay /> |
| 45 | + </Lab>, |
| 46 | + ); |
| 47 | + |
| 48 | + expect(shellMountSpy).toHaveBeenCalledTimes(1); |
| 49 | + |
| 50 | + rerender( |
| 51 | + <Lab levelId={29092} levelPropertiesMap={LEVEL_MAP}> |
| 52 | + <ShellSentinel /> |
| 53 | + <LevelDisplay /> |
| 54 | + </Lab>, |
| 55 | + ); |
| 56 | + |
| 57 | + const info = screen.getByTestId('level-info'); |
| 58 | + expect(JSON.parse(info.textContent!)).toEqual({ |
| 59 | + appName: 'fish', |
| 60 | + mode: 'short', |
| 61 | + }); |
| 62 | + // Shell re-rendered (React always calls render), but was not remounted |
| 63 | + // (no second mount lifecycle). Two calls = two renders, not two mounts. |
| 64 | + expect(shellMountSpy).toHaveBeenCalledTimes(2); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe('Lab error containment', () => { |
| 69 | + it('shows error state and reports via onError when child throws', () => { |
| 70 | + const reportSpy = vi.fn(); |
| 71 | + |
| 72 | + function Bomb(): never { |
| 73 | + throw new Error('boom'); |
| 74 | + } |
| 75 | + |
| 76 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 77 | + |
| 78 | + render( |
| 79 | + <Lab levelId={29091} levelPropertiesMap={LEVEL_MAP} onError={reportSpy}> |
| 80 | + <Bomb /> |
| 81 | + </Lab>, |
| 82 | + ); |
| 83 | + |
| 84 | + expect(screen.getByRole('alert')).toHaveTextContent( |
| 85 | + /error occurred.*reloading/i, |
| 86 | + ); |
| 87 | + expect(reportSpy).toHaveBeenCalledWith( |
| 88 | + expect.any(Error), |
| 89 | + expect.any(String), |
| 90 | + ); |
| 91 | + |
| 92 | + consoleSpy.mockRestore(); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('Lab without level properties (project route)', () => { |
| 97 | + it('renders children directly when no levelId or map provided', () => { |
| 98 | + render( |
| 99 | + <Lab> |
| 100 | + <div data-testid="lab-child">Oceans Lab Content</div> |
| 101 | + </Lab>, |
| 102 | + ); |
| 103 | + |
| 104 | + expect(screen.getByTestId('lab-child')).toHaveTextContent( |
| 105 | + 'Oceans Lab Content', |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('catches errors from children even without level properties', () => { |
| 110 | + const reportSpy = vi.fn(); |
| 111 | + |
| 112 | + function Bomb(): never { |
| 113 | + throw new Error('project crash'); |
| 114 | + } |
| 115 | + |
| 116 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 117 | + |
| 118 | + render( |
| 119 | + <Lab onError={reportSpy}> |
| 120 | + <Bomb /> |
| 121 | + </Lab>, |
| 122 | + ); |
| 123 | + |
| 124 | + expect(screen.getByRole('alert')).toHaveTextContent( |
| 125 | + /error occurred.*reloading/i, |
| 126 | + ); |
| 127 | + expect(reportSpy).toHaveBeenCalled(); |
| 128 | + |
| 129 | + consoleSpy.mockRestore(); |
| 130 | + }); |
| 131 | +}); |
0 commit comments