Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

3.6 KB
Raw Blame
/**
 * @vitest-environment jsdom
 */

import {
  cleanup,
  fireEvent,
  render,
  screen,
  waitFor,
} from '@solidjs/testing-library';
import { type JSX, Show } from 'solid-js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { NewCallButton } from './NewCallButton';

const mocks = vi.hoisted(() => ({
  joinChannelCall: vi.fn<(_: string) => Promise<void>>(),
  navigate: vi.fn<(_: string) => void>(),
}));

vi.mock('@channel/Call/join-channel-call', () => ({
  joinChannelCall: mocks.joinChannelCall,
}));

vi.mock('@solidjs/router', () => ({ useNavigate: () => mocks.navigate }));

vi.mock('@channel/Call/NewMeetingButton', () => ({
  NewMeetingButton: (props: {
    onChannelCall: () => void;
    onInstantCall: () => void;
  }) => (
    <>
      <button type="button" onClick={props.onChannelCall}>
        New channel call
      </button>
      <button type="button" onClick={props.onInstantCall}>
        New instant call
      </button>
    </>
  ),
}));

vi.mock('@core/component/RecipientSelector', () => ({
  RecipientSelector: (props: {
    setSelectedOptions: (options: Array<{ id: string }>) => void;
  }) => (
    <button
      type="button"
      onClick={() => props.setSelectedOptions([{ id: 'channel-option' }])}
    >
      Choose channel
    </button>
  ),
}));

vi.mock('@core/component/Toast/Toast', () => ({
  toast: { failure: vi.fn() },
}));

vi.mock('@core/signal/useCombinedRecipient', () => ({
  useCombinedRecipients: () => ({ all: () => [] }),
}));

vi.mock('@core/util/destination', () => ({
  getDestinationFromOptions: () => ({
    type: 'channel',
    id: 'channel-123',
  }),
}));

vi.mock('@queries/channel/get-or-create-dm', () => ({
  useGetOrCreateDirectMessageMutation: () => ({ mutateAsync: vi.fn() }),
  useGetOrCreatePrivateChannelMutation: () => ({ mutateAsync: vi.fn() }),
}));

vi.mock('@ui', () => {
  type ChildrenProps = { children?: JSX.Element };
  type ButtonProps = ChildrenProps & {
    disabled?: boolean;
    onClick?: JSX.EventHandlerUnion<HTMLButtonElement, MouseEvent>;
  };

  const Button = (props: ButtonProps) => (
    <button disabled={props.disabled} onClick={props.onClick} type="button">
      {props.children}
    </button>
  );
  const Dialog = (props: ChildrenProps & { open: boolean }) => (
    <Show when={props.open}>{props.children}</Show>
  );
  Dialog.CloseButton = Button;
  Dialog.Title = (props: ChildrenProps) => <span>{props.children}</span>;

  return {
    Button,
    Dialog,
    Surface: (props: ChildrenProps) => <div>{props.children}</div>,
  };
});

beforeEach(() => {
  mocks.joinChannelCall.mockReset();
  mocks.joinChannelCall.mockResolvedValue();
  mocks.navigate.mockReset();
});
afterEach(cleanup);

describe('NewCallButton', () => {
  it('opens instant call setup without creating a channel call', () => {
    render(() => <NewCallButton />);
    fireEvent.click(screen.getByRole('button', { name: 'New instant call' }));

    expect(mocks.navigate).toHaveBeenCalledExactlyOnceWith('/meet/new');
    expect(mocks.joinChannelCall).not.toHaveBeenCalled();
    expect(screen.queryByRole('button', { name: 'Start Call' })).toBeNull();
  });

  it('starts the selected channel through the mounted-channel call flow', async () => {
    render(() => <NewCallButton />);
    fireEvent.click(screen.getByRole('button', { name: 'New channel call' }));
    fireEvent.click(screen.getByRole('button', { name: 'Choose channel' }));
    fireEvent.click(screen.getByRole('button', { name: 'Start Call' }));

    await waitFor(() => {
      expect(mocks.joinChannelCall).toHaveBeenCalledOnce();
      expect(mocks.joinChannelCall).toHaveBeenCalledWith('channel-123');
    });
  });
});