File size: 2,032 Bytes
67756a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { I18nProvider } from '../i18n'
import { D2SignInPage } from './D2SignInPage'
import * as api from '../api/identity'

vi.mock('../api/identity')

const renderPage = (onNav = () => {}) =>
  render(<I18nProvider initial="en"><D2SignInPage onNav={onNav} /></I18nProvider>)

describe('D2SignInPage', () => {
  beforeEach(() => vi.resetAllMocks())

  it('offers passkey sign-in and no password field', () => {
    renderPage()
    expect(screen.getByRole('button', { name: 'Sign in with passkey' })).toBeInTheDocument()
    expect(document.querySelector('input[type="password"]')).toBeNull()
  })

  it('signs in with a discoverable passkey and navigates to the account', async () => {
    const onNav = vi.fn()
    vi.mocked(api.signIn).mockResolvedValue({
      account: {
        id: 'a1', public_handle: 'ali', display_alias: 'Ali', role: 'customer',
        status: 'active', preferred_language: 'en',
      },
      csrf_token: 'csrf',
    })
    renderPage(onNav)
    fireEvent.click(screen.getByRole('button', { name: 'Sign in with passkey' }))
    await waitFor(() => expect(api.signIn).toHaveBeenCalledWith(undefined))
    await waitFor(() => expect(onNav).toHaveBeenCalledWith('id/account'))
  })

  it('reveals a handle-assisted fallback and passes the handle', async () => {
    vi.mocked(api.signIn).mockResolvedValue({
      account: {
        id: 'a1', public_handle: 'ali', display_alias: 'Ali', role: 'customer',
        status: 'active', preferred_language: 'en',
      },
      csrf_token: 'csrf',
    })
    renderPage()
    fireEvent.click(screen.getByRole('button', { name: 'Sign in with a handle instead' }))
    fireEvent.change(await screen.findByLabelText('Public handle'), { target: { value: 'ali' } })
    fireEvent.click(screen.getByRole('button', { name: 'Sign in with handle' }))
    await waitFor(() => expect(api.signIn).toHaveBeenCalledWith('ali'))
  })
})