/
mock-fetch.ts
130 lines (117 loc) · 3.11 KB
/
mock-fetch.ts
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
/* global Headers */
// See test/modules/carto/responseToJson for details for creating test data
import binaryTileData from './data/binaryTile.json';
const BINARY_TILE = new Uint8Array(binaryTileData).buffer;
const fetch = globalThis.fetch;
type MockFetchCall = {url: string; headers: Record<string, unknown>};
export const TILEJSON_RESPONSE = {
tilejson: '2.2.0',
tiles: ['https://xyz.com/{z}/{x}/{y}?formatTiles=binary'],
tilestats: {
layers: [
{
attributes: [
{attribute: 'population', type: 'integer'},
{attribute: 'category', type: 'string'}
]
}
]
}
};
export const GEOJSON_RESPONSE = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: [-6.7531585693359375, 37.57505900514996]
}
}
]
};
export const TILESTATS_RESPONSE = {
attribute: 'population',
avg: 10,
min: 1,
max: 20,
quantiles: [],
sum: 100,
type: 'Number'
};
const createDefaultResponse = (
url: string,
headers: HeadersInit,
cacheKey?: string
): Promise<unknown> => {
return Promise.resolve({
json: () => {
if (url.indexOf('format=tilejson') !== -1) {
return TILEJSON_RESPONSE;
}
if (url.indexOf('format=geojson') !== -1) {
return GEOJSON_RESPONSE;
}
if (url.indexOf('tileset') !== -1) {
return {
tilejson: {
url: [`https://xyz.com?format=tilejson&cache=${cacheKey}`]
}
};
}
if (url.indexOf('stats') !== -1) {
return TILESTATS_RESPONSE;
}
if (url.indexOf('query') !== -1 || url.indexOf('table')) {
return {
tilejson: {
url: [`https://xyz.com?format=tilejson&cache=${cacheKey}`]
},
geojson: {
url: [`https://xyz.com?format=geojson&cache=${cacheKey}`]
}
};
}
return null;
},
arrayBuffer: () => BINARY_TILE,
text: () => null, // Required to get loaders.gl to use arrayBuffer()
ok: true,
url,
headers: new Headers(headers)
});
};
async function setupMockFetchMapsV3(
responseFunc = createDefaultResponse,
cacheKey = btoa(Math.random().toFixed(4))
): Promise<MockFetchCall[]> {
const calls: MockFetchCall[] = [];
const mockFetch = (url: string, {headers}) => {
calls.push({url, headers});
if (url.indexOf('formatTiles=binary') !== -1) {
headers = {...headers, 'Content-Type': 'application/vnd.carto-vector-tile'};
}
return responseFunc(url, headers, cacheKey);
};
globalThis.fetch = mockFetch as unknown as typeof fetch;
return calls;
}
function teardownMockFetchMaps() {
globalThis.fetch = fetch;
}
export async function withMockFetchMapsV3(
testFunc: (calls: {url: string; headers: Record<string, unknown>}[]) => Promise<void>,
responseFunc: (
url: string,
headers: HeadersInit,
cacheKey?: string
) => Promise<unknown> = createDefaultResponse
): Promise<void> {
try {
const calls = await setupMockFetchMapsV3(responseFunc);
await testFunc(calls);
} finally {
teardownMockFetchMaps();
}
}