-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.test.tsx
More file actions
161 lines (128 loc) · 5.02 KB
/
App.test.tsx
File metadata and controls
161 lines (128 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import App from './App'
// Mock URL.createObjectURL and URL.revokeObjectURL
const mockObjectURL = 'blob:mock-url'
beforeEach(() => {
global.URL.createObjectURL = vi.fn(
() => mockObjectURL
) as unknown as typeof URL.createObjectURL
global.URL.revokeObjectURL =
vi.fn() as unknown as typeof URL.revokeObjectURL
})
describe.skip('App', () => {
it('renders without crashing', () => {
const { container } = render(<App />)
expect(container).toBeInTheDocument()
})
it('initially shows upload component', () => {
render(<App />)
expect(screen.getByText(/drop your video here/i)).toBeInTheDocument()
})
it('shows error when invalid file is uploaded', async () => {
const user = userEvent.setup()
render(<App />)
const file = new File(['content'], 'document.pdf', {
type: 'application/pdf',
})
const input = screen.getByLabelText(/choose video file/i)
await user.upload(input, file)
expect(screen.getByRole('alert')).toBeInTheDocument()
expect(screen.getByText(/invalid file type/i)).toBeInTheDocument()
})
it('shows video preview when valid file is uploaded', async () => {
const user = userEvent.setup()
render(<App />)
const file = new File(['video content'], 'test.mp4', {
type: 'video/mp4',
})
// Create a mock video element for metadata extraction
const mockVideo = document.createElement('video')
Object.defineProperty(mockVideo, 'duration', { value: 60 })
Object.defineProperty(mockVideo, 'videoWidth', { value: 1280 })
Object.defineProperty(mockVideo, 'videoHeight', { value: 720 })
const createElementSpy = vi.spyOn(document, 'createElement')
createElementSpy.mockImplementation((tag: string) => {
if (tag === 'video') {
// Simulate loadedmetadata event
setTimeout(() => {
mockVideo.onloadedmetadata?.({} as Event)
}, 0)
return mockVideo
}
return document.createElement(tag)
})
const input = screen.getByLabelText(/choose video file/i)
await user.upload(input, file)
await waitFor(() => {
expect(screen.getByText('Video Preview')).toBeInTheDocument()
})
expect(screen.getByText('test.mp4')).toBeInTheDocument()
createElementSpy.mockRestore()
})
it('returns to upload state when remove button is clicked', async () => {
const user = userEvent.setup()
render(<App />)
const file = new File(['video content'], 'test.mp4', {
type: 'video/mp4',
})
// Mock video metadata extraction
const mockVideo = document.createElement('video')
Object.defineProperty(mockVideo, 'duration', { value: 60 })
Object.defineProperty(mockVideo, 'videoWidth', { value: 1280 })
Object.defineProperty(mockVideo, 'videoHeight', { value: 720 })
const createElementSpy = vi.spyOn(document, 'createElement')
createElementSpy.mockImplementation((tag: string) => {
if (tag === 'video') {
setTimeout(() => {
mockVideo.onloadedmetadata?.({} as Event)
}, 0)
return mockVideo
}
return document.createElement(tag)
})
const input = screen.getByLabelText(/choose video file/i)
await user.upload(input, file)
await waitFor(() => {
expect(screen.getByText('Video Preview')).toBeInTheDocument()
})
const removeButton = screen.getByRole('button', { name: /remove video/i })
await user.click(removeButton)
expect(screen.getByText(/drop your video here/i)).toBeInTheDocument()
expect(screen.queryByText('Video Preview')).not.toBeInTheDocument()
createElementSpy.mockRestore()
})
it('clears error when valid file is uploaded after invalid file', async () => {
const user = userEvent.setup()
render(<App />)
// Upload invalid file
const invalidFile = new File(['content'], 'document.pdf', {
type: 'application/pdf',
})
const input = screen.getByLabelText(/choose video file/i)
await user.upload(input, invalidFile)
expect(screen.getByText(/invalid file type/i)).toBeInTheDocument()
// Upload valid file
const validFile = new File(['video'], 'test.mp4', { type: 'video/mp4' })
const mockVideo = document.createElement('video')
Object.defineProperty(mockVideo, 'duration', { value: 60 })
Object.defineProperty(mockVideo, 'videoWidth', { value: 1280 })
Object.defineProperty(mockVideo, 'videoHeight', { value: 720 })
const createElementSpy = vi.spyOn(document, 'createElement')
createElementSpy.mockImplementation((tag: string) => {
if (tag === 'video') {
setTimeout(() => {
mockVideo.onloadedmetadata?.({} as Event)
}, 0)
return mockVideo
}
return document.createElement(tag)
})
await user.upload(input, validFile)
await waitFor(() => {
expect(screen.queryByText(/invalid file type/i)).not.toBeInTheDocument()
})
createElementSpy.mockRestore()
})
})