|
| 1 | +import { ClerkAPIResponseError } from '@clerk/shared/error'; |
| 2 | +import type { SignInResource } from '@clerk/shared/types'; |
| 3 | +import { fireEvent } from '@testing-library/react'; |
| 4 | +import React from 'react'; |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +import { bindCreateFixtures } from '@/test/create-fixtures'; |
| 8 | +import { render, screen } from '@/test/utils'; |
| 9 | + |
| 10 | +import { PathRouter } from '../../../router'; |
| 11 | +import { SignIn } from '../index'; |
| 12 | + |
| 13 | +const { createFixtures } = bindCreateFixtures('SignIn'); |
| 14 | + |
| 15 | +const passwordError = (code: string, long_message: string) => |
| 16 | + new ClerkAPIResponseError('Error', { |
| 17 | + data: [{ code, long_message, message: long_message, meta: { param_name: 'password' } }], |
| 18 | + status: 422, |
| 19 | + }); |
| 20 | + |
| 21 | +const PASSWORD_RESET_REQUIRED = passwordError( |
| 22 | + 'password_reset_required', |
| 23 | + 'This account requires a password reset before sign-in can continue. Please reset your password or contact support.', |
| 24 | +); |
| 25 | + |
| 26 | +const setup = async ({ globalClerk, error }: { globalClerk: boolean; error: ClerkAPIResponseError }) => { |
| 27 | + const { wrapper, fixtures } = await createFixtures(f => { |
| 28 | + f.withEmailAddress(); |
| 29 | + f.withPassword({ required: true }); |
| 30 | + f.startSignInWithEmailAddress({ supportEmailCode: true, supportPassword: true, supportResetPassword: true }); |
| 31 | + }); |
| 32 | + vi.mocked(fixtures.clerk.navigate).mockImplementation((to: string) => { |
| 33 | + window.history.pushState({}, '', new URL(to, window.location.href).href); |
| 34 | + return Promise.resolve(); |
| 35 | + }); |
| 36 | + fixtures.signIn.create |
| 37 | + .mockRejectedValueOnce(error) |
| 38 | + .mockResolvedValueOnce({ status: 'needs_first_factor' } as SignInResource); |
| 39 | + fixtures.signIn.prepareFirstFactor.mockResolvedValue({} as SignInResource); |
| 40 | + window.Clerk = globalClerk ? (fixtures.clerk as typeof window.Clerk) : undefined; |
| 41 | + |
| 42 | + return { wrapper, fixtures }; |
| 43 | +}; |
| 44 | + |
| 45 | +const submitAutofilledPassword = async (wrapper: React.FC<{ children: React.ReactNode }>) => { |
| 46 | + window.history.replaceState({}, '', '/sign-in'); |
| 47 | + const result = render( |
| 48 | + <PathRouter basePath='/sign-in'> |
| 49 | + <SignIn /> |
| 50 | + </PathRouter>, |
| 51 | + { wrapper }, |
| 52 | + ); |
| 53 | + await result.userEvent.type(screen.getByLabelText(/email address/i), 'hello@clerk.com'); |
| 54 | + fireEvent.change(result.container.querySelector('#password-field') as Element, { |
| 55 | + target: { value: 'some-password' }, |
| 56 | + }); |
| 57 | + fireEvent.submit(result.container.querySelector('form') as Element); |
| 58 | + return result; |
| 59 | +}; |
| 60 | + |
| 61 | +describe('SignIn with a password autofilled on the first step', () => { |
| 62 | + const originalClerk = window.Clerk; |
| 63 | + |
| 64 | + afterEach(() => { |
| 65 | + window.Clerk = originalClerk; |
| 66 | + window.history.replaceState({}, '', '/'); |
| 67 | + }); |
| 68 | + |
| 69 | + it.each([ |
| 70 | + ['Clerk is also window.Clerk', true], |
| 71 | + ['Clerk comes from the npm package and window.Clerk is not set', false], |
| 72 | + ])('opens the password reset screen when the password needs a reset and %s', async (_, globalClerk) => { |
| 73 | + const { wrapper, fixtures } = await setup({ globalClerk, error: PASSWORD_RESET_REQUIRED }); |
| 74 | + const { userEvent } = await submitAutofilledPassword(wrapper); |
| 75 | + |
| 76 | + await screen.findByText('Password reset required'); |
| 77 | + expect(window.location.pathname).toBe('/sign-in/factor-one'); |
| 78 | + screen.getByText('You need to reset your password before you can use it again.'); |
| 79 | + screen.getByText('Email code to hello@clerk.com'); |
| 80 | + expect(screen.queryByLabelText('Password')).not.toBeInTheDocument(); |
| 81 | + expect(fixtures.signIn.create).toHaveBeenCalledTimes(2); |
| 82 | + expect(fixtures.signIn.create).toHaveBeenLastCalledWith( |
| 83 | + expect.not.objectContaining({ password: expect.anything() }), |
| 84 | + ); |
| 85 | + |
| 86 | + await userEvent.click(screen.getByText('Reset your password')); |
| 87 | + await screen.findByText('First, enter the code sent to your email address'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('asks for the password again on the next step when the password was found in a breach', async () => { |
| 91 | + const { wrapper } = await setup({ |
| 92 | + globalClerk: false, |
| 93 | + error: passwordError('form_password_pwned', 'Password has been found in an online data breach.'), |
| 94 | + }); |
| 95 | + await submitAutofilledPassword(wrapper); |
| 96 | + |
| 97 | + await screen.findByLabelText('Password'); |
| 98 | + expect(window.location.pathname).toBe('/sign-in/factor-one'); |
| 99 | + expect(screen.queryByText('Password reset required')).not.toBeInTheDocument(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments