UNPKG

4.19 kBPlain TextView Raw
1import {mapObject} from '../util/util';
2import StyleLayerIndex from './style_layer_index';
3
4describe('StyleLayerIndex', () => {
5 test('StyleLayerIndex#replace', () => {
6 const index = new StyleLayerIndex([
7 {id: '1', type: 'fill', source: 'source', 'source-layer': 'layer', paint: {'fill-color': 'red'}},
8 {id: '2', type: 'circle', source: 'source', 'source-layer': 'layer', paint: {'circle-color': 'green'}},
9 {id: '3', type: 'circle', source: 'source', 'source-layer': 'layer', paint: {'circle-color': 'blue'}}
10 ]);
11
12 const families = index.familiesBySource['source']['layer'];
13 expect(families).toHaveLength(2);
14 expect(families[0]).toHaveLength(1);
15 expect(families[0][0].id).toBe('1');
16 expect(families[1]).toHaveLength(2);
17 expect(families[1][0].id).toBe('2');
18 expect(families[1][1].id).toBe('3');
19
20 index.replace([]);
21 expect(index.familiesBySource).toEqual({});
22
23 });
24
25 test('StyleLayerIndex#update', () => {
26 const index = new StyleLayerIndex([
27 {id: '1', type: 'fill', source: 'foo', 'source-layer': 'layer', paint: {'fill-color': 'red'}},
28 {id: '2', type: 'circle', source: 'foo', 'source-layer': 'layer', paint: {'circle-color': 'green'}},
29 {id: '3', type: 'circle', source: 'foo', 'source-layer': 'layer', paint: {'circle-color': 'blue'}}
30 ]);
31
32 index.update([
33 {id: '1', type: 'fill', source: 'bar', 'source-layer': 'layer', paint: {'fill-color': 'cyan'}},
34 {id: '2', type: 'circle', source: 'bar', 'source-layer': 'layer', paint: {'circle-color': 'magenta'}},
35 {id: '3', type: 'circle', source: 'bar', 'source-layer': 'layer', paint: {'circle-color': 'yellow'}}
36 ], []);
37
38 const families = index.familiesBySource['bar']['layer'];
39 expect(families).toHaveLength(2);
40 expect(families[0]).toHaveLength(1);
41 expect(families[0][0].getPaintProperty('fill-color')).toBe('cyan');
42 expect(families[1]).toHaveLength(2);
43 expect(families[1][0].getPaintProperty('circle-color')).toBe('magenta');
44 expect(families[1][0].source).toBe('bar');
45 expect(families[1][1].getPaintProperty('circle-color')).toBe('yellow');
46 expect(families[1][1].source).toBe('bar');
47
48 });
49
50 test('StyleLayerIndex#familiesBySource', () => {
51 const index = new StyleLayerIndex([
52 {id: '0', type: 'fill', 'source': 'A', 'source-layer': 'foo'},
53 {id: '1', type: 'fill', 'source': 'A', 'source-layer': 'foo'},
54 {id: '2', type: 'fill', 'source': 'A', 'source-layer': 'foo', 'minzoom': 1},
55 {id: '3', type: 'fill', 'source': 'A', 'source-layer': 'bar'},
56 {id: '4', type: 'fill', 'source': 'B', 'source-layer': 'foo'},
57 {id: '5', type: 'fill', 'source': 'geojson'},
58 {id: '6', type: 'background'}
59 ]);
60
61 const ids = mapObject(index.familiesBySource, (bySource) => {
62 return mapObject(bySource, (families) => {
63 return families.map((family) => {
64 return family.map((layer) => layer.id);
65 });
66 });
67 });
68
69 expect(ids).toEqual({
70 'A': {
71 'foo': [['0', '1'], ['2']],
72 'bar': [['3']]
73 },
74 'B': {
75 'foo': [['4']]
76 },
77 'geojson': {
78 '_geojsonTileLayer': [['5']]
79 },
80 '': {
81 '_geojsonTileLayer': [['6']]
82 }
83 });
84
85 });
86
87 test('StyleLayerIndex groups families even if layout key order differs', () => {
88 const index = new StyleLayerIndex([
89 {id: '0', type: 'line', 'source': 'source', 'source-layer': 'layer',
90 'layout': {'line-cap': 'butt', 'line-join': 'miter'}},
91 {id: '1', type: 'line', 'source': 'source', 'source-layer': 'layer',
92 'layout': {'line-join': 'miter', 'line-cap': 'butt'}}
93 ]);
94
95 const families = index.familiesBySource['source']['layer'];
96 expect(families[0]).toHaveLength(2);
97
98 });
99});