Accept an object literal for static layer styling
This commit is contained in:
57
test/node/ol/layer/Vector.test.js
Normal file
57
test/node/ol/layer/Vector.test.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import Fill from '../../../../src/ol/style/Fill.js';
|
||||
import Stroke from '../../../../src/ol/style/Stroke.js';
|
||||
import Style from '../../../../src/ol/style/Style.js';
|
||||
import Vector from '../../../../src/ol/layer/Vector.js';
|
||||
import expect from '../../expect.js';
|
||||
|
||||
describe('ol/layer/Vector.js', () => {
|
||||
describe('setStyle()', () => {
|
||||
it('accepts a flat style', () => {
|
||||
const layer = new Vector();
|
||||
layer.setStyle({
|
||||
'fill-color': 'red',
|
||||
});
|
||||
|
||||
const style = layer.getStyle();
|
||||
expect(style).to.be.a(Style);
|
||||
|
||||
const fill = style.getFill();
|
||||
expect(fill).to.be.a(Fill);
|
||||
expect(fill.getColor()).to.be('red');
|
||||
});
|
||||
|
||||
it('accepts an array of flat styles', () => {
|
||||
const layer = new Vector();
|
||||
layer.setStyle([
|
||||
{
|
||||
'stroke-color': 'red',
|
||||
'stroke-width': 10,
|
||||
},
|
||||
{
|
||||
'stroke-color': 'yellow',
|
||||
'stroke-width': 5,
|
||||
},
|
||||
]);
|
||||
|
||||
const style = layer.getStyle();
|
||||
expect(Array.isArray(style)).to.be(true);
|
||||
expect(style).to.have.length(2);
|
||||
|
||||
const first = style[0];
|
||||
expect(first).to.be.a(Style);
|
||||
|
||||
const firstStroke = first.getStroke();
|
||||
expect(firstStroke).to.be.a(Stroke);
|
||||
expect(firstStroke.getColor()).to.be('red');
|
||||
expect(firstStroke.getWidth()).to.be(10);
|
||||
|
||||
const second = style[1];
|
||||
expect(second).to.be.a(Style);
|
||||
|
||||
const secondStroke = second.getStroke();
|
||||
expect(secondStroke).to.be.a(Stroke);
|
||||
expect(secondStroke.getColor()).to.be('yellow');
|
||||
expect(secondStroke.getWidth()).to.be(5);
|
||||
});
|
||||
});
|
||||
});
|
||||
161
test/node/ol/style/flat.test.js
Normal file
161
test/node/ol/style/flat.test.js
Normal file
@@ -0,0 +1,161 @@
|
||||
import Fill from '../../../../src/ol/style/Fill.js';
|
||||
import Icon from '../../../../src/ol/style/Icon.js';
|
||||
import RegularShape from '../../../../src/ol/style/RegularShape.js';
|
||||
import Stroke from '../../../../src/ol/style/Stroke.js';
|
||||
import Style from '../../../../src/ol/style/Style.js';
|
||||
import Text from '../../../../src/ol/style/Text.js';
|
||||
import expect from '../../expect.js';
|
||||
import {toStyle} from '../../../../src/ol/style/flat.js';
|
||||
|
||||
describe('ol/style/flat.js', () => {
|
||||
describe('toStyle()', () => {
|
||||
it('creates a style with a fill', () => {
|
||||
const style = toStyle({
|
||||
'fill-color': 'red',
|
||||
});
|
||||
|
||||
expect(style).to.be.a(Style);
|
||||
|
||||
const fill = style.getFill();
|
||||
expect(fill).to.be.a(Fill);
|
||||
expect(fill.getColor()).to.be('red');
|
||||
|
||||
expect(style.getStroke()).to.be(null);
|
||||
expect(style.getText()).to.be(null);
|
||||
expect(style.getImage()).to.be(null);
|
||||
});
|
||||
|
||||
it('creates a style with a stroke', () => {
|
||||
const style = toStyle({
|
||||
'stroke-width': 2,
|
||||
});
|
||||
|
||||
expect(style).to.be.a(Style);
|
||||
|
||||
const stroke = style.getStroke();
|
||||
expect(stroke).to.be.a(Stroke);
|
||||
expect(stroke.getWidth()).to.be(2);
|
||||
|
||||
expect(style.getFill()).to.be(null);
|
||||
expect(style.getText()).to.be(null);
|
||||
expect(style.getImage()).to.be(null);
|
||||
});
|
||||
|
||||
it('creates a style with a text', () => {
|
||||
const style = toStyle({
|
||||
'text-value': 'foo',
|
||||
'text-fill-color': 'blue',
|
||||
'text-stroke-width': 2,
|
||||
});
|
||||
|
||||
expect(style).to.be.a(Style);
|
||||
|
||||
const text = style.getText();
|
||||
expect(text).to.be.a(Text);
|
||||
expect(text.getText()).to.be('foo');
|
||||
|
||||
const textFill = text.getFill();
|
||||
expect(textFill).to.be.a(Fill);
|
||||
expect(textFill.getColor()).to.be('blue');
|
||||
|
||||
const textStroke = text.getStroke();
|
||||
expect(textStroke).to.be.a(Stroke);
|
||||
expect(textStroke.getWidth()).to.be(2);
|
||||
|
||||
expect(style.getFill()).to.be(null);
|
||||
expect(style.getStroke()).to.be(null);
|
||||
expect(style.getImage()).to.be(null);
|
||||
});
|
||||
|
||||
it('creates a style with an icon', () => {
|
||||
const style = toStyle({
|
||||
'icon-src': 'https://example.com/icon.png',
|
||||
});
|
||||
|
||||
expect(style).to.be.a(Style);
|
||||
|
||||
const icon = style.getImage();
|
||||
expect(icon).to.be.a(Icon);
|
||||
|
||||
expect(style.getFill()).to.be(null);
|
||||
expect(style.getStroke()).to.be(null);
|
||||
expect(style.getText()).to.be(null);
|
||||
});
|
||||
|
||||
it('creates a style with a regular shape', () => {
|
||||
const style = toStyle({
|
||||
'shape-points': 10,
|
||||
'shape-radius': 42,
|
||||
'shape-fill-color': 'red',
|
||||
'shape-stroke-color': 'blue',
|
||||
});
|
||||
|
||||
expect(style).to.be.a(Style);
|
||||
|
||||
const shape = style.getImage();
|
||||
expect(shape).to.be.a(RegularShape);
|
||||
expect(shape.getPoints()).to.be(10);
|
||||
expect(shape.getRadius()).to.be(42);
|
||||
|
||||
const shapeFill = shape.getFill();
|
||||
expect(shapeFill).to.be.a(Fill);
|
||||
expect(shapeFill.getColor()).to.be('red');
|
||||
|
||||
const shapeStroke = shape.getStroke();
|
||||
expect(shapeStroke).to.be.a(Stroke);
|
||||
expect(shapeStroke.getColor()).to.be('blue');
|
||||
|
||||
expect(style.getFill()).to.be(null);
|
||||
expect(style.getStroke()).to.be(null);
|
||||
expect(style.getText()).to.be(null);
|
||||
});
|
||||
|
||||
it('creates a style with a circle', () => {
|
||||
const style = toStyle({
|
||||
'circle-radius': 42,
|
||||
'circle-fill-color': 'red',
|
||||
'circle-stroke-color': 'blue',
|
||||
});
|
||||
|
||||
expect(style).to.be.a(Style);
|
||||
|
||||
const circle = style.getImage();
|
||||
expect(circle).to.be.a(RegularShape);
|
||||
expect(circle.getRadius()).to.be(42);
|
||||
|
||||
const circleFill = circle.getFill();
|
||||
expect(circleFill).to.be.a(Fill);
|
||||
expect(circleFill.getColor()).to.be('red');
|
||||
|
||||
const circleStroke = circle.getStroke();
|
||||
expect(circleStroke).to.be.a(Stroke);
|
||||
expect(circleStroke.getColor()).to.be('blue');
|
||||
|
||||
expect(style.getFill()).to.be(null);
|
||||
expect(style.getStroke()).to.be(null);
|
||||
expect(style.getText()).to.be(null);
|
||||
});
|
||||
|
||||
it('creates a style with a fill and stroke', () => {
|
||||
const style = toStyle({
|
||||
'fill-color': 'red',
|
||||
'stroke-width': 2,
|
||||
'stroke-color': 'green',
|
||||
});
|
||||
|
||||
expect(style).to.be.a(Style);
|
||||
|
||||
const fill = style.getFill();
|
||||
expect(fill).to.be.a(Fill);
|
||||
expect(fill.getColor()).to.be('red');
|
||||
|
||||
const stroke = style.getStroke();
|
||||
expect(stroke).to.be.a(Stroke);
|
||||
expect(stroke.getWidth()).to.be(2);
|
||||
expect(stroke.getColor()).to.be('green');
|
||||
|
||||
expect(style.getText()).to.be(null);
|
||||
expect(style.getImage()).to.be(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user