Files
kamidso-front/src/features/assistant/components/HtmlPreview.test.tsx
T

66 lines
2.7 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createHtmlShare } from '../api/reports.api'
import { downloadReportPdf } from '../report/downloadReportPdf'
import { HtmlPreview } from './HtmlPreview'
vi.mock('../report/downloadReportPdf', async () => {
const actual = await vi.importActual<typeof import('../report/downloadReportPdf')>(
'../report/downloadReportPdf',
)
return { ...actual, downloadReportPdf: vi.fn() }
})
vi.mock('../api/reports.api', () => ({
createHtmlShare: vi.fn(async () => ({
id: 'rpt_abc',
title: 'گزارش',
share_path: '/share/reports/rpt_abc',
})),
}))
const sample = "<html dir='rtl' lang='fa'><body><h1>گزارش</h1></body></html>"
describe('HtmlPreview', () => {
beforeEach(() => {
vi.mocked(downloadReportPdf).mockClear()
vi.mocked(createHtmlShare).mockClear()
})
it('opens the report full screen and closes it', async () => {
const user = userEvent.setup()
render(<HtmlPreview html={sample} />)
expect(screen.getByTitle('html-preview')).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'تمام‌صفحه' }))
expect(screen.getByRole('dialog', { name: 'گزارش تحلیل' })).toBeInTheDocument()
expect(screen.getByTitle('html-preview-full')).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'بستن' }))
expect(screen.queryByRole('dialog', { name: 'گزارش تحلیل' })).not.toBeInTheDocument()
})
it('downloads the report as a print-ready PDF', async () => {
const user = userEvent.setup()
render(<HtmlPreview html={sample} />)
await user.click(screen.getByRole('button', { name: 'دانلود PDF' }))
expect(downloadReportPdf).toHaveBeenCalledWith(sample, 'گزارش تحلیل')
})
it('creates a public share link and copies it', async () => {
const user = userEvent.setup()
const writeText = vi.fn().mockResolvedValue(undefined)
vi.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeText)
render(<HtmlPreview html={sample} />)
await user.click(screen.getByRole('button', { name: 'اشتراک‌گذاری' }))
expect(createHtmlShare).toHaveBeenCalledWith(sample, 'گزارش تحلیل')
expect(await screen.findByText(/\/share\/reports\/rpt_abc$/)).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'کپی لینک' }))
expect(writeText).toHaveBeenCalledWith(expect.stringMatching(/\/share\/reports\/rpt_abc$/))
expect(screen.getByRole('button', { name: 'لینک کپی شد' })).toBeInTheDocument()
})
})