-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathHeaderCrop.test.tsx
More file actions
144 lines (114 loc) · 4.26 KB
/
HeaderCrop.test.tsx
File metadata and controls
144 lines (114 loc) · 4.26 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
import { fireEvent, mockFunction, render, screen, waitFor } from '@app/test-utils'
import { beforeAll, describe, expect, it, vi } from 'vitest'
import { makeMockIntersectionObserver } from '../../../../../test/mock/makeMockIntersectionObserver'
import { CropComponent } from './HeaderCrop'
makeMockIntersectionObserver()
const mockHandleCancel = vi.fn()
const mockSetDataURL = vi.fn()
// Create a simple 100x100 image for testing
const createTestImage = () => {
const canvas = document.createElement('canvas')
canvas.width = 100
canvas.height = 100
const ctx = canvas.getContext('2d')!
ctx.fillStyle = 'red'
ctx.fillRect(0, 0, 100, 100)
return new Promise<File>((resolve) => {
canvas.toBlob((blob) => {
if (blob) {
resolve(new File([blob], 'test.png', { type: 'image/png' }))
}
})
})
}
describe('<CropComponent /> - Zoom Functionality', () => {
beforeAll(() => {
URL.createObjectURL = vi.fn(() => 'https://localhost/test.png')
})
it('renders the crop component with zoom slider', async () => {
const mockFile = await createTestImage()
render(
<CropComponent header={mockFile} handleCancel={mockHandleCancel} setDataURL={mockSetDataURL} />
)
expect(screen.getByTestId('edit-image-container')).toBeVisible()
// Check if slider is present
const slider = screen.getByLabelText('zoom')
expect(slider).toBeInTheDocument()
expect(slider).toHaveAttribute('min', '100')
expect(slider).toHaveAttribute('max', '200')
})
it('changes zoom value when slider is moved', async () => {
const mockFile = await createTestImage()
render(
<CropComponent header={mockFile} handleCancel={mockHandleCancel} setDataURL={mockSetDataURL} />
)
const slider = screen.getByLabelText('zoom') as HTMLInputElement
// Initial value should be 100
expect(slider.value).toBe('100')
// Change zoom to 150
fireEvent.change(slider, { target: { value: '150' } })
await waitFor(() => {
expect(slider.value).toBe('150')
})
})
it('handles zoom to maximum value', async () => {
const mockFile = await createTestImage()
render(
<CropComponent header={mockFile} handleCancel={mockHandleCancel} setDataURL={mockSetDataURL} />
)
const slider = screen.getByLabelText('zoom') as HTMLInputElement
// Change zoom to max (200)
fireEvent.change(slider, { target: { value: '200' } })
await waitFor(() => {
expect(slider.value).toBe('200')
})
})
it('calls handleCancel when cancel button is clicked', async () => {
const mockFile = await createTestImage()
render(
<CropComponent header={mockFile} handleCancel={mockHandleCancel} setDataURL={mockSetDataURL} />
)
const cancelButton = screen.getByTestId('header-cancel-button')
fireEvent.click(cancelButton)
expect(mockHandleCancel).toHaveBeenCalled()
})
it('calls setDataURL when continue button is clicked', async () => {
const mockFile = await createTestImage()
mockSetDataURL.mockClear()
render(
<CropComponent header={mockFile} handleCancel={mockHandleCancel} setDataURL={mockSetDataURL} />
)
// Wait for canvas to be in the document
await waitFor(() => {
const canvas = document.querySelector('canvas')
expect(canvas).toBeInTheDocument()
}, { timeout: 3000 })
const continueButton = screen.getByTestId('continue-button')
fireEvent.click(continueButton)
await waitFor(() => {
expect(mockSetDataURL).toHaveBeenCalled()
const dataURL = mockSetDataURL.mock.calls[0][0]
expect(dataURL).toMatch(/^data:image\/jpeg;base64,/)
})
})
it('displays 3:1 aspect ratio labels', async () => {
const mockFile = await createTestImage()
render(
<CropComponent header={mockFile} handleCancel={mockHandleCancel} setDataURL={mockSetDataURL} />
)
await waitFor(() => {
const labels = screen.getAllByText(/3:1/i)
expect(labels).toHaveLength(2)
})
})
it('displays 6:1 aspect ratio labels', async () => {
const mockFile = await createTestImage()
render(
<CropComponent header={mockFile} handleCancel={mockHandleCancel} setDataURL={mockSetDataURL} />
)
await waitFor(() => {
const labels = screen.getAllByText(/6:1/i)
expect(labels).toHaveLength(2)
})
})
})