Skip to content

The open alternative to Salesforce, designed for AI.

11.4 KB
Raw Blame
import { i18n } from '@lingui/core';
import { I18nProvider } from '@lingui/react';
import { act, renderHook, waitFor } from '@testing-library/react';
import { Provider as JotaiProvider } from 'jotai';
import { SOURCE_LOCALE } from 'twenty-shared/translations';
import { dynamicActivate } from '~/utils/i18n/dynamicActivate';

import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { isBookCallOnboardingStepEnabledState } from '@/client-config/states/isBookCallOnboardingStepEnabledState';
import { isCompanyEnrichmentEnabledState } from '@/client-config/states/isCompanyEnrichmentEnabledState';
import { onboardingConfigState } from '@/client-config/states/onboardingConfigState';
import { useInviteTeam } from '@/onboarding/hooks/useInviteTeam';
import { onboardingFreeCreditsFamilyState } from '@/onboarding/states/onboardingFreeCreditsFamilyState';
import { onboardingInviteTeamEmailsDraftState } from '@/onboarding/states/onboardingInviteTeamEmailsDraftState';
import {
  jotaiStore,
  resetJotaiStore,
} from '@/ui/utilities/state/jotai/jotaiStore';
import { mockCurrentWorkspace } from '~/testing/mock-data/users';

const mockSendInvitation = jest.fn();
const mockSetNextOnboardingStatus = jest.fn();
const mockWaitForCompanyEnrichmentSettlement = jest.fn();

jest.mock('@/workspace-invitation/hooks/useCreateWorkspaceInvitation', () => ({
  useCreateWorkspaceInvitation: () => ({
    sendInvitation: mockSendInvitation,
  }),
}));

jest.mock('@/onboarding/hooks/useSetNextOnboardingStatus', () => ({
  useSetNextOnboardingStatus: () => mockSetNextOnboardingStatus,
}));

jest.mock('@/onboarding/utils/waitForCompanyEnrichmentSettlement', () => ({
  waitForCompanyEnrichmentSettlement: (...args: unknown[]) =>
    mockWaitForCompanyEnrichmentSettlement(...args),
}));

jest.mock('@apollo/client/react', () => ({
  useQuery: () => ({ data: undefined, loading: false }),
}));

const mockEnqueueToast = jest.fn();

jest.mock('twenty-ui/components/feedback', () => ({
  ...jest.requireActual('twenty-ui/components/feedback'),
  useToast: () => ({ enqueueToast: mockEnqueueToast }),
}));

jest.mock('@/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement', () => ({
  useHotkeysOnFocusedElement: jest.fn(),
}));

dynamicActivate(SOURCE_LOCALE);

const renderInviteTeam = () =>
  renderHook(() => useInviteTeam(), {
    wrapper: ({ children }) =>
      JotaiProvider({
        store: jotaiStore,
        children: I18nProvider({ i18n, children }),
      }),
  });

