Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.0 KB
Raw Blame
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { useLocale } from '#i18n'
import { useRouter, useSearchParams } from '@/next/navigation'
import { consoleClient } from '@/service/console'
import CheckCodePage from '../page'

const mockPush = vi.fn()

vi.mock('@/app/components/signin/countdown', () => ({
  default: () => null,
}))

vi.mock('#i18n', async (importOriginal) => ({
  ...(await importOriginal<typeof import('#i18n')>()),
  useLocale: vi.fn(),
}))

vi.mock('@/hooks/use-document-title', () => ({
  default: vi.fn(),
}))

vi.mock('@/next/navigation', () => ({
  useRouter: vi.fn(),
  useSearchParams: vi.fn(),
}))

vi.mock('@/service/console', async (importOriginal) => ({
  ...(await importOriginal<typeof import('@/service/console')>()),
  consoleClient: {
    forgotPassword: {
      post: vi.fn(),
      validity: {
        post: vi.fn(),
      },
    },
  },
}))

describe('ResetPasswordCheckCodePage', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    vi.mocked(useLocale).mockReturnValue('en-US')
    vi.mocked(useRouter).mockReturnValue({ push: mockPush } as unknown as ReturnType<
      typeof useRouter
    >)
    vi.mocked(useSearchParams).mockReturnValue(
      new URLSearchParams({
        email: '[email protected]',
        token: 'email-token',
      }) as ReturnType<typeof useSearchParams>,
    )
    vi.mocked(consoleClient.forgotPassword.validity.post).mockResolvedValue({
      email: '[email protected]',
      is_valid: true,
      token: 'verified-token',
    })
  })

  it('labels the one-time code field and submits it with Enter', async () => {
    const user = userEvent.setup()
    render(<CheckCodePage />)

    const codeInput = screen.getByRole('textbox', {
      name: 'login.checkCode.verificationCode',
    })
    expect(codeInput).toHaveAttribute('inputmode', 'numeric')
    expect(codeInput).toHaveAttribute('autocomplete', 'one-time-code')

    await user.type(codeInput, '123456{Enter}')

    await waitFor(() => {
      expect(consoleClient.forgotPassword.validity.post).toHaveBeenCalledWith({
        body: {
          code: '123456',
          email: '[email protected]',
          token: 'email-token',
        },
      })
    })
    expect(mockPush).toHaveBeenCalledWith(
      '/reset-password/set-password?email=user%40example.com&token=verified-token',
    )
  })

  it('associates an incomplete code error with the field', async () => {
    const user = userEvent.setup()
    render(<CheckCodePage />)

    const codeInput = screen.getByRole('textbox', {
      name: 'login.checkCode.verificationCode',
    })
    await user.type(codeInput, '123{Enter}')

    const error = await screen.findByText('login.checkCode.invalidCode')
    expect(codeInput).toHaveAttribute('aria-invalid', 'true')
    expect(codeInput).toHaveAccessibleDescription(error.textContent ?? '')
    expect(codeInput).toHaveFocus()
    expect(consoleClient.forgotPassword.validity.post).not.toHaveBeenCalled()
  })
})