dify/web/app/device/components/__tests__/authorize-account.spec.tsx
Yunlu Wen a728e0ac69
feat: adding dify cli (#36348)
Co-authored-by: GareArc <garethcxy@dify.ai>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: L1nSn0w <l1nsn0w@qq.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: gigglewang <gigglewang@dify.ai>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
2026-05-26 01:12:36 +00:00

89 lines
3.0 KiB
TypeScript

import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import AuthorizeAccount from '../authorize-account'
const mockApproveAccount = vi.fn()
const mockDenyAccount = vi.fn()
vi.mock('@/service/device-flow', () => ({
deviceApproveAccount: (...args: unknown[]) => mockApproveAccount(...args),
deviceDenyAccount: (...args: unknown[]) => mockDenyAccount(...args),
DeviceFlowError: class extends Error {
code: string
status: number
constructor(code: string, status = 400) {
super(code)
this.code = code
this.status = status
}
},
}))
const makeProps = () => ({
userCode: 'ABCD-3456',
accountEmail: 'gareth@example.com',
accountName: 'Gareth Chen',
accountAvatarUrl: null,
defaultWorkspace: 'Dify Enterprise',
onApproved: vi.fn(),
onDenied: vi.fn(),
onError: vi.fn(),
})
describe('AuthorizeAccount', () => {
beforeEach(() => {
vi.clearAllMocks()
mockApproveAccount.mockResolvedValue(undefined)
mockDenyAccount.mockResolvedValue(undefined)
})
it('renders accountName', () => {
render(<AuthorizeAccount {...makeProps()} />)
expect(screen.getByText('Gareth Chen')).toBeInTheDocument()
})
it('renders accountEmail', () => {
render(<AuthorizeAccount {...makeProps()} />)
expect(screen.getByText('gareth@example.com')).toBeInTheDocument()
})
it('renders defaultWorkspace', () => {
render(<AuthorizeAccount {...makeProps()} />)
expect(screen.getByText(/Dify Enterprise/)).toBeInTheDocument()
})
it('calls deviceApproveAccount with userCode on Authorize click', async () => {
render(<AuthorizeAccount {...makeProps()} />)
fireEvent.click(screen.getByRole('button', { name: /Authorize/i }))
await waitFor(() => expect(mockApproveAccount).toHaveBeenCalledWith('ABCD-3456'))
})
it('calls onApproved after successful approve', async () => {
const props = makeProps()
render(<AuthorizeAccount {...props} />)
fireEvent.click(screen.getByRole('button', { name: /Authorize/i }))
await waitFor(() => expect(props.onApproved).toHaveBeenCalled())
})
it('calls deviceDenyAccount with userCode on Cancel click', async () => {
render(<AuthorizeAccount {...makeProps()} />)
fireEvent.click(screen.getByRole('button', { name: /Cancel/i }))
await waitFor(() => expect(mockDenyAccount).toHaveBeenCalledWith('ABCD-3456'))
})
it('calls onDenied after successful deny', async () => {
const props = makeProps()
render(<AuthorizeAccount {...props} />)
fireEvent.click(screen.getByRole('button', { name: /Cancel/i }))
await waitFor(() => expect(props.onDenied).toHaveBeenCalled())
})
it('calls onError when approve throws', async () => {
mockApproveAccount.mockRejectedValue(new Error('unexpected'))
const props = makeProps()
render(<AuthorizeAccount {...props} />)
fireEvent.click(screen.getByRole('button', { name: /Authorize/i }))
await waitFor(() => expect(props.onError).toHaveBeenCalledWith(expect.any(String)))
})
})