describe('useInviteTeam', () => {
  beforeEach(() => {
    localStorage.clear();
    sessionStorage.clear();
    resetJotaiStore();
    jotaiStore.set(currentWorkspaceState.atom, mockCurrentWorkspace);
    jest.clearAllMocks();
    mockSendInvitation.mockResolvedValue({});
    mockWaitForCompanyEnrichmentSettlement.mockResolvedValue(undefined);
    jotaiStore.set(isBookCallOnboardingStepEnabledState.atom, true);
    jotaiStore.set(isCompanyEnrichmentEnabledState.atom, true);
  });

  it('should not wait for an enrichment that will never run', async () => {
    jotaiStore.set(isCompanyEnrichmentEnabledState.atom, false);

    const { result } = renderInviteTeam();

    await act(async () => {
      await result.current.handleSkip();
    });

    expect(mockWaitForCompanyEnrichmentSettlement).not.toHaveBeenCalled();
    expect(mockSetNextOnboardingStatus).toHaveBeenCalled();
  });

  it('should not wait for the enrichment when the book-call step is disabled', async () => {
    jotaiStore.set(isBookCallOnboardingStepEnabledState.atom, false);

    const { result } = renderInviteTeam();

    await act(async () => {
      await result.current.handleSkip();
    });

    expect(mockWaitForCompanyEnrichmentSettlement).not.toHaveBeenCalled();
    expect(mockSetNextOnboardingStatus).toHaveBeenCalled();
  });

  it('should wait for the enrichment answer before advancing', async () => {
    let resolveCompanyEnrichmentSettlement: () => void = () => {};

    mockWaitForCompanyEnrichmentSettlement.mockReturnValue(
      new Promise<void>((resolve) => {
        resolveCompanyEnrichmentSettlement = resolve;
      }),
    );

    const { result } = renderInviteTeam();

    let hasSkipResolved = false;

    await act(async () => {
      void result.current.handleSkip().then(() => {
        hasSkipResolved = true;
      });
    });

    expect(mockWaitForCompanyEnrichmentSettlement).toHaveBeenCalled();
    expect(hasSkipResolved).toBe(false);
    expect(mockSetNextOnboardingStatus).not.toHaveBeenCalled();

    await act(async () => {
      resolveCompanyEnrichmentSettlement();
    });

    expect(hasSkipResolved).toBe(true);
    expect(mockSetNextOnboardingStatus).toHaveBeenCalled();
  });

  it('should start waiting for the enrichment before the invitation resolves', async () => {
    let resolveInvitation: (value: unknown) => void = () => {};

    mockSendInvitation.mockReturnValue(
      new Promise((resolve) => {
        resolveInvitation = resolve;
      }),
    );

    const { result } = renderInviteTeam();

    act(() => {
      void result.current.handleSkip();
    });

    expect(mockWaitForCompanyEnrichmentSettlement).toHaveBeenCalled();
    expect(mockSetNextOnboardingStatus).not.toHaveBeenCalled();

    await act(async () => {
      resolveInvitation({});
    });
  });

  it('should disable the form while the submission is still in flight', async () => {
    let resolveInvitation: (value: unknown) => void = () => {};

    mockSendInvitation.mockReturnValue(
      new Promise((resolve) => {
        resolveInvitation = resolve;
      }),
    );

    const { result } = renderInviteTeam();

    act(() => {
      void result.current.handleSkip();
    });

    expect(result.current.isNavigating).toBe(true);

    await act(async () => {
      resolveInvitation({});
    });
  });

  it('should stay disabled after advancing', async () => {
    const { result } = renderInviteTeam();

    await act(async () => {
      await result.current.handleSkip();
    });

    expect(result.current.isNavigating).toBe(true);
  });

  it('should re-enable submission when sending the invitations fails', async () => {
    mockSendInvitation.mockResolvedValue({ error: new Error('network error') });

    const { result } = renderInviteTeam();

    await act(async () => {
      await expect(result.current.handleSkip()).rejects.toThrow(
        'network error',
      );
    });

    expect(result.current.isNavigating).toBe(false);
    expect(mockSetNextOnboardingStatus).not.toHaveBeenCalled();
  });

  it('should credit the sent invites up to the maximum rewarded invites', async () => {
    jotaiStore.set(onboardingConfigState.atom, {
      importContactsCreditsReward: 1,
      inviteTeamCreditsRewardPerUser: 0.5,
      createProfileCreditsReward: 0.5,
      upgradeCreditsReward: 2,
      inviteTeamMaxInvites: 2,
    });
    jotaiStore.set(onboardingInviteTeamEmailsDraftState.atom, [
      '[email protected]',
      '[email protected]',
      '[email protected]',
      '',
    ]);
    mockSendInvitation.mockResolvedValue({
      data: {
        sendInvitations: {
          result: [
            { email: '[email protected]' },
            { email: '[email protected]' },
            { email: '[email protected]' },
          ],
        },
      },
    });

    const { result } = renderInviteTeam();

    act(() => {
      result.current.handleInvite();
    });

    await waitFor(() => expect(mockSetNextOnboardingStatus).toHaveBeenCalled());
    expect(
      jotaiStore.get(
        onboardingFreeCreditsFamilyState.atomFamily(mockCurrentWorkspace.id),
      ).inviteTeam,
    ).toBe(1);
  });

  it('should bring the typed emails back when going back after the server rejected them all', async () => {
    jotaiStore.set(onboardingInviteTeamEmailsDraftState.atom, [
      '[email protected]',
      '[email protected]',
      '',
    ]);
    mockSendInvitation.mockResolvedValue({
      data: {
        sendInvitations: {
          success: false,
          errors: [
            '[email protected] already invited',
            '[email protected] is already in the workspace',
          ],
          result: [],
        },
      },
    });

    const { result, unmount } = renderInviteTeam();

    act(() => {
      result.current.handleInvite();
    });

    await waitFor(() =>
      expect(mockSetNextOnboardingStatus).toHaveBeenCalledWith({
        stepHistoryEffect: 'recordAsReversible',
      }),
    );
    unmount();

    const { result: resultAfterGoingBack } = renderInviteTeam();

    expect(
      resultAfterGoingBack.current.fields.map(({ email }) => email),
    ).toEqual(['[email protected]', '[email protected]', '']);
  });

  it('should show the server errors instead of the success toast when every invitation is rejected', async () => {
    jotaiStore.set(onboardingInviteTeamEmailsDraftState.atom, [
      '[email protected]',
      '',
    ]);
    mockSendInvitation.mockResolvedValue({
      data: {
        sendInvitations: {
          success: false,
          errors: ['[email protected] already invited'],
          result: [],
        },
      },
    });

    const { result } = renderInviteTeam();

    act(() => {
      result.current.handleInvite();
    });

    await waitFor(() => expect(mockSetNextOnboardingStatus).toHaveBeenCalled());
    expect(mockEnqueueToast).toHaveBeenCalledTimes(1);
    expect(mockEnqueueToast).toHaveBeenCalledWith(
      expect.objectContaining({
        status: 'error',
        children: '[email protected] already invited',
      }),
    );
  });

  it('should drop the invite credits on skip and keep the typed emails', async () => {
    jotaiStore.set(onboardingInviteTeamEmailsDraftState.atom, [
      '[email protected]',
      '',
    ]);
    jotaiStore.set(
      onboardingFreeCreditsFamilyState.atomFamily(mockCurrentWorkspace.id),
      {
        ...jotaiStore.get(
          onboardingFreeCreditsFamilyState.atomFamily(mockCurrentWorkspace.id),
        ),
        inviteTeam: 0.5,
      },
    );

    const { result } = renderInviteTeam();

    await act(async () => {
      await result.current.handleSkip();
    });

    expect(
      jotaiStore.get(
        onboardingFreeCreditsFamilyState.atomFamily(mockCurrentWorkspace.id),
      ).inviteTeam,
    ).toBe(0);
    expect(jotaiStore.get(onboardingInviteTeamEmailsDraftState.atom)).toEqual([
      '[email protected]',
      '',
    ]);
  });

  it('should send the invitations once when inviting again while they are being sent', async () => {
    let resolveInvitation: (value: unknown) => void = () => {};

    jotaiStore.set(onboardingInviteTeamEmailsDraftState.atom, [
      '[email protected]',
      '',
    ]);
    mockSendInvitation.mockReturnValue(
      new Promise((resolve) => {
        resolveInvitation = resolve;
      }),
    );

    const { result } = renderInviteTeam();

    await act(async () => {
      result.current.handleInvite();
    });

    await waitFor(() => expect(mockSendInvitation).toHaveBeenCalledTimes(1));

    await act(async () => {
      result.current.handleInvite();
    });

    await act(async () => {
      resolveInvitation({});
    });

    expect(mockSendInvitation).toHaveBeenCalledTimes(1);
  });

  it('should aim the focus at the first invalid email restored from the draft when the skip dialog opens', async () => {
    jotaiStore.set(onboardingInviteTeamEmailsDraftState.atom, [
      '[email protected]',
      'alan@',
      '',
    ]);

    const { result } = renderInviteTeam();

    await act(async () => {
      await result.current.openSkipDialog();
    });

    expect(result.current.emailIndexToFocus).toBe(1);
  });
});