-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
attribution.test.js
291 lines (243 loc) · 11.7 KB
/
attribution.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import {test} from '../../../util/test.js';
import config from '../../../../src/util/config.js';
import AttributionControl from '../../../../src/ui/control/attribution_control.js';
import {createMap as globalCreateMap} from '../../../util/index.js';
import simulate from '../../../util/simulate_interaction.js';
function createMap(t) {
return globalCreateMap(t, {
attributionControl: false,
accessToken: 'pk.123',
style: {
version: 8,
sources: {},
layers: [],
owner: 'mapbox',
id: 'streets-v10',
},
hash: true
});
}
test('AttributionControl appears in bottom-right by default', (t) => {
const map = createMap(t);
map.addControl(new AttributionControl());
t.equal(map.getContainer().querySelectorAll('.mapboxgl-ctrl-bottom-right .mapboxgl-ctrl-attrib').length, 1);
t.end();
});
test('AttributionControl appears in the position specified by the position option', (t) => {
const map = createMap(t);
map.addControl(new AttributionControl(), 'top-left');
t.equal(map.getContainer().querySelectorAll('.mapboxgl-ctrl-top-left .mapboxgl-ctrl-attrib').length, 1);
t.end();
});
test('AttributionControl appears in compact mode if compact option is used', (t) => {
const map = createMap(t);
Object.defineProperty(map.getContainer(), 'getBoundingClientRect', {value: () => ({height: 200, width: 700})});
Object.defineProperty(map.getCanvasContainer(), 'offsetWidth', {value: 700, configurable: true});
let attributionControl = new AttributionControl({
compact: true
});
map.addControl(attributionControl);
const container = map.getContainer();
t.equal(container.querySelectorAll('.mapboxgl-ctrl-attrib.mapboxgl-compact').length, 1);
map.removeControl(attributionControl);
Object.defineProperty(map.getContainer(), 'getBoundingClientRect', {value: () => ({height: 200, width: 600})});
Object.defineProperty(map.getCanvasContainer(), 'offsetWidth', {value: 600, configurable: true});
attributionControl = new AttributionControl({
compact: false
});
map.addControl(attributionControl);
t.equal(container.querySelectorAll('.mapboxgl-ctrl-attrib:not(.mapboxgl-compact)').length, 1);
t.end();
});
test('AttributionControl appears in compact mode if container is less then 640 pixel wide', (t) => {
const map = createMap(t);
Object.defineProperty(map.getContainer(), 'getBoundingClientRect', {value: () => ({height: 200, width: 700})});
Object.defineProperty(map.getCanvasContainer(), 'offsetWidth', {value: 700, configurable: true});
map.addControl(new AttributionControl());
const container = map.getContainer();
t.equal(container.querySelectorAll('.mapboxgl-ctrl-attrib:not(.mapboxgl-compact)').length, 1);
Object.defineProperty(map.getContainer(), 'getBoundingClientRect', {value: () => ({height: 200, width: 600})});
Object.defineProperty(map.getCanvasContainer(), 'offsetWidth', {value: 600, configurable: true});
map.resize();
t.equal(container.querySelectorAll('.mapboxgl-ctrl-attrib.mapboxgl-compact').length, 1);
t.end();
});
test('AttributionControl compact mode control toggles attribution', (t) => {
const map = createMap(t);
map.addControl(new AttributionControl({
compact: true
}));
const container = map.getContainer();
const toggle = container.querySelector('.mapboxgl-ctrl-attrib-button');
t.equal(container.querySelectorAll('.mapboxgl-compact-show').length, 0);
simulate.click(toggle);
t.equal(container.querySelectorAll('.mapboxgl-compact-show').length, 1);
simulate.click(toggle);
t.equal(container.querySelectorAll('.mapboxgl-compact-show').length, 0);
t.end();
});
test('AttributionControl dedupes attributions that are substrings of others', (t) => {
const map = createMap(t);
const attribution = new AttributionControl();
map.addControl(attribution);
map.on('load', () => {
map.addSource('1', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'World'});
map.addSource('2', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Hello World'});
map.addSource('3', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Another Source'});
map.addSource('4', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Hello'});
map.addSource('5', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Hello World'});
map.addSource('6', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Hello World'});
map.addSource('7', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'GeoJSON Source'});
map.addLayer({id: '1', type: 'fill', source: '1'});
map.addLayer({id: '2', type: 'fill', source: '2'});
map.addLayer({id: '3', type: 'fill', source: '3'});
map.addLayer({id: '4', type: 'fill', source: '4'});
map.addLayer({id: '5', type: 'fill', source: '5'});
map.addLayer({id: '6', type: 'fill', source: '6'});
map.addLayer({id: '7', type: 'fill', source: '7'});
});
let times = 0;
map.on('data', (e) => {
if (e.dataType === 'source' && e.sourceDataType === 'metadata') {
if (++times === 7) {
t.equal(attribution._innerContainer.innerHTML, 'Hello World | Another Source | GeoJSON Source');
t.end();
}
}
});
});
test('AttributionControl has the correct edit map link', (t) => {
config.FEEDBACK_URL = "https://feedback.com";
const map = createMap(t);
const attribution = new AttributionControl();
map.addControl(attribution);
map.on('load', () => {
map.addSource('1', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: '<a class="mapbox-improve-map" href="https://feedback.com" target="_blank">Improve this map</a>'});
map.addLayer({id: '1', type: 'fill', source: '1'});
map.on('data', (e) => {
if (e.dataType === 'source' && e.sourceDataType === 'metadata') {
t.equal(attribution._editLink.rel, 'noopener nofollow');
t.equal(attribution._editLink.href, 'https://feedback.com/?owner=mapbox&id=streets-v10&access_token=pk.123#/0/0/0', 'edit link contains map location data');
map.setZoom(2);
t.equal(attribution._editLink.href, 'https://feedback.com/?owner=mapbox&id=streets-v10&access_token=pk.123#/0/0/2', 'edit link updates on mapmove');
t.end();
}
});
});
});
test('AttributionControl is hidden if empty', (t) => {
const map = createMap(t);
const attribution = new AttributionControl();
map.addControl(attribution);
map.on('load', () => {
map.addSource('1', {type: 'geojson', data: {type: 'FeatureCollection', features: []}});
map.addLayer({id: '1', type: 'fill', source: '1'});
});
const container = map.getContainer();
const checkEmptyFirst = () => {
t.equal(attribution._innerContainer.innerHTML, '');
t.equal(container.querySelectorAll('.mapboxgl-attrib-empty').length, 1, 'includes empty class when no attribution strings are provided');
map.addSource('2', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Hello World'});
map.addLayer({id: '2', type: 'fill', source: '2'});
};
const checkNotEmptyLater = () => {
t.equal(attribution._innerContainer.innerHTML, 'Hello World');
t.equal(container.querySelectorAll('.mapboxgl-attrib-empty').length, 0, 'removes empty class when source with attribution is added');
t.end();
};
let times = 0;
map.on('data', (e) => {
if (e.dataType === 'source' && e.sourceDataType === 'metadata') {
times++;
if (times === 1) {
checkEmptyFirst();
} else if (times === 2) {
checkNotEmptyLater();
}
}
});
});
test('AttributionControl shows custom attribution if customAttribution option is provided', (t) => {
const map = createMap(t);
const attributionControl = new AttributionControl({
customAttribution: 'Custom string'
});
map.addControl(attributionControl);
t.equal(attributionControl._innerContainer.innerHTML, 'Custom string');
t.end();
});
test('AttributionControl shows custom attribution if customAttribution option is provided, control is removed and added back', (t) => {
const map = createMap(t);
const attributionControl = new AttributionControl({
customAttribution: 'Custom string'
});
map.addControl(attributionControl);
map.removeControl(attributionControl);
map.addControl(attributionControl);
t.equal(attributionControl._innerContainer.innerHTML, 'Custom string');
t.end();
});
test('AttributionControl in compact mode shows custom attribution if customAttribution option is provided', (t) => {
const map = createMap(t);
const attributionControl = new AttributionControl({
customAttribution: 'Custom string',
compact: true
});
map.addControl(attributionControl);
t.equal(attributionControl._innerContainer.innerHTML, 'Custom string');
t.end();
});
test('AttributionControl shows all custom attributions if customAttribution array of strings is provided', (t) => {
const map = createMap(t);
const attributionControl = new AttributionControl({
customAttribution: ['Some very long custom string', 'Custom string', 'Another custom string']
});
map.addControl(attributionControl);
t.equal(
attributionControl._innerContainer.innerHTML,
'Custom string | Another custom string | Some very long custom string'
);
t.end();
});
test('AttributionControl hides attributions for sources that are not currently visible', (t) => {
const map = createMap(t);
const attribution = new AttributionControl();
map.addControl(attribution);
map.on('load', () => {
map.addSource('1', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Used'});
map.addSource('2', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Not used'});
map.addSource('3', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Visibility none'});
map.addLayer({id: '1', type: 'fill', source: '1'});
map.addLayer({id: '3', type: 'fill', source: '3', layout: {visibility: 'none'}});
});
let times = 0;
map.on('data', (e) => {
if (e.dataType === 'source' && e.sourceDataType === 'metadata') {
if (++times === 3) {
t.equal(attribution._innerContainer.innerHTML, 'Used');
t.end();
}
}
});
});
test('AttributionControl toggles attributions for sources whose visibility changes when zooming', (t) => {
const map = createMap(t);
const attribution = new AttributionControl();
map.addControl(attribution);
map.on('load', () => {
map.addSource('1', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Used'});
map.addLayer({id: '1', type: 'fill', source: '1', minzoom: 12});
});
map.on('data', (e) => {
if (e.dataType === 'source' && e.sourceDataType === 'metadata') {
t.equal(attribution._innerContainer.innerHTML, '');
map.setZoom(13);
}
if (e.dataType === 'source' && e.sourceDataType === 'visibility') {
if (map.getZoom() === 13) {
t.equal(attribution._innerContainer.innerHTML, 'Used');
t.end();
}
}
});
});