| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- import React from 'react'
- import { render, screen, fireEvent, waitFor } from '@testing-library/react'
- import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
- import VideoManagement from '../src/pages/VideoManagement/VideoManagement'
- // Mock Taro
- jest.mock('@tarojs/taro', () => ({
- showToast: jest.fn(),
- showLoading: jest.fn(),
- hideLoading: jest.fn(),
- showShareMenu: jest.fn(),
- showModal: jest.fn(({ success }) => success?.({ confirm: true })),
- openDocument: jest.fn()
- }))
- // Mock API client
- jest.mock('../src/api/enterpriseOrderClient', () => ({
- enterpriseOrderClient: {
- 'video-statistics': {
- $get: jest.fn()
- },
- 'company-videos': {
- $get: jest.fn()
- },
- 'batch-download': {
- $post: jest.fn()
- }
- }
- }))
- // Mock layouts
- jest.mock('@d8d/yongren-shared-ui/components/YongrenTabBarLayout', () => ({
- YongrenTabBarLayout: ({ children, activeKey }: any) => (
- <div data-testid="tab-bar-layout" data-active-key={activeKey}>
- {children}
- </div>
- )
- }))
- jest.mock('@d8d/mini-shared-ui-components/components/navbar', () => ({
- Navbar: ({ title }: any) => <div data-testid="navbar">{title}</div>
- }))
- const { enterpriseOrderClient } = require('../../api/enterpriseOrderClient')
- const Taro = require('@tarojs/taro')
- const createTestQueryClient = () => new QueryClient({
- defaultOptions: {
- queries: { retry: false },
- mutations: { retry: false }
- }
- })
- const renderWithQueryClient = (component: React.ReactElement) => {
- const queryClient = createTestQueryClient()
- return render(
- <QueryClientProvider client={queryClient}>
- {component}
- </QueryClientProvider>
- )
- }
- describe('VideoManagement', () => {
- beforeEach(() => {
- jest.clearAllMocks()
- })
- describe('视频列表加载', () => {
- it('应该显示加载状态', async () => {
- enterpriseOrderClient['video-statistics'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ total: 10, stats: [] })
- })
- enterpriseOrderClient['company-videos'].$get.mockImplementation(
- () => new Promise(() => {}) // 永不resolve,测试加载状态
- )
- renderWithQueryClient(<VideoManagement />)
- expect(screen.getByText('加载中...')).toBeInTheDocument()
- })
- it('应该成功加载并显示视频列表', async () => {
- const mockStatistics = {
- total: 12,
- stats: [
- { assetType: 'salary_video', count: 4, percentage: 33 },
- { assetType: 'tax_video', count: 3, percentage: 25 },
- { assetType: 'checkin_video', count: 5, percentage: 42 }
- ]
- }
- const mockVideos = {
- data: [
- {
- id: '1',
- talentName: '张明',
- assetType: 'salary_video',
- status: 'verified',
- fileSize: 12345678,
- duration: 45,
- description: '工资发放确认视频',
- createdAt: '2023-11-25T10:00:00Z',
- fileUrl: 'https://example.com/video1.mp4'
- },
- {
- id: '2',
- talentName: '李小红',
- assetType: 'tax_video',
- status: 'pending',
- fileSize: 10800000,
- duration: 38,
- description: '个税申报确认视频',
- createdAt: '2023-11-24T10:00:00Z',
- fileUrl: 'https://example.com/video2.mp4'
- }
- ]
- }
- enterpriseOrderClient['video-statistics'].$get.mockResolvedValue({
- ok: true,
- json: async () => mockStatistics
- })
- enterpriseOrderClient['company-videos'].$get.mockResolvedValue({
- ok: true,
- json: async () => mockVideos
- })
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('视频列表 (2)')).toBeInTheDocument()
- expect(screen.getByText('张明 - 工资视频')).toBeInTheDocument()
- expect(screen.getByText('李小红 - 个税视频')).toBeInTheDocument()
- expect(screen.getByText('已验证')).toBeInTheDocument()
- expect(screen.getByText('待审核')).toBeInTheDocument()
- })
- })
- it('应该显示空状态', async () => {
- enterpriseOrderClient['video-statistics'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ total: 0, stats: [] })
- })
- enterpriseOrderClient['company-videos'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ data: [] })
- })
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('暂无视频数据')).toBeInTheDocument()
- })
- })
- })
- describe('视频分类筛选', () => {
- it('应该显示所有分类标签及计数', async () => {
- const mockStatistics = {
- total: 12,
- stats: [
- { assetType: 'salary_video', count: 4 },
- { assetType: 'tax_video', count: 3 },
- { assetType: 'checkin_video', count: 5 }
- ]
- }
- enterpriseOrderClient['video-statistics'].$get.mockResolvedValue({
- ok: true,
- json: async () => mockStatistics
- })
- enterpriseOrderClient['company-videos'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ data: [] })
- })
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('全部视频 (12)')).toBeInTheDocument()
- expect(screen.getByText('工资视频 (4)')).toBeInTheDocument()
- expect(screen.getByText('个税视频 (3)')).toBeInTheDocument()
- expect(screen.getByText('打卡视频 (5)')).toBeInTheDocument()
- })
- })
- it('应该能够切换视频分类', async () => {
- enterpriseOrderClient['video-statistics'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ total: 10, stats: [] })
- })
- enterpriseOrderClient['company-videos'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ data: [] })
- })
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('工资视频')).toBeInTheDocument()
- })
- fireEvent.click(screen.getByText('工资视频'))
- await waitFor(() => {
- expect(enterpriseOrderClient['company-videos'].$get).toHaveBeenCalledWith(
- expect.objectContaining({
- query: { assetType: 'salary_video' }
- })
- )
- })
- })
- })
- describe('视频操作', () => {
- beforeEach(async () => {
- const mockVideos = {
- data: [
- {
- id: '1',
- talentName: '张明',
- assetType: 'salary_video',
- status: 'verified',
- fileSize: 12345678,
- duration: 45,
- description: '工资发放确认视频',
- createdAt: '2023-11-25T10:00:00Z',
- fileUrl: 'https://example.com/video1.mp4'
- }
- ]
- }
- enterpriseOrderClient['video-statistics'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ total: 1, stats: [] })
- })
- enterpriseOrderClient['company-videos'].$get.mockResolvedValue({
- ok: true,
- json: async () => mockVideos
- })
- })
- it('应该能够选择视频进行批量下载', async () => {
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('张明 - 工资视频')).toBeInTheDocument()
- })
- // 点击选择按钮
- const selectButtons = screen.getAllByText('选择')
- fireEvent.click(selectButtons[0])
- await waitFor(() => {
- expect(screen.getByText('已选择')).toBeInTheDocument()
- expect(screen.getByText('批量下载 (1)')).toBeInTheDocument()
- })
- })
- it('应该能够执行批量下载', async () => {
- enterpriseOrderClient['batch-download'].$post.mockResolvedValue({
- ok: true,
- json: async () => ({ success: true })
- })
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('张明 - 工资视频')).toBeInTheDocument()
- })
- // 先选择视频
- const selectButtons = screen.getAllByText('选择')
- fireEvent.click(selectButtons[0])
- // 再点击批量下载
- fireEvent.click(screen.getByText(/批量下载/))
- await waitFor(() => {
- expect(enterpriseOrderClient['batch-download'].$post).toHaveBeenCalled()
- expect(Taro.showToast).toHaveBeenCalledWith(
- expect.objectContaining({ title: '下载任务已创建' })
- )
- })
- })
- it('应该在批量下载前提示选择视频', async () => {
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('张明 - 工资视频')).toBeInTheDocument()
- })
- // 直接点击批量下载,不选择视频
- fireEvent.click(screen.getByText(/批量下载/))
- expect(Taro.showToast).toHaveBeenCalledWith(
- expect.objectContaining({ title: '请先选择要下载的视频' })
- )
- })
- it('应该能够播放视频', async () => {
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('张明 - 工资视频')).toBeInTheDocument()
- })
- const playButton = screen.getByText('播放')
- fireEvent.click(playButton)
- expect(Taro.showModal).toHaveBeenCalledWith(
- expect.objectContaining({
- title: '播放视频',
- content: '即将播放:张明 - 工资视频'
- })
- )
- })
- it('应该能够下载单个视频', async () => {
- enterpriseOrderClient['batch-download'].$post.mockResolvedValue({
- ok: true,
- json: async () => ({ success: true })
- })
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('张明 - 工资视频')).toBeInTheDocument()
- })
- const downloadButton = screen.getByText('下载')
- fireEvent.click(downloadButton)
- await waitFor(() => {
- expect(enterpriseOrderClient['batch-download'].$post).toHaveBeenCalled()
- expect(Taro.showToast).toHaveBeenCalledWith(
- expect.objectContaining({ title: '下载成功' })
- )
- })
- })
- it('应该能够分享视频', async () => {
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- expect(screen.getByText('张明 - 工资视频')).toBeInTheDocument()
- })
- const shareButton = screen.getByText('分享')
- fireEvent.click(shareButton)
- expect(Taro.showShareMenu).toHaveBeenCalledWith(
- expect.objectContaining({ withShareTicket: true })
- )
- })
- })
- describe('组件集成', () => {
- it('应该正确集成Navbar组件', async () => {
- enterpriseOrderClient['video-statistics'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ total: 0, stats: [] })
- })
- enterpriseOrderClient['company-videos'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ data: [] })
- })
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- const navbar = screen.getByTestId('navbar')
- expect(navbar).toBeInTheDocument()
- expect(navbar).toHaveTextContent('视频管理')
- })
- })
- it('应该正确集成TabBarLayout并激活settings标签', async () => {
- enterpriseOrderClient['video-statistics'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ total: 0, stats: [] })
- })
- enterpriseOrderClient['company-videos'].$get.mockResolvedValue({
- ok: true,
- json: async () => ({ data: [] })
- })
- renderWithQueryClient(<VideoManagement />)
- await waitFor(() => {
- const layout = screen.getByTestId('tab-bar-layout')
- expect(layout).toHaveAttribute('data-active-key', 'settings')
- })
- })
- })
- })
|