dify/web/app/components/base/markdown-blocks/video-block.spec.tsx
Poojan 0ac09127c7
test: add unit tests for base components-part-4 (#32452)
Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com>
2026-02-25 17:36:58 +08:00

85 lines
2.1 KiB
TypeScript

import { render } from '@testing-library/react'
import * as React from 'react'
import { describe, expect, it } from 'vitest'
import VideoGallery from '../video-gallery'
import VideoBlock from './video-block'
type ChildNode = {
properties?: {
src?: string
}
}
type BlockNode = {
children: ChildNode[]
properties?: {
src?: string
}
}
describe('VideoBlock', () => {
it('renders multiple video sources from node.children', () => {
const node: BlockNode = {
children: [
{ properties: { src: 'a.mp4' } },
{ properties: { src: 'b.mp4' } },
],
}
render(<VideoBlock node={node} />)
const video = document.querySelector('video')
expect(video).toBeTruthy()
const sources = document.querySelectorAll('source')
expect(sources).toHaveLength(2)
expect(sources[0]).toHaveAttribute('src', 'a.mp4')
expect(sources[1]).toHaveAttribute('src', 'b.mp4')
})
it('renders single video from node.properties.src when no children srcs', () => {
const node: BlockNode = {
children: [],
properties: { src: 'single.mp4' },
}
render(<VideoBlock node={node} />)
const sources = document.querySelectorAll('source')
expect(sources).toHaveLength(1)
expect(sources[0]).toHaveAttribute('src', 'single.mp4')
})
it('returns null when no sources exist', () => {
const node: BlockNode = {
children: [],
properties: {},
}
const { container } = render(<VideoBlock node={node} />)
expect(container.innerHTML).toBe('')
})
it('has displayName set', () => {
expect(VideoBlock.displayName).toBe('VideoBlock')
})
})
describe('VideoGallery', () => {
it('returns null when srcs are empty or invalid', () => {
const { container } = render(<VideoGallery srcs={['', '']} />)
expect(container.innerHTML).toBe('')
})
it('renders video when valid srcs provided', () => {
render(<VideoGallery srcs={['ok.mp4', 'also.mp4']} />)
const sources = document.querySelectorAll('source')
expect(sources).toHaveLength(2)
expect(sources[0]).toHaveAttribute('src', 'ok.mp4')
expect(sources[1]).toHaveAttribute('src', 'also.mp4')
})
})