/
map.spec.js
193 lines (166 loc) · 4.82 KB
/
map.spec.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* global setTimeout, clearTimeout */
import MapGL, {InteractiveMap} from 'react-map-gl';
import React from 'react';
import ReactTestUtils from 'react-test-renderer/shallow';
import ReactTestRenderer from 'react-test-renderer';
import sinon from 'sinon';
import test from 'tape-catch';
import Immutable from 'immutable';
const mapboxApiAccessToken = process.env._MapboxAccessToken_; // eslint-disable-line
const defaultProps = {
width: 500,
height: 500,
longitude: -122,
latitude: 37,
zoom: 14,
mapboxApiAccessToken
};
const minimalStyle = {
version: 8,
name: 'Minimal',
sources: {
point: {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-122, 37]
}
}
}
},
layers: [
{
id: 'point',
source: 'point',
type: 'circle',
paint: {
'circle-radius': 10,
'circle-color': '#000000'
}
}
]
};
const TEST_CASES = [
{
title: 'Mapbox data without token',
props: Object.assign({}, defaultProps, {
mapboxApiAccessToken: ''
}),
shouldLoad: false
},
{
title: 'Mapbox data with token (string)',
props: Object.assign({}, defaultProps, {
mapStyle: 'mapbox://styles/mapbox/dark-v9'
}),
shouldLoad: true
},
{
title: 'non-Mapbox data without token (JSON)',
props: Object.assign({}, defaultProps, {
mapStyle: minimalStyle,
mapboxApiAccessToken: ''
}),
shouldLoad: true
},
{
title: 'non-Mapbox data without token (Immutable)',
props: Object.assign({}, defaultProps, {
mapStyle: Immutable.fromJS(minimalStyle)
}),
shouldLoad: true
}
];
test('InteractiveMap#default export', t => {
t.ok(MapGL, 'InteractiveMap is defined');
const map = <MapGL {...defaultProps} />;
const result = ReactTestUtils.createRenderer().render(map);
t.ok(result, 'InteractiveMap rendered');
t.end();
});
test('InteractiveMap#named export', t => {
t.ok(InteractiveMap, 'InteractiveMap is defined');
const map = <InteractiveMap {...defaultProps} />;
const result = ReactTestUtils.createRenderer().render(map);
t.ok(result, 'InteractiveMap rendered');
t.end();
});
TEST_CASES.forEach(testCase => {
test(`InteractiveMap#load ${testCase.title}`, t => {
let result = null;
let timer = null;
function checkRenderResult() {
// The "Mapbox API access token required" warning is rendered with a <h3 />
const warning = result.root.findAllByType('h3');
if (testCase.shouldLoad) {
t.notOk(warning.length, "shouldn't show warning");
} else {
t.ok(warning.length, 'should show warning');
}
// Unmount the component to avoid creating too many maps
result.unmount();
}
function onLoad() {
if (testCase.shouldLoad) {
t.pass('onLoad is called');
} else {
t.fail('onLoad should not be called');
}
clearTimeout(timer);
checkRenderResult();
t.end();
}
function onTimeout() {
if (testCase.shouldLoad) {
t.fail("onLoad wasn't called");
} else {
t.pass("onLoad wasn't called");
}
checkRenderResult();
t.end();
}
const props = Object.assign({}, testCase.props, {onLoad});
const map = <InteractiveMap {...props} />;
result = ReactTestRenderer.create(map);
t.ok(result, 'InteractiveMap rendered');
if (!InteractiveMap.supported()) {
t.ok('onLoad not called since InteractiveMap.supported() false');
t.end();
} else {
timer = setTimeout(onTimeout, 5000);
}
});
});
// children are no longer rendered on first pass due to auto sizer
test.skip('Interactive map renders children on first render', t => {
const childComponent = sinon.spy(() => null);
const child = <childComponent />;
const map = <InteractiveMap {...defaultProps}>{child}</InteractiveMap>;
try {
const result = ReactTestRenderer.create(map);
// Unmount the component to avoid creating too many maps
result.unmount();
} catch (e) {
// we use try catch here as InteractiveMap fails in DidMount
// but having that render have already called this fail does not matter
// for this test
}
t.ok(childComponent.called, 'Child rendered');
t.end();
});
test('Interactive map#call transformRequest callback when provided', t => {
const transformRequest = sinon.spy(() => null);
const props = Object.assign({}, defaultProps, {transformRequest});
const map = <InteractiveMap {...props} />;
// const result = ReactTestUtils.createRenderer().render(map);
const result = ReactTestRenderer.create(map);
if (!InteractiveMap.supported()) {
t.pass('transformRequest not called since InteractiveMap.supported() false');
} else {
t.ok(transformRequest.called, 'transformRequest handler was called');
}
result.unmount();
t.end();
});