|
1 | 1 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
2 | | -import { render, screen } from '@testing-library/react'; |
| 2 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
3 | 4 |
|
4 | 5 | // Mock dependencies before importing App |
5 | 6 | vi.mock('../lib/imageUtils', () => ({ |
@@ -82,4 +83,125 @@ describe('App', () => { |
82 | 83 | render(<App />); |
83 | 84 | expect(screen.queryByText('Clear all')).not.toBeInTheDocument(); |
84 | 85 | }); |
| 86 | + |
| 87 | + // --- Theme toggle --- |
| 88 | + it('clicking Dark theme button adds dark class to document', async () => { |
| 89 | + const user = userEvent.setup(); |
| 90 | + render(<App />); |
| 91 | + |
| 92 | + await user.click(screen.getByTitle('Dark')); |
| 93 | + expect(document.documentElement.classList.contains('dark')).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it('clicking Light theme button removes dark class', async () => { |
| 97 | + const user = userEvent.setup(); |
| 98 | + document.documentElement.classList.add('dark'); |
| 99 | + render(<App />); |
| 100 | + |
| 101 | + await user.click(screen.getByTitle('Light')); |
| 102 | + expect(document.documentElement.classList.contains('dark')).toBe(false); |
| 103 | + }); |
| 104 | + |
| 105 | + // Helper to set up FileReader mock and upload mocks |
| 106 | + function setupUploadMocks() { |
| 107 | + const origFileReader = global.FileReader; |
| 108 | + |
| 109 | + class MockFileReader { |
| 110 | + result: string | null = 'data:image/png;base64,abc'; |
| 111 | + onload: (() => void) | null = null; |
| 112 | + onerror: (() => void) | null = null; |
| 113 | + readAsDataURL() { |
| 114 | + setTimeout(() => this.onload?.(), 0); |
| 115 | + } |
| 116 | + } |
| 117 | + global.FileReader = MockFileReader as any; |
| 118 | + |
| 119 | + return { |
| 120 | + restore: () => { global.FileReader = origFileReader; }, |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + async function setupMocksAndUpload() { |
| 125 | + const { processFilesForHeic } = await import('../lib/heicUtils'); |
| 126 | + const { getImageDimensions } = await import('../lib/imageUtils'); |
| 127 | + |
| 128 | + const mockFile = new File(['pixel'], 'test.png', { type: 'image/png' }); |
| 129 | + (processFilesForHeic as ReturnType<typeof vi.fn>).mockResolvedValue({ |
| 130 | + converted: [mockFile], |
| 131 | + errors: [], |
| 132 | + }); |
| 133 | + (getImageDimensions as ReturnType<typeof vi.fn>).mockResolvedValue({ width: 200, height: 100 }); |
| 134 | + |
| 135 | + return mockFile; |
| 136 | + } |
| 137 | + |
| 138 | + // --- Photo upload and stats bar --- |
| 139 | + it('shows stats bar and clear all after uploading photos', async () => { |
| 140 | + const mockFile = await setupMocksAndUpload(); |
| 141 | + const { restore } = setupUploadMocks(); |
| 142 | + |
| 143 | + render(<App />); |
| 144 | + |
| 145 | + const input = document.querySelector('input[type="file"]') as HTMLInputElement; |
| 146 | + fireEvent.change(input, { target: { files: [mockFile] } }); |
| 147 | + |
| 148 | + await waitFor(() => { |
| 149 | + expect(screen.getByText(/1 photo/)).toBeInTheDocument(); |
| 150 | + }); |
| 151 | + expect(screen.getByText('Clear all')).toBeInTheDocument(); |
| 152 | + |
| 153 | + restore(); |
| 154 | + }); |
| 155 | + |
| 156 | + // --- Clear all --- |
| 157 | + it('clicking Clear all removes all photos', async () => { |
| 158 | + const user = userEvent.setup(); |
| 159 | + const mockFile = await setupMocksAndUpload(); |
| 160 | + const { restore } = setupUploadMocks(); |
| 161 | + |
| 162 | + render(<App />); |
| 163 | + |
| 164 | + const input = document.querySelector('input[type="file"]') as HTMLInputElement; |
| 165 | + fireEvent.change(input, { target: { files: [mockFile] } }); |
| 166 | + |
| 167 | + await waitFor(() => { |
| 168 | + expect(screen.getByText(/1 photo/)).toBeInTheDocument(); |
| 169 | + }); |
| 170 | + |
| 171 | + await user.click(screen.getByText('Clear all')); |
| 172 | + expect(screen.queryByText(/1 photo/)).not.toBeInTheDocument(); |
| 173 | + expect(screen.queryByText('Clear all')).not.toBeInTheDocument(); |
| 174 | + |
| 175 | + restore(); |
| 176 | + }); |
| 177 | + |
| 178 | + // --- Process flow --- |
| 179 | + it('processing photos shows Download All button after completion', async () => { |
| 180 | + const user = userEvent.setup(); |
| 181 | + const mockFile = await setupMocksAndUpload(); |
| 182 | + const { padImageToAspectRatio } = await import('../lib/imageUtils'); |
| 183 | + (padImageToAspectRatio as ReturnType<typeof vi.fn>).mockResolvedValue('data:image/png;base64,padded'); |
| 184 | + const { restore } = setupUploadMocks(); |
| 185 | + |
| 186 | + render(<App />); |
| 187 | + |
| 188 | + // Upload a photo |
| 189 | + const input = document.querySelector('input[type="file"]') as HTMLInputElement; |
| 190 | + fireEvent.change(input, { target: { files: [mockFile] } }); |
| 191 | + |
| 192 | + await waitFor(() => { |
| 193 | + expect(screen.getByText(/1 photo/)).toBeInTheDocument(); |
| 194 | + }); |
| 195 | + |
| 196 | + // Process |
| 197 | + const processButton = screen.getByText('Process Images').closest('button')!; |
| 198 | + expect(processButton).not.toBeDisabled(); |
| 199 | + await user.click(processButton); |
| 200 | + |
| 201 | + await waitFor(() => { |
| 202 | + expect(screen.getByText('Download All as ZIP')).toBeInTheDocument(); |
| 203 | + }); |
| 204 | + |
| 205 | + restore(); |
| 206 | + }); |
85 | 207 | }); |
0 commit comments