Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

2.5 KB
Raw Blame
import wootAPI from '../apiClient';
import { register, resendConfirmation, verifyPasswordToken } from '../auth';
import { setAuthCredentials } from 'dashboard/store/utils/api';

vi.mock('../apiClient', () => ({
  default: {
    post: vi.fn(),
  },
}));

vi.mock('dashboard/store/utils/api', () => ({
  setAuthCredentials: vi.fn(),
  throwErrorMessage: vi.fn(),
  clearLocalStorageOnLogout: vi.fn(),
  parseAPIErrorResponse: vi.fn(),
}));

describe('auth API', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('keeps the regular signup payload unchanged', async () => {
    wootAPI.post.mockResolvedValue({ data: { email: '[email protected]' } });

    await register({
      email: '[email protected]',
      password: 'Password1!',
      hCaptchaClientResponse: 'captcha-token',
    });

    expect(wootAPI.post).toHaveBeenCalledWith('api/v1/accounts.json', {
      account_name: 'Acme',
      user_full_name: 'John',
      email: '[email protected]',
      password: 'Password1!',
      h_captcha_client_response: 'captcha-token',
    });
  });

  it('includes the pending install token for Shopify signup', async () => {
    wootAPI.post.mockResolvedValue({ data: { email: '[email protected]' } });

    await register({
      email: '[email protected]',
      password: 'Password1!',
      hCaptchaClientResponse: 'captcha-token',
      shopifyPendingInstallToken: 'pending-install-token',
    });

    expect(wootAPI.post).toHaveBeenCalledWith(
      'api/v1/accounts.json',
      expect.objectContaining({
        shopify_pending_install_token: 'pending-install-token',
      })
    );
  });
  it('returns the authenticated user after email confirmation', async () => {
    const user = { id: 1, accounts: [{ id: 2 }] };
    const response = { data: { data: user } };
    wootAPI.post.mockResolvedValue(response);

    await expect(
      verifyPasswordToken({ confirmationToken: 'confirmation-token' })
    ).resolves.toEqual(user);
    expect(setAuthCredentials).toHaveBeenCalledWith(response);
  });

  it('includes the pending install redirect when resending confirmation', async () => {
    const redirectUrl = `settings/integrations/shopify?shopify_pending_install=${'a'.repeat(32)}`;

    await resendConfirmation({
      email: '[email protected]',
      hCaptchaClientResponse: 'captcha-token',
      redirectUrl,
    });

    expect(wootAPI.post).toHaveBeenCalledWith('resend_confirmation', {
      email: '[email protected]',
      h_captcha_client_response: 'captcha-token',
      redirect_url: redirectUrl,
    });
  });
});