UNPKG

3.57 kBPlain TextView Raw
1import Light from './light';
2import styleSpec from '../style-spec/reference/latest';
3import Color from '../style-spec/util/color';
4import {sphericalToCartesian} from '../util/util';
5import EvaluationParameters from './evaluation_parameters';
6import {LightSpecification} from '../style-spec/types.g';
7import {TransitionParameters} from './properties';
8
9const spec = styleSpec.light;
10
11test('Light with defaults', () => {
12 const light = new Light({});
13 light.recalculate({zoom: 0, zoomHistory: {}} as EvaluationParameters);
14
15 expect(light.properties.get('anchor')).toEqual(spec.anchor.default);
16 expect(light.properties.get('position')).toEqual(sphericalToCartesian(spec.position.default as any as [number, number, number]));
17 expect(light.properties.get('intensity')).toEqual(spec.intensity.default);
18 expect(light.properties.get('color')).toEqual(Color.parse(spec.color.default));
19});
20
21test('Light with options', () => {
22 const light = new Light({
23 anchor: 'map',
24 position: [2, 30, 30],
25 intensity: 1
26 });
27 light.recalculate({zoom: 0, zoomHistory: {}} as EvaluationParameters);
28
29 expect(light.properties.get('anchor')).toBe('map');
30 expect(light.properties.get('position')).toEqual(sphericalToCartesian([2, 30, 30]));
31 expect(light.properties.get('intensity')).toBe(1);
32 expect(light.properties.get('color')).toEqual(Color.parse(spec.color.default));
33});
34
35test('Light with stops function', () => {
36 const light = new Light({
37 intensity: {
38 stops: [[16, 0.2], [17, 0.8]]
39 }
40 } as LightSpecification);
41 light.recalculate({zoom: 16.5, zoomHistory: {}} as EvaluationParameters);
42
43 expect(light.properties.get('intensity')).toBe(0.5);
44});
45
46test('Light#getLight', () => {
47 const defaults = {};
48 for (const key in spec) {
49 defaults[key] = spec[key].default;
50 }
51
52 expect(new Light(defaults).getLight()).toEqual(defaults);
53});
54
55describe('Light#setLight', () => {
56 test('sets light', () => {
57 const light = new Light({});
58 light.setLight({color: 'red', 'color-transition': {duration: 3000}} as LightSpecification);
59 light.updateTransitions({transition: true} as any as TransitionParameters);
60 light.recalculate({zoom: 16, zoomHistory: {}, now: 1500} as EvaluationParameters);
61 expect(light.properties.get('color')).toEqual(new Color(1, 0.5, 0.5, 1));
62 });
63
64 test('validates by default', () => {
65 const light = new Light({});
66 const lightSpy = jest.spyOn(light, '_validate');
67 jest.spyOn(console, 'error').mockImplementation(() => { });
68 light.setLight({color: 'notacolor'});
69 light.updateTransitions({transition: false} as any as TransitionParameters);
70 light.recalculate({zoom: 16, zoomHistory: {}, now: 10} as EvaluationParameters);
71 expect(lightSpy).toHaveBeenCalledTimes(1);
72 expect(console.error).toHaveBeenCalledTimes(1);
73 expect(lightSpy.mock.calls[0][2]).toEqual({});
74 });
75
76 test('respects validation option', () => {
77 const light = new Light({});
78
79 const lightSpy = jest.spyOn(light, '_validate');
80 light.setLight({color: [999]}, {validate: false});
81 light.updateTransitions({transition: false} as any as TransitionParameters);
82 light.recalculate({zoom: 16, zoomHistory: {}, now: 10} as EvaluationParameters);
83
84 expect(lightSpy).toHaveBeenCalledTimes(1);
85 expect(lightSpy.mock.calls[0][2]).toEqual({validate: false});
86 expect(light.properties.get('color')).toEqual([999]);
87 });
88});