Files
pterodactyl-panel/resources/scripts/components/elements/CodemirrorEditor.spec.tsx
T
Sandip Ghimire 0d3ce311eb feat(editor): align CodeMirror search widget with panel theme and fix replace toggle (#7279)
The default CodeMirror search & replace bar in the File Editor was
unstyled, pushed the editor code down when opened, used mismatched
fonts/colors, and had a broken Replace collapse toggle.

### Changes
- Replaced the default search panel with a compact, floating top-right
widget (VS Code style) that doesn't shift editor content.
- Applied panel theme variables for backgrounds, inputs
(`var(--input)`), borders (`var(--border)`), primary buttons
(`var(--primary)`), and a danger close button (`var(--destructive)`).
- Set all widget inputs and buttons to use the theme font
(`var(--font-sans)`).
- Change the Replace section so it stays hidden by default and properly
collapses/expands on chevron click or `Ctrl+H`.
2026-10-11 17:49:53 -04:00

80 lines
3.1 KiB
TypeScript

/** @vitest-environment jsdom */
import { act, cleanup, render } from '@testing-library/react';
import { createRef } from 'react';
import { EditorView } from '@codemirror/view';
import { afterEach, expect, it, vi } from 'vitest';
import CodemirrorEditor, { type CodemirrorEditorHandle } from './CodemirrorEditor';
afterEach(cleanup);
const viewOf = (container: HTMLElement) => {
const view = EditorView.findFromDOM(container.querySelector<HTMLElement>('.cm-editor')!);
if (!view) {
throw new Error('The editor view is not mounted.');
}
return view;
};
it('mounts the editor as the direct child of the sized container', () => {
const { container } = render(
<CodemirrorEditor initialContent='alpha' mode='text/plain' onContentSaved={vi.fn()} />
);
const box = container.firstElementChild!;
expect(box.className).toContain('overflow-hidden');
expect(box.children).toHaveLength(1);
expect(box.firstElementChild).toHaveClass('cm-editor');
expect(box.querySelector('.cm-content')).not.toHaveAttribute('spellcheck', 'true');
});
it('keeps the view, its edits and history when initialContent changes', () => {
const ref = createRef<CodemirrorEditorHandle>();
const onContentChanged = vi.fn();
const props = { ref, mode: 'text/plain', onContentSaved: vi.fn(), onContentChanged };
const { container, rerender } = render(<CodemirrorEditor {...props} initialContent='alpha' />);
const view = viewOf(container);
act(() => view.dispatch({ changes: { from: 5, insert: ' beta' } }));
expect(onContentChanged).toHaveBeenLastCalledWith('alpha beta');
rerender(<CodemirrorEditor {...props} initialContent='saved on the server' />);
expect(viewOf(container)).toBe(view);
expect(ref.current?.getValue()).toBe('alpha beta');
});
it('runs the latest save handler for Mod-s', () => {
const first = vi.fn();
const latest = vi.fn();
const { container, rerender } = render(<CodemirrorEditor mode='text/plain' onContentSaved={first} />);
rerender(<CodemirrorEditor mode='text/plain' onContentSaved={latest} />);
container
.querySelector('.cm-content')!
.dispatchEvent(new KeyboardEvent('keydown', { key: 's', ctrlKey: true, bubbles: true }));
expect(first).not.toHaveBeenCalled();
expect(latest).toHaveBeenCalledOnce();
});
it('renders the search and replace panel with theme controls', () => {
const { container } = render(<CodemirrorEditor mode='text/plain' onContentSaved={vi.fn()} />);
const view = viewOf(container);
container
.querySelector('.cm-content')!
.dispatchEvent(new KeyboardEvent('keydown', { key: 'f', ctrlKey: true, bubbles: true }));
const searchPanel = container.querySelector('.cm-search');
expect(searchPanel).not.toBeNull();
expect(searchPanel?.querySelector('input[name="search"]')).not.toBeNull();
expect(searchPanel?.querySelector('input[name="replace"]')).not.toBeNull();
expect(searchPanel?.querySelector('button[name="close"]')).not.toBeNull();
expect(searchPanel?.querySelectorAll('.cm-button').length).toBeGreaterThanOrEqual(4);
});