|
| 1 | +import { act, render, waitFor } from '../../test-utils'; |
| 2 | +import { KowlJsonView } from './KowlJsonView'; |
| 3 | + |
| 4 | +const { editorLayoutSpy, editorPropsSpy } = vi.hoisted(() => ({ |
| 5 | + editorLayoutSpy: vi.fn(), |
| 6 | + editorPropsSpy: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock('./KowlEditor', async () => { |
| 10 | + const React = await import('react'); |
| 11 | + |
| 12 | + return { |
| 13 | + __esModule: true, |
| 14 | + default: (props: any) => { |
| 15 | + editorPropsSpy(props); |
| 16 | + |
| 17 | + React.useEffect(() => { |
| 18 | + props.onMount?.({ |
| 19 | + layout: editorLayoutSpy, |
| 20 | + }); |
| 21 | + }, [props.onMount]); |
| 22 | + |
| 23 | + return <div data-testid="mock-kowl-editor">{props.value}</div>; |
| 24 | + }, |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +describe('KowlJsonView', () => { |
| 29 | + let originalResizeObserver: typeof ResizeObserver | undefined; |
| 30 | + let resizeCallback: ResizeObserverCallback | undefined; |
| 31 | + let currentSize = { width: 640, height: 384 }; |
| 32 | + let getBoundingClientRectSpy: ReturnType<typeof vi.spyOn>; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + editorLayoutSpy.mockReset(); |
| 36 | + editorPropsSpy.mockReset(); |
| 37 | + resizeCallback = undefined; |
| 38 | + currentSize = { width: 640, height: 384 }; |
| 39 | + |
| 40 | + vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => { |
| 41 | + callback(0); |
| 42 | + return 1; |
| 43 | + }); |
| 44 | + vi.stubGlobal('cancelAnimationFrame', vi.fn()); |
| 45 | + |
| 46 | + originalResizeObserver = globalThis.ResizeObserver; |
| 47 | + class ResizeObserverMock { |
| 48 | + observe = vi.fn(); |
| 49 | + disconnect = vi.fn(); |
| 50 | + |
| 51 | + constructor(callback: ResizeObserverCallback) { |
| 52 | + resizeCallback = callback; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + vi.stubGlobal('ResizeObserver', ResizeObserverMock); |
| 57 | + getBoundingClientRectSpy = vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(() => ({ |
| 58 | + width: currentSize.width, |
| 59 | + height: currentSize.height, |
| 60 | + top: 0, |
| 61 | + left: 0, |
| 62 | + right: currentSize.width, |
| 63 | + bottom: currentSize.height, |
| 64 | + x: 0, |
| 65 | + y: 0, |
| 66 | + toJSON: () => ({}), |
| 67 | + })); |
| 68 | + }); |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + vi.unstubAllGlobals(); |
| 72 | + getBoundingClientRectSpy.mockRestore(); |
| 73 | + |
| 74 | + if (originalResizeObserver) { |
| 75 | + globalThis.ResizeObserver = originalResizeObserver; |
| 76 | + } else { |
| 77 | + delete (globalThis as typeof globalThis & { ResizeObserver?: typeof ResizeObserver }).ResizeObserver; |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + test('uses the lightweight read-only monaco preset and manually lays out the editor', async () => { |
| 82 | + render(<KowlJsonView srcObj={{ market: 'BSEX' }} />); |
| 83 | + |
| 84 | + await waitFor(() => { |
| 85 | + expect(editorLayoutSpy).toHaveBeenCalledWith({ width: 640, height: 384 }); |
| 86 | + }); |
| 87 | + |
| 88 | + expect(editorPropsSpy).toHaveBeenCalled(); |
| 89 | + expect(editorPropsSpy.mock.lastCall?.[0].options).toMatchObject({ |
| 90 | + readOnly: true, |
| 91 | + domReadOnly: true, |
| 92 | + automaticLayout: false, |
| 93 | + folding: false, |
| 94 | + showFoldingControls: 'never', |
| 95 | + lineNumbers: 'off', |
| 96 | + renderLineHighlight: 'none', |
| 97 | + renderValidationDecorations: 'off', |
| 98 | + hover: { enabled: false }, |
| 99 | + links: false, |
| 100 | + matchBrackets: 'never', |
| 101 | + stickyScroll: { enabled: false }, |
| 102 | + guides: { |
| 103 | + indentation: false, |
| 104 | + highlightActiveIndentation: false, |
| 105 | + bracketPairs: false, |
| 106 | + bracketPairsHorizontal: false, |
| 107 | + highlightActiveBracketPair: false, |
| 108 | + }, |
| 109 | + unicodeHighlight: { |
| 110 | + ambiguousCharacters: false, |
| 111 | + invisibleCharacters: false, |
| 112 | + }, |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + test('relayouts only when the container size changes', async () => { |
| 117 | + render(<KowlJsonView srcObj={{ market: 'BSEX' }} />); |
| 118 | + |
| 119 | + await waitFor(() => { |
| 120 | + expect(editorLayoutSpy).toHaveBeenCalledTimes(1); |
| 121 | + }); |
| 122 | + |
| 123 | + act(() => { |
| 124 | + resizeCallback?.([] as ResizeObserverEntry[], {} as ResizeObserver); |
| 125 | + }); |
| 126 | + |
| 127 | + expect(editorLayoutSpy).toHaveBeenCalledTimes(1); |
| 128 | + |
| 129 | + currentSize = { width: 800, height: 480 }; |
| 130 | + |
| 131 | + act(() => { |
| 132 | + resizeCallback?.([] as ResizeObserverEntry[], {} as ResizeObserver); |
| 133 | + }); |
| 134 | + |
| 135 | + await waitFor(() => { |
| 136 | + expect(editorLayoutSpy).toHaveBeenCalledTimes(2); |
| 137 | + }); |
| 138 | + |
| 139 | + expect(editorLayoutSpy).toHaveBeenLastCalledWith({ width: 800, height: 480 }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments