import { describe, expect, it, jest } from '@jest/globals'; import { render } from '@testing-library/react-native'; import { createRef } from 'react'; import { CodeEditor } from '../CodeEditor'; import type { CodeEditorViewProps } from '../specs/CodeEditorView.nitro'; import { githubLight } from '../themes/githubLight'; import type { CodeEditorProps, CodeEditorRef, Decoration } from '../types'; jest.mock('react-native-nitro-modules', () => ({ getHostComponent: () => 'CodeEditorView', callback: (f: unknown) => ({ f }), NitroModules: { createHybridObject: () => ({ availableLanguages: () => [] }) }, })); // The mocked callback() wraps every function prop as { f }, so the host props follow the Nitro spec in that shape. type Wrapped = T extends (...args: infer A) => infer R ? { f: (...args: A) => R } : T; type HostProps = { [K in keyof CodeEditorViewProps]-?: Wrapped>; } & { hybridRef: { f: (ref: unknown) => void } }; type Rendered = Awaited>; // The wrapper renders exactly one host element, so the root is the native view. const host = (api: Rendered) => { if (!api.root) throw new Error('no host element rendered'); return api.root.props as HostProps; }; describe('CodeEditor', () => { it('wraps every callback once and reads the latest handler', async () => { const rows: [keyof HostProps, (h: jest.Mock) => CodeEditorProps, unknown][] = [ ['onReady', (h) => ({ onReady: h }), undefined], [ 'onScroll', (h) => ({ onScroll: h }), { x: 0, y: 12, contentWidth: 100, contentHeight: 2000 }, ], ['onSelectionChange', (h) => ({ onSelectionChange: h }), { anchor: 1, head: 4, revision: 2 }], [ 'onChange', (h) => ({ onChange: h }), { start: 0, end: 0, text: 'a', origin: 'input', revision: 1 }, ], ['onChangeText', (h) => ({ onChangeText: h }), 'a'], ['onEditorFocus', (h) => ({ onFocus: h }), undefined], ['onEditorBlur', (h) => ({ onBlur: h }), undefined], ['onContentSizeChange', (h) => ({ onContentSizeChange: h }), { width: 360, height: 1200 }], ]; for (const [hostProp, props, payload] of rows) { const first = jest.fn(); const second = jest.fn(); const api = await render(); const before = host(api); await api.rerender(); const after = host(api); expect(after[hostProp]).toBe(before[hostProp]); expect(after.hybridRef).toBe(before.hybridRef); (after[hostProp] as { f: (p?: unknown) => void }).f(payload); expect(first).not.toHaveBeenCalled(); if (payload === undefined) expect(second).toHaveBeenCalledTimes(1); else expect(second).toHaveBeenCalledWith(payload); } }); it('answers reads before the native object exists and forwards afterwards', async () => { const ref = createRef(); const props = host(await render()); expect(ref.current?.getText()).toBe(''); expect(ref.current?.getLineCount()).toBe(1); const setText = jest.fn(() => Promise.resolve()); props.hybridRef.f({ getText: () => 'abc', getLength: () => 3, getLineCount: () => 1, getLine: () => 'abc', getRevision: () => 0, setText, }); expect(ref.current?.getText()).toBe('abc'); await ref.current?.setText('x'); expect(setText).toHaveBeenCalledWith('x', false); await ref.current?.setText('y', { keepHistory: true }); expect(setText).toHaveBeenCalledWith('y', true); }); it('normalizes theme tokens into structs', async () => { const theme = { ...githubLight, tokens: { keyword: '#f00', comment: { color: '#0f0', italic: true } }, }; const props = host(await render()); expect(props.theme.tokens).toEqual({ keyword: { color: '#f00', bold: false, italic: false }, comment: { color: '#0f0', bold: false, italic: true }, }); }); it('fills the prop defaults and passes explicit values through', async () => { const api = await render(); const props = host(api); expect(props).toMatchObject({ initialValue: '', language: '', fontFamily: '', fontSize: 14, lineHeight: 1.4, lineNumbers: true, softWrap: false, readOnly: false, highlightActiveLine: true, matchBrackets: true, placeholder: '', scrollEnabled: true, textPadding: 8, avoidKeyboard: false, tabSize: 4, insertSpaces: true, autoIndent: true, autoCloseBrackets: true, suggestions: 'off', autoFocus: false, }); // A removed prop would reach Nitro as null, which it rejects; the insets keep one identity across renders. expect(props.contentInsets).toEqual({}); await api.rerender(); expect(host(api).contentInsets).toBe(props.contentInsets); expect(props.onChangeText).toBeUndefined(); const custom = host( await render( , ), ); expect(custom).toMatchObject({ softWrap: true, matchBrackets: false, avoidKeyboard: true, placeholder: 'Type here', contentInsets: { bottom: 40 }, textPadding: 0, }); }); it('keeps onChangeText on the native view after the handler goes away', async () => { const api = await render( {}} />); const wired = host(api).onChangeText; expect(wired).toBeDefined(); await api.rerender(); expect(host(api).onChangeText).toBe(wired); }); it('maps the selection and scroll methods onto the native object', async () => { const ref = createRef(); const props = host(await render()); expect(ref.current?.getSelection()).toEqual({ anchor: 0, head: 0 }); expect(ref.current?.getCursorRect()).toBeNull(); expect(ref.current?.getVisibleLines()).toEqual({ first: 0, last: 0 }); const native = { getText: () => '', getLength: () => 0, getLineCount: () => 1, getLine: () => '', getRevision: () => 0, setText: jest.fn(() => Promise.resolve()), getSelection: () => ({ anchor: 2, head: 5, revision: 7 }), getTextInRange: jest.fn(() => 'bc'), offsetToPosition: () => ({ line: 1, column: 2 }), positionToOffset: jest.fn(() => 9), getCursorRect: () => undefined, getVisibleLines: () => ({ first: 3, last: 30 }), setSelection: jest.fn(() => Promise.resolve()), selectAll: jest.fn(() => Promise.resolve()), scrollToLine: jest.fn(() => Promise.resolve()), revealSelection: jest.fn(() => Promise.resolve()), }; props.hybridRef.f(native); expect(ref.current?.getSelection()).toEqual({ anchor: 2, head: 5 }); expect(ref.current?.getTextInRange({ start: 1, end: 3 })).toBe('bc'); expect(native.getTextInRange).toHaveBeenCalledWith(1, 3); expect(ref.current?.offsetToPosition(5)).toEqual({ line: 1, column: 2 }); expect(ref.current?.positionToOffset({ line: 1, column: 2 })).toBe(9); expect(native.positionToOffset).toHaveBeenCalledWith(1, 2); expect(ref.current?.getCursorRect()).toBeNull(); expect(ref.current?.getVisibleLines()).toEqual({ first: 3, last: 30 }); await ref.current?.setSelection({ anchor: 1, head: 3 }); expect(native.setSelection).toHaveBeenCalledWith(1, 3); await ref.current?.scrollToLine(5); expect(native.scrollToLine).toHaveBeenCalledWith(5, false); await ref.current?.scrollToLine(6, { animated: true }); expect(native.scrollToLine).toHaveBeenCalledWith(6, true); await ref.current?.selectAll(); await ref.current?.revealSelection(); expect(native.selectAll).toHaveBeenCalledTimes(1); expect(native.revealSelection).toHaveBeenCalledTimes(1); }); it('maps the editing methods onto the native object', async () => { const ref = createRef(); const props = host(await render()); expect(ref.current?.canUndo()).toBe(false); expect(ref.current?.isFocused()).toBe(false); await ref.current?.undo(); const native = { canUndo: () => true, canRedo: () => false, isFocused: () => true, replaceRange: jest.fn(() => Promise.resolve()), insertText: jest.fn(() => Promise.resolve()), undo: jest.fn(() => Promise.resolve()), redo: jest.fn(() => Promise.resolve()), indent: jest.fn(() => Promise.resolve()), outdent: jest.fn(() => Promise.resolve()), cut: jest.fn(() => Promise.resolve()), copy: jest.fn(() => Promise.resolve()), paste: jest.fn(() => Promise.resolve()), focus: jest.fn(() => Promise.resolve()), blur: jest.fn(() => Promise.resolve()), }; props.hybridRef.f(native); expect(ref.current?.canUndo()).toBe(true); expect(ref.current?.canRedo()).toBe(false); expect(ref.current?.isFocused()).toBe(true); await ref.current?.replaceRange({ start: 1, end: 3 }, 'xy'); expect(native.replaceRange).toHaveBeenCalledWith(1, 3, 'xy'); await ref.current?.insertText('q'); expect(native.insertText).toHaveBeenCalledWith('q'); await ref.current?.undo(); await ref.current?.redo(); await ref.current?.indent(); await ref.current?.outdent(); await ref.current?.cut(); await ref.current?.copy(); await ref.current?.paste(); await ref.current?.focus(); await ref.current?.blur(); for (const fn of [ native.undo, native.redo, native.indent, native.outdent, native.cut, native.copy, native.paste, native.focus, native.blur, ]) { expect(fn).toHaveBeenCalledTimes(1); } }); it('maps the search methods onto the native object', async () => { const ref = createRef(); const props = host(await render()); expect(await ref.current?.setSearch({ text: 'ab' })).toEqual({ count: 0, limited: false }); expect(await ref.current?.findNext()).toBeNull(); expect(await ref.current?.findPrevious()).toBeNull(); expect(await ref.current?.replaceCurrent('x')).toBe(false); expect(await ref.current?.replaceAll('x')).toBe(0); await ref.current?.clearSearch(); const native = { setSearch: jest.fn(() => Promise.resolve({ count: 3, limited: true })), findNext: jest.fn(() => Promise.resolve({ index: -1, total: 0 })), findPrevious: jest.fn(() => Promise.resolve({ index: 1, total: 3 })), replaceCurrent: jest.fn(() => Promise.resolve(true)), replaceAll: jest.fn(() => Promise.resolve(2)), clearSearch: jest.fn(() => Promise.resolve()), }; props.hybridRef.f(native); expect(await ref.current?.setSearch({ text: 'ab' })).toEqual({ count: 3, limited: true }); expect(native.setSearch).toHaveBeenCalledWith({ text: 'ab', regex: false, caseSensitive: false, wholeWord: false, }); await ref.current?.setSearch({ text: 'a.', regex: true, caseSensitive: true, wholeWord: true }); expect(native.setSearch).toHaveBeenLastCalledWith({ text: 'a.', regex: true, caseSensitive: true, wholeWord: true, }); // Nitro cannot cross null inside a struct, so a total of 0 is the no-match sentinel. expect(await ref.current?.findNext()).toBeNull(); expect(await ref.current?.findPrevious()).toEqual({ index: 1, total: 3 }); expect(await ref.current?.replaceCurrent('x')).toBe(true); expect(native.replaceCurrent).toHaveBeenCalledWith('x'); expect(await ref.current?.replaceAll('y')).toBe(2); expect(native.replaceAll).toHaveBeenCalledWith('y'); await ref.current?.clearSearch(); expect(native.clearSearch).toHaveBeenCalledTimes(1); }); it('maps the caret and typing methods onto the native object', async () => { const ref = createRef(); const props = host(await render()); await ref.current?.moveCaret('left'); await ref.current?.typeText('a'); await ref.current?.deleteBackward(); await ref.current?.deleteForward(); const native = { moveCaret: jest.fn(() => Promise.resolve()), typeText: jest.fn(() => Promise.resolve()), deleteBackward: jest.fn(() => Promise.resolve()), deleteForward: jest.fn(() => Promise.resolve()), }; props.hybridRef.f(native); await ref.current?.moveCaret('up'); expect(native.moveCaret).toHaveBeenCalledWith('up', false); await ref.current?.moveCaret('wordRight', { select: true }); expect(native.moveCaret).toHaveBeenLastCalledWith('wordRight', true); await ref.current?.typeText('{'); expect(native.typeText).toHaveBeenCalledWith('{'); await ref.current?.deleteBackward(); await ref.current?.deleteForward(); expect(native.deleteBackward).toHaveBeenCalledTimes(1); expect(native.deleteForward).toHaveBeenCalledTimes(1); }); it('passes decorations as flat structs with defaults', async () => { const decorations: Decoration[] = [ { kind: 'underline', range: { start: 10, end: 14 }, color: '#C8322B' }, { kind: 'underline', range: { start: 20, end: 24 }, color: '#B8730E', style: 'wavy', thickness: 1.5, offset: 4, }, { kind: 'gap', line: 6, rows: 2 }, { kind: 'lineBackground', line: 6, color: '#FAE8E6' }, ]; const props = host( await render(), ); const neutral = { start: -1, end: -1, line: -1, rows: 0, color: '', style: 'solid', thickness: 0, offset: 0, }; expect(props.decorations).toEqual([ { ...neutral, kind: 'underline', start: 10, end: 14, color: '#C8322B', thickness: 1, offset: 2, }, { ...neutral, kind: 'underline', start: 20, end: 24, color: '#B8730E', style: 'wavy', thickness: 1.5, offset: 4, }, { ...neutral, kind: 'gap', line: 6, rows: 2 }, { ...neutral, kind: 'lineBackground', line: 6, color: '#FAE8E6' }, ]); expect(props.decorationsRevision).toBe(7); }); it('keeps one decorations array across renders', async () => { const decorations: Decoration[] = [{ kind: 'gap', line: 1, rows: 2 }]; const api = await render(); const first = host(api).decorations; await api.rerender(); expect(host(api).decorations).toBe(first); const empty = host(await render()); const other = host(await render()); expect(empty.decorations).toEqual([]); expect(empty.decorations).toBe(other.decorations); expect(empty.decorationsRevision).toBe(-1); }); it('clears removed decorations for the current text, whatever decorationsRevision says', async () => { const decorations: Decoration[] = [{ kind: 'gap', line: 1, rows: 2 }]; const api = await render(); await api.rerender(); expect(host(api).decorations).toEqual([]); expect(host(api).decorationsRevision).toBe(-1); }); it('wires onGapLayout once a handler was set', async () => { const api = await render(); expect(host(api).onGapLayout).toBeUndefined(); const handler = jest.fn(); await api.rerender(); const wired = host(api).onGapLayout; expect(wired).toBeDefined(); await api.rerender(); expect(host(api).onGapLayout).toBe(wired); const latest = jest.fn(); await api.rerender(); const gaps = [{ line: 2, rect: { x: 0, y: 44, width: 300, height: 44 } }]; host(api).onGapLayout.f(gaps); expect(latest).toHaveBeenCalledWith({ gaps }); expect(handler).not.toHaveBeenCalled(); }); it('getGapRect returns null for no gap', async () => { const ref = createRef(); const props = host(await render()); expect(ref.current?.getGapRect(2)).toBeNull(); const rect = { x: 0, y: 44, width: 300, height: 44 }; props.hybridRef.f({ getGapRect: (line: number) => (line === 2 ? rect : undefined) }); expect(ref.current?.getGapRect(1)).toBeNull(); expect(ref.current?.getGapRect(2)).toEqual(rect); }); });