Repository navigation
Expand file tree
/
Copy pathObjectMap.test.tsx
More file actions
168 lines (147 loc) · 6.05 KB
/
Copy pathObjectMap.test.tsx
File metadata and controls
168 lines (147 loc) · 6.05 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
162
163
164
165
166
167
168
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { ObjectMap } from './ObjectMap';
import { DataSource } from '@object-ui/types';
import './__tests__/webgl2Available';
// Mock react-map-gl/maplibre components to check they are receiving the right props/children
// without relying on real WebGL canvas rendering.
vi.mock('react-map-gl/maplibre', () => ({
default: ({ children }: any) => <div aria-label="Map">{children}</div>,
Map: ({ children }: any) => <div aria-label="Map">{children}</div>,
NavigationControl: () => <div data-testid="nav-control" />,
Marker: ({ children, longitude, latitude, onClick }: any) => (
<div
data-testid="map-marker"
data-lat={latitude}
data-lng={longitude}
onClick={onClick}
>
{children}
</div>
),
Popup: ({ children }: any) => <div data-testid="map-popup">{children}</div>,
}));
const mockData = [
{ id: '1', name: 'Loc 1', latitude: 40, longitude: -74 },
{ id: '2', name: 'Loc 2', latitude: 41, longitude: -75 },
];
const mockDataSource: DataSource = {
find: vi.fn(),
findOne: vi.fn(),
create: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
getObjectSchema: vi.fn(),
};
describe('ObjectMap', () => {
it('renders with static value provider', async () => {
const schema: any = {
type: 'map',
map: {
latitudeField: 'latitude',
longitudeField: 'longitude',
titleField: 'name'
},
data: {
provider: 'value',
items: mockData
}
};
render(<ObjectMap schema={schema} />);
// Wait for loading to finish
await waitFor(() => {
expect(screen.queryByText('Loading map...')).toBeNull();
});
// Check if map is rendered
expect(screen.getByLabelText('Map')).toBeDefined();
// Check if markers are rendered using testid from our mock
await waitFor(() => {
const markers = screen.getAllByTestId('map-marker');
expect(markers).toHaveLength(2);
// Verify coordinates of first marker
expect(markers[0]).toHaveAttribute('data-lat', '40');
expect(markers[0]).toHaveAttribute('data-lng', '-74');
});
// Check content inside marker (the emoji)
expect(screen.getAllByText('📍')).toHaveLength(2);
});
it('fetches data with object provider', async () => {
(mockDataSource.find as any).mockResolvedValue({ data: mockData });
const schema: any = {
type: 'map',
map: {
latitudeField: 'latitude',
longitudeField: 'longitude',
titleField: 'name'
},
data: {
provider: 'object',
object: 'locations'
}
};
render(<ObjectMap schema={schema} dataSource={mockDataSource} />);
// Wait for loading to finish
await waitFor(() => {
expect(screen.queryByText('Loading map...')).toBeNull();
});
// Verify dataSource.find was called
expect(mockDataSource.find).toHaveBeenCalledWith('locations', expect.anything());
// Check markers
await waitFor(() => {
const markers = screen.getAllByTestId('map-marker');
expect(markers).toHaveLength(2);
});
});
});
/**
* A refused read draws the calm no-access state, not the raw error.
*
* A reader who may open the page but not read the map's object used to see
* the transport's own string ("Error: ApiDataSource: HTTP 403 Forbidden — ...").
* The map now classifies a failed load with the shared `classifyLoadError` and,
* for a `forbidden` read, draws the shared `list.loadErrorForbidden*` sentence
* as a neutral `role="status"` panel. Every other failure keeps the existing
* error screen (CONTROL).
*/
describe('ObjectMap: a refused read draws the no-access state', () => {
const schema: any = {
type: 'map',
map: { latitudeField: 'latitude', longitudeField: 'longitude', titleField: 'name' },
data: { provider: 'object', object: 'locations' },
};
function failingSource(err: Error): DataSource {
return {
find: vi.fn(async () => { throw err; }),
findOne: vi.fn(),
create: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
getObjectSchema: vi.fn(),
} as unknown as DataSource;
}
it('a 403 read draws the no-access state, with none of the server text', async () => {
const err = new Error(
'ApiDataSource: HTTP 403 Forbidden — {"error":"PERMISSION_DENIED: no read access to locations"}',
) as Error & { httpStatus?: number; code?: string };
err.httpStatus = 403;
err.code = 'PERMISSION_DENIED';
const { container } = render(<ObjectMap schema={schema} dataSource={failingSource(err)} />);
const panel = await screen.findByTestId('map-no-access');
expect(panel.getAttribute('role')).toBe('status');
expect(panel.textContent).toContain('You don’t have access');
expect(panel.textContent).toContain('You don’t have permission to view these records.');
const text = container.textContent ?? '';
expect(text).not.toContain('Error');
expect(text).not.toContain('PERMISSION_DENIED');
expect(text).not.toContain('HTTP 403');
expect(screen.queryByLabelText('Map')).toBeNull();
});
it('CONTROL: a 500 keeps the existing error screen', async () => {
const err = new Error('ApiDataSource: HTTP 500 Internal Server Error') as Error & { httpStatus?: number };
err.httpStatus = 500;
render(<ObjectMap schema={schema} dataSource={failingSource(err)} />);
expect(await screen.findByText('Error: ApiDataSource: HTTP 500 Internal Server Error')).toBeTruthy();
expect(screen.queryByTestId('map-no-access')).toBeNull();
});
});