|
| 1 | +import { fireEvent, screen, waitFor } from '@testing-library/react'; |
| 2 | +import { render } from '@tests/mocks/test-utils'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { GeoJsonOverlay } from '@/screens/MapScreen/GeoJsonOverlay'; |
| 6 | + |
| 7 | +const { uuid } = vi.hoisted(() => { |
| 8 | + let n = 0; |
| 9 | + return { uuid: () => `uuid-${n++}` }; |
| 10 | +}); |
| 11 | +vi.mock('@/lib/uuid', () => ({ uuid })); |
| 12 | + |
| 13 | +const dropGeoJson = (zone: HTMLElement, files: File[]) => { |
| 14 | + fireEvent.drop(zone, { |
| 15 | + dataTransfer: { files, items: [], types: ['Files'] }, |
| 16 | + }); |
| 17 | +}; |
| 18 | + |
| 19 | +const makeFile = (name: string, content: string, size?: number): File => { |
| 20 | + const f = new File([content], name, { type: 'application/json' }); |
| 21 | + if (size !== undefined) Object.defineProperty(f, 'size', { value: size }); |
| 22 | + return f; |
| 23 | +}; |
| 24 | + |
| 25 | +const featureCollection = JSON.stringify({ |
| 26 | + type: 'FeatureCollection', |
| 27 | + features: [ |
| 28 | + { |
| 29 | + type: 'Feature', |
| 30 | + geometry: { type: 'Point', coordinates: [0, 0] }, |
| 31 | + properties: {}, |
| 32 | + }, |
| 33 | + ], |
| 34 | +}); |
| 35 | +const feature = JSON.stringify({ |
| 36 | + type: 'Feature', |
| 37 | + geometry: { type: 'Point', coordinates: [1, 1] }, |
| 38 | + properties: {}, |
| 39 | +}); |
| 40 | +const bareGeometry = JSON.stringify({ type: 'Point', coordinates: [2, 2] }); |
| 41 | + |
| 42 | +beforeEach(() => { |
| 43 | + vi.clearAllMocks(); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('GeoJsonOverlay', () => { |
| 47 | + it('renders the dropzone with the correct label and no panel initially', () => { |
| 48 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 49 | + expect(screen.getByTestId('geojson-dropzone')).toHaveAttribute( |
| 50 | + 'aria-label', |
| 51 | + expect.stringMatching(/drop a \.geojson/i), |
| 52 | + ); |
| 53 | + expect(screen.queryByTestId('geojson-overlay-panel')).toBeNull(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('shows the active drop hint on drag enter and hides it on drag leave', () => { |
| 57 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 58 | + const zone = screen.getByTestId('geojson-dropzone'); |
| 59 | + fireEvent.dragEnter(zone); |
| 60 | + expect( |
| 61 | + screen.getByText(/drop the file to add the overlay/i), |
| 62 | + ).toBeInTheDocument(); |
| 63 | + expect(zone.className).toContain('border-dashed'); |
| 64 | + fireEvent.dragLeave(zone); |
| 65 | + expect(screen.queryByText(/drop the file to add the overlay/i)).toBeNull(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('adds an overlay from a valid FeatureCollection file', async () => { |
| 69 | + const onOverlaysChange = vi.fn(); |
| 70 | + render(<GeoJsonOverlay onOverlaysChange={onOverlaysChange} />); |
| 71 | + dropGeoJson(screen.getByTestId('geojson-dropzone'), [ |
| 72 | + makeFile('layer.geojson', featureCollection), |
| 73 | + ]); |
| 74 | + await waitFor(() => { |
| 75 | + expect(screen.getByTestId('geojson-overlay-row')).toBeInTheDocument(); |
| 76 | + }); |
| 77 | + expect(onOverlaysChange).toHaveBeenCalledWith( |
| 78 | + expect.arrayContaining([ |
| 79 | + expect.objectContaining({ name: 'layer.geojson' }), |
| 80 | + ]), |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('adds an overlay from a single Feature file', async () => { |
| 85 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 86 | + dropGeoJson(screen.getByTestId('geojson-dropzone'), [ |
| 87 | + makeFile('feat.geojson', feature), |
| 88 | + ]); |
| 89 | + await waitFor(() => { |
| 90 | + expect(screen.getByTestId('geojson-overlay-row')).toBeInTheDocument(); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('adds an overlay from a bare Geometry object', async () => { |
| 95 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 96 | + dropGeoJson(screen.getByTestId('geojson-dropzone'), [ |
| 97 | + makeFile('geom.geojson', bareGeometry), |
| 98 | + ]); |
| 99 | + await waitFor(() => { |
| 100 | + expect(screen.getByTestId('geojson-overlay-row')).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('rejects an invalid GeoJSON object with an alert and no overlay', async () => { |
| 105 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 106 | + dropGeoJson(screen.getByTestId('geojson-dropzone'), [ |
| 107 | + makeFile('bad.geojson', JSON.stringify({ foo: 'bar' })), |
| 108 | + ]); |
| 109 | + await waitFor(() => { |
| 110 | + expect(screen.getByRole('alert')).toHaveTextContent(/not valid geojson/i); |
| 111 | + }); |
| 112 | + expect(screen.queryByTestId('geojson-overlay-row')).toBeNull(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('rejects a non-geojson filename extension', async () => { |
| 116 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 117 | + dropGeoJson(screen.getByTestId('geojson-dropzone'), [ |
| 118 | + makeFile('layer.txt', featureCollection), |
| 119 | + ]); |
| 120 | + await waitFor(() => { |
| 121 | + expect(screen.getByRole('alert')).toHaveTextContent(/not valid geojson/i); |
| 122 | + }); |
| 123 | + expect(screen.queryByTestId('geojson-overlay-row')).toBeNull(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('rejects unparseable JSON content', async () => { |
| 127 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 128 | + dropGeoJson(screen.getByTestId('geojson-dropzone'), [ |
| 129 | + makeFile('broken.geojson', 'not json'), |
| 130 | + ]); |
| 131 | + await waitFor(() => { |
| 132 | + expect(screen.getByRole('alert')).toHaveTextContent(/not valid geojson/i); |
| 133 | + }); |
| 134 | + expect(screen.queryByTestId('geojson-overlay-row')).toBeNull(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('warns when a large file is dropped', async () => { |
| 138 | + const big = makeFile('big.geojson', featureCollection, 6 * 1024 * 1024); |
| 139 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 140 | + dropGeoJson(screen.getByTestId('geojson-dropzone'), [big]); |
| 141 | + await waitFor(() => { |
| 142 | + expect(screen.getByText(/large files may slow/i)).toBeInTheDocument(); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it('toggles an overlay visibility', async () => { |
| 147 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 148 | + dropGeoJson(screen.getByTestId('geojson-dropzone'), [ |
| 149 | + makeFile('layer.geojson', featureCollection), |
| 150 | + ]); |
| 151 | + await waitFor(() => { |
| 152 | + expect(screen.getByTestId('geojson-overlay-row')).toBeInTheDocument(); |
| 153 | + }); |
| 154 | + const toggle = screen.getByLabelText(/toggle overlay visibility/i); |
| 155 | + expect(toggle).toHaveAttribute('aria-pressed', 'true'); |
| 156 | + fireEvent.click(toggle); |
| 157 | + expect(screen.getByLabelText(/toggle overlay visibility/i)).toHaveAttribute( |
| 158 | + 'aria-pressed', |
| 159 | + 'false', |
| 160 | + ); |
| 161 | + }); |
| 162 | + |
| 163 | + it('removes an overlay', async () => { |
| 164 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 165 | + dropGeoJson(screen.getByTestId('geojson-dropzone'), [ |
| 166 | + makeFile('layer.geojson', featureCollection), |
| 167 | + ]); |
| 168 | + await waitFor(() => { |
| 169 | + expect(screen.getByTestId('geojson-overlay-row')).toBeInTheDocument(); |
| 170 | + }); |
| 171 | + fireEvent.click(screen.getByLabelText(/remove overlay/i)); |
| 172 | + expect(screen.queryByTestId('geojson-overlay-row')).toBeNull(); |
| 173 | + }); |
| 174 | + |
| 175 | + it('clears all overlays', async () => { |
| 176 | + render(<GeoJsonOverlay onOverlaysChange={vi.fn()} />); |
| 177 | + const zone = screen.getByTestId('geojson-dropzone'); |
| 178 | + dropGeoJson(zone, [makeFile('a.geojson', featureCollection)]); |
| 179 | + dropGeoJson(zone, [makeFile('b.geojson', feature)]); |
| 180 | + await waitFor(() => { |
| 181 | + expect(screen.getAllByTestId('geojson-overlay-row')).toHaveLength(2); |
| 182 | + }); |
| 183 | + fireEvent.click(screen.getByText(/clear all overlays/i)); |
| 184 | + await waitFor(() => { |
| 185 | + expect(screen.queryByTestId('geojson-overlay-panel')).toBeNull(); |
| 186 | + }); |
| 187 | + expect(screen.queryByTestId('geojson-overlay-row')).toBeNull(); |
| 188 | + }); |
| 189 | +}); |
0 commit comments