Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.4 KB
Raw Blame
import '@testing-library/jest-dom'

import { cleanup, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { Provider } from 'kea'
import { router } from 'kea-router'

import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'

import { InviteSignup } from './InviteSignup'
import { inviteSignupLogic } from './inviteSignupLogic'

describe('InviteSignup', () => {
    beforeEach(() => {
        useMocks({
            get: {
                // The signup form only renders for a logged-out person
                '/api/users/@me/': () => [401, { detail: 'Not authenticated' }],
                '/api/signup/1234/': () => [
                    200,
                    { id: '1234', target_email: '[email protected]', first_name: 'Jane', organization_name: 'Example' },
                ],
            },
            post: {
                '/api/login/precheck': () => [200, { sso_enforcement: null, saml_available: false }],
            },
        })
        window.POSTHOG_APP_CONTEXT = { current_user: null } as any
        initKeaTests()
        inviteSignupLogic.mount()
        router.actions.push('/signup/1234')
        render(
            <Provider>
                <InviteSignup />
            </Provider>
        )
    })

    afterEach(() => {
        cleanup()
    })

    it('points a submit with no password at the password input', async () => {
        const passwordInput = await screen.findByPlaceholderText('••••••••••')
        await userEvent.click(await screen.findByText(/Join Example/))

        expect(passwordInput).toHaveFocus()
        expect(screen.getByText('Please enter your password to continue')).toBeInTheDocument()
    })

    it('states the password rules before the first submit, then shows live feedback for a short password', async () => {
        const passwordInput = await screen.findByPlaceholderText('••••••••••')
        expect(screen.getByText(/At least 8 characters/)).toBeInTheDocument()

        await userEvent.type(passwordInput, 'abc')

        expect(await screen.findByText('Must be at least 8 characters long')).toBeInTheDocument()
    })

    it('marks the role as optional, because this form submits without one', async () => {
        expect((await screen.findByText('What is your role?')).closest('label')).toHaveTextContent('(optional)')
    })
})