Skip to content

The open alternative to Salesforce, designed for AI.

2.1 KB
Raw Blame
import { WELCOME_TITLE_HANDOFF_TARGET_ELEMENT_ID } from '@/onboarding/constants/WelcomeTitleHandoffTargetElementId';
import { WELCOME_TITLE_SOURCE_ELEMENT_ID } from '@/onboarding/constants/WelcomeTitleSourceElementId';
import { measureWelcomeTitleFlight } from '@/onboarding/utils/measureWelcomeTitleFlight';

const buildSourceElement = () => {
  const source = document.createElement('div');
  source.id = WELCOME_TITLE_SOURCE_ELEMENT_ID;
  source.getBoundingClientRect = () =>
    ({ left: 100, top: 200, width: 400, height: 80 }) as DOMRect;
  document.body.appendChild(source);
  return source;
};

const buildTargetElement = (rect: Partial<DOMRect>, visibility = 'visible') => {
  const target = document.createElement('span');
  target.id = WELCOME_TITLE_HANDOFF_TARGET_ELEMENT_ID;
  target.style.visibility = visibility;
  target.getBoundingClientRect = () =>
    ({ left: 24, top: 40, width: 200, height: 20, ...rect }) as DOMRect;
  document.body.appendChild(target);
  return target;
};

describe('measureWelcomeTitleFlight', () => {
  afterEach(() => {
    document.body.innerHTML = '';
  });

  it('should return null when the source element is missing', () => {
    buildTargetElement({});

    expect(measureWelcomeTitleFlight()).toBeNull();
  });

  it('should return null when the target element is missing', () => {
    buildSourceElement();

    expect(measureWelcomeTitleFlight()).toBeNull();
  });

  it('should return null when the target has no width', () => {
    buildSourceElement();
    buildTargetElement({ width: 0 });

    expect(measureWelcomeTitleFlight()).toBeNull();
  });

  it('should return null when the target has no height', () => {
    buildSourceElement();
    buildTargetElement({ height: 0 });

    expect(measureWelcomeTitleFlight()).toBeNull();
  });

  it('should return null when the target is not visible', () => {
    buildSourceElement();
    buildTargetElement({}, 'hidden');

    expect(measureWelcomeTitleFlight()).toBeNull();
  });

  it('should measure a flight when both ends are laid out and visible', () => {
    buildSourceElement();
    buildTargetElement({});

    expect(measureWelcomeTitleFlight()).not.toBeNull();
  });
});