|
| 1 | +import { act, screen, waitFor } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { describe, expect, it, onTestFinished, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { holdRequests, serveFapi } from '../../../__tests__/feature/fake-fapi'; |
| 6 | +import { fapiEnvironment } from '../../../__tests__/feature/fapi'; |
| 7 | +import { renderWithClerk } from '../../../__tests__/feature/render'; |
| 8 | +import type { WaitlistProps } from '../waitlist'; |
| 9 | +import { Waitlist } from '../waitlist'; |
| 10 | + |
| 11 | +function renderWaitlist(props: WaitlistProps = {}) { |
| 12 | + return renderWithClerk(<Waitlist {...props} />); |
| 13 | +} |
| 14 | + |
| 15 | +const emailField = () => screen.getByRole('textbox', { name: 'Email address' }); |
| 16 | +const joinButton = () => screen.getByRole('button', { name: 'Join the waitlist' }); |
| 17 | + |
| 18 | +async function join(emailAddress: string) { |
| 19 | + const user = userEvent.setup(); |
| 20 | + await user.type(await screen.findByRole('textbox', { name: 'Email address' }), emailAddress); |
| 21 | + await user.click(joinButton()); |
| 22 | + return user; |
| 23 | +} |
| 24 | + |
| 25 | +describe('Waitlist', () => { |
| 26 | + describe('joining', () => { |
| 27 | + it('adds the email address to the waitlist and thanks the user', async () => { |
| 28 | + const fapi = serveFapi(); |
| 29 | + await renderWaitlist(); |
| 30 | + |
| 31 | + await join('alice@example.com'); |
| 32 | + |
| 33 | + expect(await screen.findByRole('heading', { name: 'Thanks for joining the waitlist!' })).toHaveFocus(); |
| 34 | + expect(screen.getByText('We’ll be in touch when your spot is ready')).toBeInTheDocument(); |
| 35 | + expect(fapi.waitlistEntries).toEqual(['alice@example.com']); |
| 36 | + expect(screen.queryByRole('status')).toBeNull(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('holds the form while the request is in flight', async () => { |
| 40 | + serveFapi(); |
| 41 | + await renderWaitlist(); |
| 42 | + const request = holdRequests('post', '/v1/waitlist'); |
| 43 | + |
| 44 | + await join('alice@example.com'); |
| 45 | + |
| 46 | + await waitFor(() => expect(request.requests).toHaveLength(1)); |
| 47 | + expect(emailField()).toHaveAttribute('aria-disabled', 'true'); |
| 48 | + await userEvent.setup().click(screen.getByRole('button', { name: /Join the waitlist/ })); |
| 49 | + expect(request.requests).toHaveLength(1); |
| 50 | + |
| 51 | + request.release(); |
| 52 | + expect(await screen.findByRole('heading', { name: 'Thanks for joining the waitlist!' })).toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('cannot be submitted without an email address', async () => { |
| 56 | + serveFapi(); |
| 57 | + await renderWaitlist(); |
| 58 | + |
| 59 | + await screen.findByRole('textbox', { name: 'Email address' }); |
| 60 | + expect(joinButton()).toHaveAttribute('aria-disabled', 'true'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('fills in the email address from the query string', async () => { |
| 64 | + const originalUrl = window.location.href; |
| 65 | + window.history.replaceState(null, '', '?email_address=bob%40example.com'); |
| 66 | + onTestFinished(() => window.history.replaceState(null, '', originalUrl)); |
| 67 | + const fapi = serveFapi(); |
| 68 | + await renderWaitlist(); |
| 69 | + |
| 70 | + expect(await screen.findByRole('textbox', { name: 'Email address' })).toHaveValue('bob@example.com'); |
| 71 | + await userEvent.setup().click(joinButton()); |
| 72 | + |
| 73 | + await screen.findByRole('heading', { name: 'Thanks for joining the waitlist!' }); |
| 74 | + expect(fapi.waitlistEntries).toEqual(['bob@example.com']); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('errors', () => { |
| 79 | + it('shows an invalid email address on the field', async () => { |
| 80 | + const fapi = serveFapi(); |
| 81 | + await renderWaitlist(); |
| 82 | + |
| 83 | + await join('not-an-email'); |
| 84 | + |
| 85 | + await waitFor(() => |
| 86 | + expect(emailField()).toHaveAccessibleDescription('Email address must be a valid email address.'), |
| 87 | + ); |
| 88 | + expect(emailField()).toHaveAttribute('aria-invalid', 'true'); |
| 89 | + expect(fapi.waitlistEntries).toEqual([]); |
| 90 | + expect(screen.queryByRole('heading', { name: 'Thanks for joining the waitlist!' })).toBeNull(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('shows a failure that names no field above the form, and lets the user try again', async () => { |
| 94 | + serveFapi(); |
| 95 | + await renderWaitlist(); |
| 96 | + const request = holdRequests('post', '/v1/waitlist'); |
| 97 | + |
| 98 | + await join('alice@example.com'); |
| 99 | + await waitFor(() => expect(request.requests).toHaveLength(1)); |
| 100 | + request.fail('waitlist_unavailable', 'The waitlist is not accepting new entries.'); |
| 101 | + |
| 102 | + await waitFor(() => |
| 103 | + expect(screen.getByRole('alert')).toHaveTextContent('The waitlist is not accepting new entries.'), |
| 104 | + ); |
| 105 | + expect(emailField()).toHaveValue('alice@example.com'); |
| 106 | + expect(emailField()).not.toHaveAttribute('aria-disabled'); |
| 107 | + expect(joinButton()).not.toHaveAttribute('aria-disabled'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('after joining', () => { |
| 112 | + it('sends the user to afterJoinWaitlistUrl once the thank-you has shown', async () => { |
| 113 | + serveFapi(); |
| 114 | + const { navigate } = await renderWaitlist({ afterJoinWaitlistUrl: '/thanks' }); |
| 115 | + vi.useFakeTimers({ shouldAdvanceTime: true }); |
| 116 | + onTestFinished(() => { |
| 117 | + vi.useRealTimers(); |
| 118 | + }); |
| 119 | + |
| 120 | + const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime }); |
| 121 | + await user.type(await screen.findByRole('textbox', { name: 'Email address' }), 'alice@example.com'); |
| 122 | + await user.click(joinButton()); |
| 123 | + |
| 124 | + await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('You will be redirected soon…')); |
| 125 | + expect(navigate).not.toHaveBeenCalled(); |
| 126 | + |
| 127 | + await act(() => vi.advanceTimersByTimeAsync(2000)); |
| 128 | + await waitFor(() => expect(navigate).toHaveBeenCalledWith('/thanks')); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('signing in', () => { |
| 133 | + it('goes to the application sign-in URL', async () => { |
| 134 | + serveFapi({ environment: fapiEnvironment({ display_config: { sign_in_url: '/login' } }) }); |
| 135 | + const { navigate } = await renderWaitlist(); |
| 136 | + |
| 137 | + await userEvent.setup().click(await screen.findByRole('button', { name: 'Sign in' })); |
| 138 | + |
| 139 | + await waitFor(() => expect(navigate).toHaveBeenCalledWith('/login')); |
| 140 | + }); |
| 141 | + |
| 142 | + it('goes to signInUrl when one is passed', async () => { |
| 143 | + serveFapi(); |
| 144 | + const { navigate } = await renderWaitlist({ signInUrl: '/custom-sign-in' }); |
| 145 | + |
| 146 | + await userEvent.setup().click(await screen.findByRole('button', { name: 'Sign in' })); |
| 147 | + |
| 148 | + await waitFor(() => expect(navigate).toHaveBeenCalledWith('/custom-sign-in')); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + describe('branding', () => { |
| 153 | + it('signs the card when the instance is branded', async () => { |
| 154 | + serveFapi({ environment: fapiEnvironment({ display_config: { branded: true } }) }); |
| 155 | + await renderWaitlist(); |
| 156 | + |
| 157 | + await screen.findByRole('textbox', { name: 'Email address' }); |
| 158 | + expect(screen.getByText(/Secured by/)).toBeInTheDocument(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('carries no signature when the branding is paid off', async () => { |
| 162 | + serveFapi(); |
| 163 | + await renderWaitlist(); |
| 164 | + |
| 165 | + await screen.findByRole('textbox', { name: 'Email address' }); |
| 166 | + expect(screen.queryByText(/Secured by/)).toBeNull(); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments