Skip to content

Commit 6d56253

Browse files
ibesoragithub-actions[bot]
authored andcommitted
Fix moveLayer tests
GitOrigin-RevId: dfd4a9fb54d5a691f290a47d883bbf01b2863eaa
1 parent abb78be commit 6d56253

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/style/style.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,7 +2710,7 @@ class Style extends Evented<MapEvents> {
27102710
// or it has the same slot as the 'before' layer,
27112711
// then we can insert the new layer before the existing one.
27122712
const beforeLayer = this._layers[before];
2713-
if (layer.slot === beforeLayer.slot) index = beforeIndex;
2713+
if (!layer.slot || layer.slot === beforeLayer.slot) index = beforeIndex;
27142714
else warnOnce(`Layer with id "${before}" has a different slot. Layers can only be rearranged within the same slot.`);
27152715
}
27162716

@@ -2785,7 +2785,7 @@ class Style extends Evented<MapEvents> {
27852785
// or it has the same slot as the 'before' layer,
27862786
// then we can insert the new layer before the existing one.
27872787
const beforeLayer = this._layers[before];
2788-
if (layer.slot === beforeLayer.slot) newIndex = beforeIndex;
2788+
if (!layer.slot || layer.slot === beforeLayer.slot) newIndex = beforeIndex;
27892789
else warnOnce(`Layer with id "${before}" has a different slot. Layers can only be rearranged within the same slot.`);
27902790
}
27912791

test/integration/render-tests/slots/layer-without-slot-before-layer-with-slot/style.json

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,26 @@
99
[
1010
"addLayer",
1111
{
12-
"id": "red",
13-
"source": "red",
12+
"id": "green",
13+
"source": "green",
1414
"type": "circle",
15-
"slot": "slot",
1615
"paint": {
17-
"circle-color": "#ff0000",
18-
"circle-radius": 10
16+
"circle-color": "#008000"
1917
}
2018
}
2119
],
2220
[
2321
"addLayer",
2422
{
25-
"id": "green",
26-
"source": "green",
23+
"id": "red",
24+
"source": "red",
2725
"type": "circle",
26+
"slot": "slot",
2827
"paint": {
29-
"circle-color": "#008000"
28+
"circle-color": "#ff0000",
29+
"circle-radius": 10
3030
}
3131
}
32-
],
33-
[
34-
"moveLayer",
35-
"green",
36-
"red"
3732
]
3833
]
3934
}

test/unit/style/style.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,39 @@ describe('Style#moveLayer', () => {
12581258
style.moveLayer('b', 'b');
12591259
expect(style._order).toEqual(['a', 'b', 'c']);
12601260
});
1261+
1262+
test('Throws a warning when layers have different slots', async () => {
1263+
const style = new Style(new StubMap());
1264+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
1265+
style.loadJSON(createStyleJSON({
1266+
layers: [
1267+
{id: 'a', type: 'background', slot: 'main'},
1268+
{id: 'b', type: 'background', slot: 'aux'}
1269+
]
1270+
}));
1271+
1272+
await waitFor(style, "style.load");
1273+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1274+
style.moveLayer('b', 'a');
1275+
expect(warnSpy).toHaveBeenCalledOnce();
1276+
expect(warnSpy).toHaveBeenCalledWith('Layer with id "a" has a different slot. Layers can only be rearranged within the same slot.');
1277+
expect(style._order).toEqual(['a', 'b']);
1278+
});
1279+
1280+
test('Does not throw warning when layer has not a slot', async () => {
1281+
const style = new Style(new StubMap());
1282+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
1283+
style.loadJSON(createStyleJSON({
1284+
layers: [
1285+
{id: 'a', type: 'background', slot: 'main'},
1286+
{id: 'b', type: 'background'}
1287+
]
1288+
}));
1289+
1290+
await waitFor(style, "style.load");
1291+
style.moveLayer('b', 'a');
1292+
expect(style._order).toEqual(['b', 'a']);
1293+
});
12611294
});
12621295

12631296
describe('Style#setPaintProperty', () => {

0 commit comments

Comments
 (0)