chore: add test case for download components (#29569)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import React from 'react'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import ResDownload from './index'
|
||||
|
||||
jest.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => key,
|
||||
}),
|
||||
}))
|
||||
|
||||
const mockType = { Link: 'mock-link' }
|
||||
let capturedProps: Record<string, unknown> | undefined
|
||||
|
||||
jest.mock('react-papaparse', () => ({
|
||||
useCSVDownloader: () => {
|
||||
const CSVDownloader = ({ children, ...props }: React.PropsWithChildren<Record<string, unknown>>) => {
|
||||
capturedProps = props
|
||||
return <div data-testid="csv-downloader" className={props.className as string}>{children}</div>
|
||||
}
|
||||
return {
|
||||
CSVDownloader,
|
||||
Type: mockType,
|
||||
}
|
||||
},
|
||||
}))
|
||||
|
||||
describe('ResDownload', () => {
|
||||
const values = [{ text: 'Hello' }]
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
capturedProps = undefined
|
||||
})
|
||||
|
||||
it('should render desktop download button with CSV downloader props', () => {
|
||||
render(<ResDownload isMobile={false} values={values} />)
|
||||
|
||||
expect(screen.getByTestId('csv-downloader')).toBeInTheDocument()
|
||||
expect(screen.getByText('common.operation.download')).toBeInTheDocument()
|
||||
expect(capturedProps?.data).toEqual(values)
|
||||
expect(capturedProps?.filename).toBe('result')
|
||||
expect(capturedProps?.bom).toBe(true)
|
||||
expect(capturedProps?.type).toBe(mockType.Link)
|
||||
})
|
||||
|
||||
it('should render mobile action button without desktop label', () => {
|
||||
render(<ResDownload isMobile={true} values={values} />)
|
||||
|
||||
expect(screen.getByTestId('csv-downloader')).toBeInTheDocument()
|
||||
expect(screen.queryByText('common.operation.download')).not.toBeInTheDocument()
|
||||
expect(screen.getByRole('button')).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user