Skip to content

The open alternative to Salesforce, designed for AI.

4.4 KB
Raw Blame
import '@/testing/setupServerRenderingGlobals';

import { act, type ReactElement } from 'react';
import { createRoot } from 'react-dom/client';

import { createCaretPreservingElement } from '../createCaretPreservingElement';

(
  globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;

const getProps = (element: ReactElement): Record<string, unknown> =>
  element.props as Record<string, unknown>;

describe('createCaretPreservingElement', () => {
  it('should create an element of the requested tag', () => {
    const element = createCaretPreservingElement({
      htmlTag: 'input',
      reactBindableProps: { type: 'text' },
      hostEnforcedProps: {},
      setEditableFocused: null,
      caretPreservingElementRef: () => {},
    });

    expect(element.type).toBe('input');
    expect(getProps(element).type).toBe('text');
  });

  it('should seed the initial value from value', () => {
    const element = createCaretPreservingElement({
      htmlTag: 'input',
      reactBindableProps: { value: 'hello' },
      hostEnforcedProps: {},
      setEditableFocused: null,
      caretPreservingElementRef: () => {},
    });

    expect(getProps(element).defaultValue).toBe('hello');
  });

  it('should seed the initial value from a numeric defaultValue', () => {
    const element = createCaretPreservingElement({
      htmlTag: 'input',
      reactBindableProps: { type: 'number', defaultValue: 42 },
      hostEnforcedProps: {},
      setEditableFocused: null,
      caretPreservingElementRef: () => {},
    });

    expect(getProps(element).defaultValue).toBe('42');
  });

  it('should seed the initial value from a numeric value', () => {
    const element = createCaretPreservingElement({
      htmlTag: 'input',
      reactBindableProps: { type: 'number', value: 42 },
      hostEnforcedProps: {},
      setEditableFocused: null,
      caretPreservingElementRef: () => {},
    });

    expect(getProps(element).defaultValue).toBe('42');
  });

  it('should prefer defaultValue over value for the initial value', () => {
    const element = createCaretPreservingElement({
      htmlTag: 'input',
      reactBindableProps: { value: 'v', defaultValue: 'd' },
      hostEnforcedProps: {},
      setEditableFocused: null,
      caretPreservingElementRef: () => {},
    });

    expect(getProps(element).defaultValue).toBe('d');
  });

  it('should apply host enforced props', () => {
    const element = createCaretPreservingElement({
      htmlTag: 'textarea',
      reactBindableProps: {},
      hostEnforcedProps: { readOnly: true },
      setEditableFocused: null,
      caretPreservingElementRef: () => {},
    });

    expect(getProps(element).readOnly).toBe(true);
  });

  it('should notify focus state and forward the original focus handler', () => {
    const setEditableFocused = jest.fn();
    const onFocus = jest.fn();
    const element = createCaretPreservingElement({
      htmlTag: 'input',
      reactBindableProps: { onFocus },
      hostEnforcedProps: {},
      setEditableFocused,
      caretPreservingElementRef: () => {},
    });

    const event = {} as never;
    (getProps(element).onFocus as (event: unknown) => void)(event);

    expect(setEditableFocused).toHaveBeenCalledWith(true);
    expect(onFocus).toHaveBeenCalledWith(event);
  });

  it('should notify blur state', () => {
    const setEditableFocused = jest.fn();
    const element = createCaretPreservingElement({
      htmlTag: 'input',
      reactBindableProps: {},
      hostEnforcedProps: {},
      setEditableFocused,
      caretPreservingElementRef: () => {},
    });

    (getProps(element).onBlur as (event: unknown) => void)({} as never);

    expect(setEditableFocused).toHaveBeenCalledWith(false);
  });

  it('should forward the caret preserving ref to the rendered element', () => {
    const caretPreservingElementRef = jest.fn();
    const element = createCaretPreservingElement({
      htmlTag: 'input',
      reactBindableProps: {},
      hostEnforcedProps: {},
      setEditableFocused: null,
      caretPreservingElementRef,
    });

    const container = document.createElement('div');
    document.body.appendChild(container);
    const root = createRoot(container);

    act(() => {
      root.render(element);
    });

    expect(caretPreservingElementRef).toHaveBeenCalledWith(
      container.firstElementChild,
    );

    act(() => {
      root.unmount();
    });
    container.remove();
  });
});