From e72fc5c64b58a3d1773adbf6f2039635101ecf8a Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Mon, 1 Apr 2019 18:11:54 +0300 Subject: [PATCH] Add unit test for SymbolStyleLayer::has/setPaintOverrides --- test/unit/symbol/symbol_style_layer.test.js | 104 ++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 test/unit/symbol/symbol_style_layer.test.js diff --git a/test/unit/symbol/symbol_style_layer.test.js b/test/unit/symbol/symbol_style_layer.test.js new file mode 100644 index 00000000000..4958b218b83 --- /dev/null +++ b/test/unit/symbol/symbol_style_layer.test.js @@ -0,0 +1,104 @@ +import { test } from 'mapbox-gl-js-test'; +import SymbolStyleLayer from '../../../src/style/style_layer/symbol_style_layer'; +import FormatSectionOverride from '../../../src/style-spec/expression/definitions/format_section_override'; +import properties from '../../../src/style/style_layer/symbol_style_layer_properties'; + +function createSymbolLayer(layerProperties) { + const layer = new SymbolStyleLayer(layerProperties); + layer.recalculate({zoom: 0, zoomHistory: {}}); + return layer; +} + +function isOverriden(paintProperty) { + if (paintProperty.value.kind === 'source' || paintProperty.value.kind === 'composite') { + return paintProperty.value._styleExpression.expression instanceof FormatSectionOverride; + } + return false; +} + +test('setPaintOverrides', (t) => { + t.test('setPaintOverrides, no overrides', (t) => { + const layer = createSymbolLayer({}); + layer.setPaintOverrides(); + for (const overridable of properties.paint.overridableProperties) { + t.equal(isOverriden(layer.paint.get(overridable)), false); + } + t.end(); + }); + + t.test('setPaintOverrides, format expression, overriden text-color', (t) => { + const props = { layout: {'text-field': ["format", "text", {"text-color": "yellow"}]} }; + const layer = createSymbolLayer(props); + layer.setPaintOverrides(); + t.equal(isOverriden(layer.paint.get('text-color')), true); + t.end(); + }); + + t.test('setPaintOverrides, format expression, no overrides', (t) => { + const props = { layout: {'text-field': ["format", "text", {}]} }; + const layer = createSymbolLayer(props); + layer.setPaintOverrides(); + t.equal(isOverriden(layer.paint.get('text-color')), false); + t.end(); + }); + + t.end(); +}); + +test('hasPaintOverrides', (t) => { + t.test('undefined', (t) => { + const layer = createSymbolLayer({}); + t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), false); + t.end(); + }); + + t.test('constant, Formatted type, overriden text-color', (t) => { + const props = { layout: {'text-field': ["format", "text", {"text-color": "red"}]} }; + const layer = createSymbolLayer(props); + t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), true); + t.end(); + }); + + t.test('constant, Formatted type, no overrides', (t) => { + const props = { layout: {'text-field': ["format", "text", {"font-scale": 0.8}]} }; + const layer = createSymbolLayer(props); + t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), false); + t.end(); + }); + + t.test('format expression, overriden text-color', (t) => { + const props = { layout: {'text-field': ["format", ["get", "name"], {"text-color":"red"}]} }; + const layer = createSymbolLayer(props); + t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), true); + t.end(); + }); + + t.test('format expression, no overrides', (t) => { + const props = { layout: {'text-field': ["format", ["get", "name"], {}]} }; + const layer = createSymbolLayer(props); + t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), false); + t.end(); + }); + + t.test('nested expression, overriden text-color', (t) => { + const matchExpr = ["match", ["get", "case"], + "one", ["format", "color", {"text-color": "blue"}], + "default"]; + const props = { layout: {'text-field': matchExpr} }; + const layer = createSymbolLayer(props); + t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), true); + t.end(); + }); + + t.test('nested expression, no overrides', (t) => { + const matchExpr = ["match", ["get", "case"], + "one", ["format", "b&w", {}], + "default"]; + const props = { layout: {'text-field': matchExpr} }; + const layer = createSymbolLayer(props); + t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), false); + t.end(); + }); + + t.end(); +});