Skip to content

The open alternative to Salesforce, designed for AI.

4.5 KB
Raw Blame
import { type TwentyUiGalleryPlayFunction } from '@/__stories__/twenty-ui-gallery/types/TwentyUiGalleryPlayFunction';
import { expect, userEvent, waitFor, within } from 'storybook/test';

export const buttonControlsTest: TwentyUiGalleryPlayFunction = async (
  context,
) => {
  const canvas = within(context.canvasElement);
  const editActions = await canvas.findByRole(
    'button',
    { name: 'Edit actions' },
    { timeout: 10000 },
  );
  await waitFor(() =>
    expect(editActions.getBoundingClientRect().height).toBe(24),
  );
  await expect(editActions).toHaveAttribute('aria-expanded', 'false');
  editActions.focus();
  await userEvent.keyboard('{Enter}');
  await waitFor(() =>
    expect(editActions).toHaveAttribute('aria-expanded', 'true'),
  );
  await userEvent.keyboard(' ');
  await waitFor(() =>
    expect(editActions).toHaveAttribute('aria-expanded', 'false'),
  );
  const button = await canvas.findByRole(
    'button',
    { name: 'Create record' },
    { timeout: 10000 },
  );
  await userEvent.click(button);
  button.focus();
  await userEvent.keyboard('{Enter} ');
  await waitFor(() =>
    expect(canvas.getByLabelText('Activations')).toHaveTextContent('3'),
  );
  const inheritedButton = canvas.getByRole('button', {
    name: 'Inherited action',
  });
  const inheritedColor = getComputedStyle(
    canvas.getByTestId('inherited-button-surface'),
  ).color;

  await expect(getComputedStyle(inheritedButton).color).toBe(inheritedColor);
  await expect(inheritedButton.querySelector('svg')).toHaveStyle({
    color: inheritedColor,
  });
  const disabled = canvas.getByRole('button', { name: 'Disabled button' });
  await expect(disabled).toBeDisabled();
  await userEvent.click(disabled);
  await waitFor(() =>
    expect(canvas.getByLabelText('Activations')).toHaveTextContent('3'),
  );
  const save = canvas.getByRole('button', { name: 'Save changes' });
  await expect(getComputedStyle(save).fontWeight).toBe('600');
  await expect(getComputedStyle(save).paddingInlineStart).toBe('12px');
  await userEvent.click(save);
  await waitFor(() => expect(save).toBeDisabled());
  const complete = canvas.getByRole('button', { name: 'Complete request' });
  await expect(getComputedStyle(complete).fontWeight).toBe('400');
  await expect(complete.getBoundingClientRect().height).toBe(24);
  await userEvent.click(complete);
  await waitFor(() => expect(save).toBeEnabled());
  await expect(
    canvas.getByRole('link', { name: 'Documentation' }),
  ).toHaveAttribute('href', 'https://twenty.com');
  await userEvent.click(canvas.getByRole('button', { name: 'Second action' }));
  await waitFor(() =>
    expect(canvas.getByLabelText('Activations')).toHaveTextContent('4'),
  );
  const addItem = canvas.getByRole('button', { name: 'Add item' });
  await userEvent.click(addItem);
  await waitFor(() =>
    expect(canvas.getByLabelText('Activations')).toHaveTextContent('5'),
  );
  const unavailableItem = canvas.getByRole('button', {
    name: 'Unavailable item',
  });
  await expect(unavailableItem).toBeDisabled();
  await userEvent.click(unavailableItem);
  await waitFor(() =>
    expect(canvas.getByLabelText('Activations')).toHaveTextContent('5'),
  );
  await expect(addItem.getBoundingClientRect().width).toBe(20);
  await expect(
    unavailableItem.getBoundingClientRect().left -
      addItem.getBoundingClientRect().right,
  ).toBe(2);
  const send = canvas.getByRole('button', { name: 'Send' });
  await expect(send.getBoundingClientRect().width).toBe(20);
  await expect(getComputedStyle(send).borderTopLeftRadius).toBe('50%');

  await userEvent.click(canvas.getByRole('button', { name: 'Add to record' }));
  const draggableRecordAction = canvas.getByRole('button', {
    name: 'Add to draggable record',
  });
  await userEvent.click(draggableRecordAction);
  draggableRecordAction.focus();
  await userEvent.keyboard('{Enter} ');
  const unavailableRecordButton = canvas.getByRole('button', {
    name: 'Unavailable record button',
  });
  await expect(unavailableRecordButton).toBeDisabled();
  await userEvent.click(unavailableRecordButton);
  await waitFor(() =>
    expect(canvas.getByLabelText('Activations')).toHaveTextContent('9'),
  );
  const logOut = canvas.getByRole('button', { name: 'Log out' });
  await userEvent.click(logOut);
  logOut.focus();
  await userEvent.keyboard('{Enter} ');
  await waitFor(() =>
    expect(canvas.getByLabelText('Activations')).toHaveTextContent('12'),
  );
  await expect(getComputedStyle(logOut).borderTopWidth).toBe('0px');
  await expect(getComputedStyle(logOut).paddingInlineStart).toBe('4px');
};