-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.test.js
240 lines (197 loc) · 6.04 KB
/
index.test.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* eslint-disable no-console */
import React from 'react';
import { mount } from 'enzyme';
import MapGL, { Source, Layer } from '../..';
test('Layer#render', () => {
const handler = jest.fn();
const data = { type: 'FeatureCollection', features: [] };
const wrapper = mount(
<MapGL latitude={0} longitude={0} zoom={0}>
<Source id="test" type="geojson" data={data} />
<Layer
id="test"
type="circle"
source="test"
onClick={handler}
onHover={handler}
onEnter={handler}
onLeave={handler}
/>
</MapGL>
);
expect(wrapper.find('Layer').exists()).toBe(true);
expect(handler).toHaveBeenCalledTimes(4);
wrapper.unmount();
expect(wrapper.find('Layer').exists()).toBe(false);
});
test('Layer#unmount', () => {
const handler = jest.fn();
const data = { type: 'FeatureCollection', features: [] };
const wrapper = mount(
<MapGL latitude={0} longitude={0} zoom={0}>
<Source id="test" type="geojson" data={data} />
<Layer
id="test"
type="circle"
source="test"
onClick={handler}
onHover={handler}
onEnter={handler}
onLeave={handler}
/>
</MapGL>
);
const map = wrapper.instance().getMap();
expect(map.getLayer('test')).toBeTruthy();
wrapper.setProps({
children: [<Source id="test" type="geojson" data={data} />]
});
expect(map.getLayer('test')).toBeFalsy();
});
test('update id', () => {
const data = { type: 'FeatureCollection', features: [] };
const wrapper = mount(
<MapGL latitude={0} longitude={0} zoom={0}>
<Source id="test" type="geojson" data={data} />
<Layer id="test1" type="circle" source="test" />
</MapGL>
);
const map = wrapper.instance().getMap();
expect(map.getLayer('test1')).toBeTruthy();
wrapper.setProps({
children: [
<Source id="test" type="geojson" data={data} />,
<Layer id="test2" type="circle" source="test" />
]
});
expect(map.getLayer('test1')).toBeFalsy();
expect(map.getLayer('test2')).toBeTruthy();
});
test('update type', () => {
const data = { type: 'FeatureCollection', features: [] };
const wrapper = mount(
<MapGL latitude={0} longitude={0} zoom={0}>
<Source id="test" type="geojson" data={data} />
<Layer id="test" type="circle" source="test" />
</MapGL>
);
const map = wrapper.instance().getMap();
expect(map.getLayer('test').type).toEqual('circle');
wrapper.setProps({
children: [
<Source id="test" type="geojson" data={data} />,
<Layer id="test" type="line" source="test" />
]
});
expect(map.getLayer('test').type).toEqual('line');
});
test('update source and source-layer', () => {
const data = { type: 'FeatureCollection', features: [] };
const wrapper = mount(
<MapGL latitude={0} longitude={0} zoom={0}>
<Source id="test1" type="geojson" data={data} />
<Layer id="test" type="circle" source="test1" source-layer="test1" />
</MapGL>
);
const map = wrapper.instance().getMap();
const layer1 = map.getLayer('test');
expect(layer1.source).toEqual('test1');
expect(layer1['source-layer']).toEqual('test1');
wrapper.setProps({
children: [
<Source id="test2" type="geojson" data={data} />,
<Layer id="test" type="circle" source="test2" source-layer="test2" />
]
});
const layer2 = map.getLayer('test');
expect(layer2.source).toEqual('test2');
expect(layer2['source-layer']).toEqual('test2');
});
test('before', () => {
const data = { type: 'FeatureCollection', features: [] };
const wrapper = mount(
<MapGL latitude={0} longitude={0} zoom={0}>
<Source id="test" type="geojson" data={data} />
<Layer id="test1" type="circle" source="test" />
<Layer id="test3" type="circle" source="test" />
<Layer id="test2" type="circle" source="test" before="test3" />
</MapGL>
);
expect(wrapper.find('Layer').exists()).toBe(true);
expect(wrapper.find('Layer')).toHaveLength(3);
wrapper.unmount();
expect(wrapper.find('Layer').exists()).toBe(false);
});
test('update', () => {
const data = { type: 'FeatureCollection', features: [] };
const wrapper = mount(
<MapGL latitude={0} longitude={0} zoom={0}>
<Source id="test" type="geojson" data={data} />
<Layer id="test1" type="circle" source="test" />
<Layer id="test2" type="circle" source="test" />
</MapGL>
);
wrapper.setProps({
children: [
<Source id="test" type="geojson" data={data} />,
<Layer id="test1" type="circle" source="test" />,
<Layer
id="test2"
before="test1"
type="circle"
source="test"
layout={{ visibility: 'visible' }}
paint={{ 'circle-color': '#000' }}
filter={['==', 'id', 0]}
/>
]
});
wrapper.setProps({
children: [
<Source id="test" type="geojson" data={data} />,
<Layer id="test1" type="circle" source="test" />,
<Layer id="test2" type="circle" source="test" />
]
});
});
test('handlers', () => {
const data = { type: 'FeatureCollection', features: [] };
const handler = jest.fn();
const wrapper = mount(
<MapGL latitude={0} longitude={0} zoom={0}>
<Source id="test" type="geojson" data={data} />
<Layer id="test1" type="circle" source="test" />
</MapGL>
);
wrapper.setProps({
children: [
<Source id="test" type="geojson" data={data} />,
<Layer
id="test1"
type="circle"
source="test"
onClick={handler}
onHover={handler}
onEnter={handler}
onLeave={handler}
onContextMenu={handler}
/>
]
});
expect(handler).toHaveBeenCalledTimes(5);
wrapper.setProps({
children: [
<Source id="test" type="geojson" data={data} />,
<Layer id="test1" type="circle" source="test" />
]
});
expect(handler).toHaveBeenCalledTimes(5);
// wrapper.setProps({ children: [] });
});
test('throws', () => {
console.error = jest.fn();
expect(() =>
mount(<Layer id="test" type="circle" source="test" />)
).toThrow();
expect(console.error).toHaveBeenCalled();
});