Skip to content

Commit e56dada

Browse files
committed
Fix tests and last comments.
1 parent 0adabc3 commit e56dada

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

src/expression/definitions/interpolate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {Stops} from '../stops';
1212
import type {Expression} from '../expression';
1313
import type {ParsingContext} from '../parsing_context';
1414
import type {EvaluationContext} from '../evaluation_context';
15-
import type {ProjectionDefinitionTypeT, StringTypeT, Type} from '../types';
15+
import type {ProjectionDefinitionTypeT, Type} from '../types';
1616

1717
export type InterpolationType = {
1818
name: 'linear';
@@ -23,7 +23,7 @@ export type InterpolationType = {
2323
name: 'cubic-bezier';
2424
controlPoints: [number, number, number, number];
2525
};
26-
type InterpolatedValueType = NumberTypeT | ColorTypeT | StringTypeT | ProjectionDefinitionTypeT | PaddingTypeT | VariableAnchorOffsetCollectionTypeT | ArrayType<NumberTypeT>;
26+
type InterpolatedValueType = NumberTypeT | ColorTypeT | ProjectionDefinitionTypeT | PaddingTypeT | VariableAnchorOffsetCollectionTypeT | ArrayType<NumberTypeT>;
2727
export class Interpolate implements Expression {
2828
type: InterpolatedValueType;
2929

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
11
import {ProjectionDefinition} from './projection_definition';
22

33
describe('Projection class', () => {
4+
test('should parse projection with multiple inputs', () => {
5+
const projection = ProjectionDefinition.parse(['mercator', 'vertical-perspective', 0.5]);
6+
expect(projection.from).toBe('mercator');
7+
expect(projection.to).toBe('vertical-perspective');
8+
expect(projection.transition).toBe(0.5);
9+
});
10+
11+
test('should parse projection with single input', () => {
12+
const projection = ProjectionDefinition.parse('mercator');
13+
expect(projection.from).toBe('mercator');
14+
expect(projection.to).toBe('mercator');
15+
expect(projection.transition).toBe(1);
16+
});
17+
18+
test('should return undefined when input is not an array or string', () => {
19+
const projection = ProjectionDefinition.parse({} as any);
20+
expect(projection).toBeUndefined();
21+
});
22+
23+
test('should return undefined when input is an array with length not equal to 3', () => {
24+
const projection = ProjectionDefinition.parse(['mercator', 'vertical-perspective'] as any);
25+
expect(projection).toBeUndefined();
26+
});
27+
28+
test('should return undefined when input is an array with non-string and non-number elements', () => {
29+
const projection = ProjectionDefinition.parse([1, 2, 3] as any);
30+
expect(projection).toBeUndefined();
31+
});
432

5-
test('should serialize projection, with [from, to, transition]', () => {
6-
expect(`${new ProjectionDefinition('mercator', 'vertical-perspective', 1)}`).toBe('["mercator", "vertical-perspective", 1]');
7-
expect(`${new ProjectionDefinition('vertical-perspective', 'mercator', 0.3)}`).toBe('["vertical-perspective", "mercator", 0.3]');
33+
test('should interpolate projections', () => {
34+
const projection = ProjectionDefinition.interpolate('mercator', 'vertical-perspective', 0.5);
35+
expect(projection.from).toBe('mercator');
36+
expect(projection.to).toBe('vertical-perspective');
37+
expect(projection.transition).toBe(0.5);
838
});
939
});

src/expression/types/projection_definition.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ export class ProjectionDefinition {
88
this.to = to;
99
this.transition = transition;
1010
}
11-
12-
toString() {
13-
return `["${this.from}", "${this.to}", ${this.transition}]`;
14-
}
15-
16-
toJSON() {
17-
return [this.from, this.to, this.transition];
18-
}
1911

2012
static interpolate(from: string, to: string, t: number) {
2113
return new ProjectionDefinition(from, to, t);
@@ -25,7 +17,7 @@ export class ProjectionDefinition {
2517
if (input instanceof ProjectionDefinition) {
2618
return input;
2719
}
28-
if (Array.isArray(input) && input.length === 3) {
20+
if (Array.isArray(input) && input.length === 3 && typeof input[0] === 'string' && typeof input[1] === 'string' && typeof input[2] === 'number') {
2921
return new ProjectionDefinition(input[0], input[1], input[2]);
3022
}
3123
if (typeof input === 'string') {

test/integration/expression/tests/interpolate/projection/linear/test.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
"type": "projectionDefinition"
3939
},
4040
"outputs": [
41-
[
42-
"vertical-perspective",
43-
"mercator",
44-
0.5
45-
]
41+
{
42+
"from": "vertical-perspective",
43+
"to": "mercator",
44+
"transition": 0.5
45+
}
4646
]
4747
}
4848
}

test/integration/expression/tests/interpolate/projection/same-from-to/test.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
"type": "projectionDefinition"
3939
},
4040
"outputs": [
41-
[
42-
"mercator",
43-
"mercator",
44-
0.5
45-
]
41+
{
42+
"from": "mercator",
43+
"to": "mercator",
44+
"transition": 0.5
45+
}
4646
]
4747
}
4848
}

0 commit comments

Comments
 (0)