Skip to content

Commit 48733e0

Browse files
Natalia Kowalczykmelitele
authored andcommitted
add unit tests checking that sources are reloaded only when necessary on changes of global state rferenced by layout properties
1 parent 8b56c22 commit 48733e0

File tree

1 file changed

+148
-4
lines changed

1 file changed

+148
-4
lines changed

src/style/style.test.ts

Lines changed: 148 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,33 @@ describe('Style.setGlobalState', () => {
13931393
expect(style.sourceCaches['fill-source-id'].reload).toHaveBeenCalled();
13941394
});
13951395

1396+
test('reloads sources when state property is used in layout property', async () => {
1397+
const style = new Style(getStubMap());
1398+
style.loadJSON(createStyleJSON({
1399+
sources: {
1400+
'line-source-id': createGeoJSONSource()
1401+
},
1402+
layers: [{
1403+
id: 'first-layer-id',
1404+
type: 'line',
1405+
source: 'line-source-id',
1406+
layout: {
1407+
'line-join': ['global-state', 'lineJoin']
1408+
}
1409+
}]
1410+
}));
1411+
1412+
await style.once('style.load');
1413+
1414+
style.sourceCaches['line-source-id'].resume = vi.fn();
1415+
style.sourceCaches['line-source-id'].reload = vi.fn();
1416+
1417+
style.setGlobalState({lineJoin: {default: 'bevel'}});
1418+
1419+
expect(style.sourceCaches['line-source-id'].resume).toHaveBeenCalled();
1420+
expect(style.sourceCaches['line-source-id'].reload).toHaveBeenCalled();
1421+
});
1422+
13961423
test('does not reload sources when state property is set to the same value as current one', async () => {
13971424
const style = new Style(getStubMap());
13981425
style.loadJSON(createStyleJSON({
@@ -1451,6 +1478,36 @@ describe('Style.setGlobalState', () => {
14511478
expect(style.sourceCaches['circle-source-id'].resume).not.toHaveBeenCalled();
14521479
expect(style.sourceCaches['circle-source-id'].reload).not.toHaveBeenCalled();
14531480
});
1481+
1482+
test('does not reload sources when new state property is used in paint property while state property used in layout is unchanged', async () => {
1483+
const style = new Style(getStubMap());
1484+
style.loadJSON(createStyleJSON({
1485+
sources: {
1486+
'line-source-id': createGeoJSONSource()
1487+
},
1488+
layers: [{
1489+
id: 'first-layer-id',
1490+
type: 'line',
1491+
source: 'line-source-id',
1492+
layout: {
1493+
'line-join': ['global-state', 'lineJoin']
1494+
},
1495+
paint: {
1496+
'line-color': ['global-state', 'lineColor']
1497+
}
1498+
}]
1499+
}));
1500+
1501+
await style.once('style.load');
1502+
1503+
style.sourceCaches['line-source-id'].resume = vi.fn();
1504+
style.sourceCaches['line-source-id'].reload = vi.fn();
1505+
1506+
style.setGlobalState({lineColor: {default: 'red'}});
1507+
1508+
expect(style.sourceCaches['line-source-id'].resume).not.toHaveBeenCalled();
1509+
expect(style.sourceCaches['line-source-id'].reload).not.toHaveBeenCalled();
1510+
});
14541511
});
14551512

14561513
describe('Style.setGlobalStateProperty', () => {
@@ -1541,6 +1598,63 @@ describe('Style.setGlobalStateProperty', () => {
15411598
expect(style.sourceCaches['fill-source-id'].reload).not.toHaveBeenCalled();
15421599
});
15431600

1601+
test('reloads sources when state property is used in layout property', async () => {
1602+
const style = new Style(getStubMap());
1603+
style.loadJSON(createStyleJSON({
1604+
sources: {
1605+
'line-1-source-id': createGeoJSONSource(),
1606+
'line-2-source-id': createGeoJSONSource(),
1607+
'line-3-source-id': createGeoJSONSource()
1608+
},
1609+
layers: [{
1610+
id: 'first-layer-id',
1611+
type: 'line',
1612+
source: 'line-1-source-id',
1613+
layout: {
1614+
'line-join': ['global-state', 'lineJoin']
1615+
}
1616+
}, {
1617+
id: 'second-layer-id',
1618+
type: 'line',
1619+
source: 'line-3-source-id'
1620+
}, {
1621+
id: 'third-layer-id',
1622+
type: 'line',
1623+
source: 'line-2-source-id',
1624+
layout: {
1625+
'line-join': ['global-state', 'lineJoin']
1626+
}
1627+
}, {
1628+
id: 'fourth-layer-id',
1629+
type: 'line',
1630+
source: 'line-3-source-id',
1631+
layout: {
1632+
'line-cap': ['global-state', 'lineCap']
1633+
}
1634+
}]
1635+
}));
1636+
1637+
await style.once('style.load');
1638+
1639+
style.sourceCaches['line-1-source-id'].resume = vi.fn();
1640+
style.sourceCaches['line-1-source-id'].reload = vi.fn();
1641+
style.sourceCaches['line-2-source-id'].resume = vi.fn();
1642+
style.sourceCaches['line-2-source-id'].reload = vi.fn();
1643+
style.sourceCaches['line-3-source-id'].resume = vi.fn();
1644+
style.sourceCaches['line-3-source-id'].reload = vi.fn();
1645+
1646+
style.setGlobalStateProperty('lineJoin', 'bevel');
1647+
1648+
// sources line-1 and line-2 should be reloaded
1649+
expect(style.sourceCaches['line-1-source-id'].resume).toHaveBeenCalled();
1650+
expect(style.sourceCaches['line-1-source-id'].reload).toHaveBeenCalled();
1651+
expect(style.sourceCaches['line-2-source-id'].resume).toHaveBeenCalled();
1652+
expect(style.sourceCaches['line-2-source-id'].reload).toHaveBeenCalled();
1653+
// source line-3 should not be reloaded
1654+
expect(style.sourceCaches['line-3-source-id'].resume).not.toHaveBeenCalled();
1655+
expect(style.sourceCaches['line-3-source-id'].reload).not.toHaveBeenCalled();
1656+
});
1657+
15441658
test('does not reload sources when state property is set to the same value as current one', async () => {
15451659
const style = new Style(getStubMap());
15461660
style.loadJSON(createStyleJSON({
@@ -1599,6 +1713,36 @@ describe('Style.setGlobalStateProperty', () => {
15991713
expect(style.sourceCaches['circle-source-id'].resume).not.toHaveBeenCalled();
16001714
expect(style.sourceCaches['circle-source-id'].reload).not.toHaveBeenCalled();
16011715
});
1716+
1717+
test('does not reload sources when state property is used in paint property while a different state property used in layout is unchanged', async () => {
1718+
const style = new Style(getStubMap());
1719+
style.loadJSON(createStyleJSON({
1720+
sources: {
1721+
'line-source-id': createGeoJSONSource()
1722+
},
1723+
layers: [{
1724+
id: 'first-layer-id',
1725+
type: 'line',
1726+
source: 'line-source-id',
1727+
layout: {
1728+
'line-join': ['global-state', 'lineJoin']
1729+
},
1730+
paint: {
1731+
'line-color': ['global-state', 'lineColor']
1732+
}
1733+
}]
1734+
}));
1735+
1736+
await style.once('style.load');
1737+
1738+
style.sourceCaches['line-source-id'].resume = vi.fn();
1739+
style.sourceCaches['line-source-id'].reload = vi.fn();
1740+
1741+
style.setGlobalStateProperty('lineColor', 'red');
1742+
1743+
expect(style.sourceCaches['line-source-id'].resume).not.toHaveBeenCalled();
1744+
expect(style.sourceCaches['line-source-id'].reload).not.toHaveBeenCalled();
1745+
});
16021746
});
16031747

16041748
describe('Style.addLayer', () => {
@@ -1660,7 +1804,7 @@ describe('Style.addLayer', () => {
16601804
style.loadJSON(createStyleJSON());
16611805
await style.once('style.load');
16621806
const errorPromise = style.once('error');
1663-
1807+
16641808
style.addLayer({
16651809
id: 'background',
16661810
type: 'background',
@@ -1887,9 +2031,9 @@ describe('Style.addLayer', () => {
18872031
}as LayerSpecification;
18882032

18892033
await style.once('style.load');
1890-
const errorPromise = style.once('error');
2034+
const errorPromise = style.once('error');
18912035
style.addLayer(layer);
1892-
2036+
18932037
const {error} = await errorPromise;
18942038
expect(error.message).toMatch(/does not exist on source/);
18952039
});
@@ -2078,7 +2222,7 @@ describe('Style.setPaintProperty', () => {
20782222

20792223
await source.once('data');
20802224
vi.spyOn(sourceCache, 'reload');
2081-
2225+
20822226
source.setData({'type': 'FeatureCollection', 'features': []});
20832227
style.setPaintProperty('circle', 'circle-color', {type: 'identity', property: 'foo'});
20842228
await waitForEvent(source, 'data', (e) => e.sourceDataType === 'content');

0 commit comments

Comments
 (0)