Skip to content

The open alternative to Salesforce, designed for AI.

1.9 KB
Raw Blame
import { expect, userEvent, waitFor, within } from 'storybook/test';

import { errorHandler } from '@/__stories__/shared/test-utils/createFrontComponentStoryMeta';
import { expectElementWithinScrollport } from '@/__stories__/shared/test-utils/matchers/expectElementWithinScrollport';
import { expectFrontComponentMounted } from '@/__stories__/shared/test-utils/matchers/expectFrontComponentMounted';
import { INTERACTION_TIMEOUT } from '@/__stories__/shared/test-utils/timeouts';
import { type TwentyUiGalleryPlayFunction } from '@/__stories__/twenty-ui-gallery/types/TwentyUiGalleryPlayFunction';
import { expectScrollPositionStable } from '@/__stories__/twenty-ui-gallery/utils/expectScrollPositionStable';
import { findScrollContainerOrThrow } from '@/__stories__/twenty-ui-gallery/utils/findScrollContainerOrThrow';
import { openSelectListbox } from '@/__stories__/twenty-ui-gallery/utils/openSelectListbox';

const HOST_SCROLL_POSITIONS = [0, 240, 120];

export const selectAlignedOpenTest: TwentyUiGalleryPlayFunction = async ({
  canvasElement,
}) => {
  const canvas = within(canvasElement);
  const body = within(canvasElement.ownerDocument.body);
  await expectFrontComponentMounted(canvas);

  await openSelectListbox({ canvas, body, selectName: 'Aligned option' });

  const selectedOption = body.getByRole('option', { name: 'Option 30' });
  const scrollContainer = findScrollContainerOrThrow(selectedOption);

  await waitFor(
    () => {
      expect(scrollContainer.scrollTop).toBeGreaterThan(0);
      expectElementWithinScrollport({
        element: selectedOption,
        scrollContainer,
      });
    },
    { timeout: INTERACTION_TIMEOUT },
  );
  await expectScrollPositionStable(scrollContainer);

  for (const hostScrollPosition of HOST_SCROLL_POSITIONS) {
    scrollContainer.scrollTop = hostScrollPosition;
    await expectScrollPositionStable(scrollContainer);
  }

  await userEvent.keyboard('{Escape}');
  await expect(errorHandler).not.toHaveBeenCalled();
};