mirror of
https://github.com/langgenius/dify.git
synced 2026-07-23 20:18:40 +08:00
62 lines
2.4 KiB
TypeScript
62 lines
2.4 KiB
TypeScript
import { fireEvent, render, screen } from '@testing-library/react'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import { setPostLoginRedirect } from '@/app/signin/utils/post-login-redirect'
|
|
import Chooser from '../chooser'
|
|
|
|
const mockPush = vi.fn()
|
|
|
|
vi.mock('@/next/navigation', () => ({
|
|
useRouter: () => ({ push: mockPush }),
|
|
}))
|
|
|
|
vi.mock('@/app/signin/utils/post-login-redirect', () => ({
|
|
setPostLoginRedirect: vi.fn(),
|
|
}))
|
|
|
|
describe('Chooser', () => {
|
|
it('renders account button', () => {
|
|
render(<Chooser userCode="ABCD-3456" ssoAvailable={false} />)
|
|
expect(
|
|
screen.getByRole('button', { name: /deviceFlow.chooser.signInAccount/i }),
|
|
).toBeInTheDocument()
|
|
})
|
|
|
|
it('hides SSO button when ssoAvailable is false', () => {
|
|
render(<Chooser userCode="ABCD-3456" ssoAvailable={false} />)
|
|
expect(
|
|
screen.queryByRole('button', { name: /deviceFlow.chooser.signInSSO/i }),
|
|
).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('shows SSO button when ssoAvailable is true', () => {
|
|
render(<Chooser userCode="ABCD-3456" ssoAvailable={true} />)
|
|
expect(
|
|
screen.getByRole('button', { name: /deviceFlow.chooser.signInSSO/i }),
|
|
).toBeInTheDocument()
|
|
})
|
|
|
|
it('sets post-login redirect and navigates to /signin on account button click', () => {
|
|
render(<Chooser userCode="ABCD-3456" ssoAvailable={false} />)
|
|
fireEvent.click(screen.getByRole('button', { name: /deviceFlow.chooser.signInAccount/i }))
|
|
expect(vi.mocked(setPostLoginRedirect)).toHaveBeenCalledWith('/device?user_code=ABCD-3456')
|
|
expect(mockPush).toHaveBeenCalledWith('/signin')
|
|
})
|
|
|
|
it('encodes userCode in post-login redirect', () => {
|
|
// Uses a code with a space to exercise encodeURIComponent
|
|
render(<Chooser userCode="AB CD" ssoAvailable={false} />)
|
|
fireEvent.click(screen.getByRole('button', { name: /deviceFlow.chooser.signInAccount/i }))
|
|
expect(vi.mocked(setPostLoginRedirect)).toHaveBeenCalledWith('/device?user_code=AB%20CD')
|
|
})
|
|
|
|
it('navigates to SSO initiate URL on SSO button click', () => {
|
|
Object.defineProperty(window, 'location', {
|
|
writable: true,
|
|
value: { href: '' },
|
|
})
|
|
render(<Chooser userCode="ABCD-3456" ssoAvailable={true} />)
|
|
fireEvent.click(screen.getByRole('button', { name: /deviceFlow.chooser.signInSSO/i }))
|
|
expect(window.location.href).toBe('/openapi/v1/oauth/device/sso-initiate?user_code=ABCD-3456')
|
|
})
|
|
})
|