-
Notifications
You must be signed in to change notification settings - Fork 596
/
Copy pathfeature.test.js
152 lines (130 loc) · 5.04 KB
/
feature.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import test from 'node:test';
import assert from 'node:assert/strict';
import {spy} from 'sinon';
import Feature from '../src/feature_types/feature.js';
import createFeature from './utils/create_feature.js';
import getPublicMemberKeys from './utils/get_public_member_keys.js';
import createMockCtx from './utils/create_mock_feature_context.js';
test('Feature contrusctor and API', () => {
const featureGeoJson = createFeature('line');
const ctx = createMockCtx();
const feature = new Feature(ctx, featureGeoJson);
// Instance members
assert.equal(feature.ctx, ctx, 'feature.ctx');
assert.equal(feature.coordinates, featureGeoJson.geometry.coordinates, 'feature.coordinates');
assert.equal(feature.properties, featureGeoJson.properties, 'feature.properties');
assert.equal(feature.id, featureGeoJson.id, 'feature.id');
assert.equal(feature.type, featureGeoJson.geometry.type, 'feature.type');
assert.equal(getPublicMemberKeys(feature).length, 5, 'no unexpected instance members');
// Prototype members
assert.equal(typeof Feature.prototype.changed, 'function', 'feature.changed');
assert.equal(typeof Feature.prototype.incomingCoords, 'function', 'feature.incomingCoords');
assert.equal(typeof Feature.prototype.setCoordinates, 'function', 'feature.setCoordinates');
assert.equal(typeof Feature.prototype.getCoordinates, 'function', 'feature.getCoordinates');
assert.equal(typeof Feature.prototype.toGeoJSON, 'function', 'feature.toGeoJSON');
assert.equal(typeof Feature.prototype.internal, 'function', 'feature.internal');
assert.equal(typeof Feature.prototype.setProperty, 'function', 'feature.setProperty');
assert.equal(getPublicMemberKeys(Feature.prototype).length, 7, 'no unexpected prototype members');
const simpleFeatureGeoJson = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [0, 0]
}
};
const featureWithDefaultsOnly = new Feature(ctx, simpleFeatureGeoJson);
assert.deepEqual(featureWithDefaultsOnly.properties, {}, 'feature.properties defaults to {}');
assert.ok(featureWithDefaultsOnly.id, 'feature.id is provided');
});
test('Feature#changed', () => {
const ctx = createMockCtx();
const featureGeoJson = createFeature('point');
const feature = new Feature(ctx, featureGeoJson);
ctx.store.featureChanged.resetHistory();
feature.changed();
assert.equal(ctx.store.featureChanged.callCount, 1, 'called function on store');
assert.deepEqual(ctx.store.featureChanged.getCall(0).args[0], featureGeoJson.id, 'with correct args');
});
test('Feature#incomingCoords', () => {
const ctx = createMockCtx();
const featureGeoJson = createFeature('point');
featureGeoJson.geometry.coordinates = [9, 10];
const feature = new Feature(ctx, featureGeoJson);
const changedSpy = spy(feature, 'changed');
feature.incomingCoords([1, 2]);
assert.deepEqual(feature.coordinates, [1, 2]);
assert.equal(changedSpy.callCount, 1);
});
test('Feature#setCoordinates, Feature#setCoordinates', () => {
const ctx = createMockCtx();
const featureGeoJson = createFeature('point');
featureGeoJson.geometry.coordinates = [9, 10];
const feature = new Feature(ctx, featureGeoJson);
const changedSpy = spy(feature, 'changed');
assert.deepEqual(feature.getCoordinates(), [9, 10]);
feature.setCoordinates([1, 2]);
assert.deepEqual(feature.coordinates, [1, 2]);
assert.deepEqual(feature.getCoordinates(), [1, 2]);
assert.equal(changedSpy.callCount, 1);
});
test('Feature#toGeoJSON', () => {
const ctx = createMockCtx();
const polygon = createFeature('polygon');
const feature = new Feature(ctx, polygon);
assert.deepEqual(feature.toGeoJSON(), {
id: feature.id,
type: 'Feature',
properties: feature.properties,
geometry: {
coordinates: feature.coordinates,
type: feature.type
}
});
});
test('Feature#internal - when userProperties is true', () => {
const ctx = createMockCtx({userProperties: true});
const polygon = createFeature('polygon');
const feature = new Feature(ctx, polygon);
assert.deepEqual(feature.internal('foo'), {
type: 'Feature',
properties: {
user_a: 'b',
user_c: 'd',
id: feature.id,
meta: 'feature',
'meta:type': feature.type,
active: 'false',
mode: 'foo'
},
geometry: {
coordinates: feature.coordinates,
type: feature.type
}
});
});
test('Feature#internal - when userProperties is false', () => {
const ctx = createMockCtx({userProperties: false});
const polygon = createFeature('polygon');
const feature = new Feature(ctx, polygon);
assert.deepEqual(feature.internal('foo'), {
type: 'Feature',
properties: {
id: feature.id,
meta: 'feature',
'meta:type': feature.type,
active: 'false',
mode: 'foo'
},
geometry: {
coordinates: feature.coordinates,
type: feature.type
}
});
});
test('Feature#setProperty', () => {
const ctx = createMockCtx();
const polygon = createFeature('polygon');
const feature = new Feature(ctx, polygon);
feature.setProperty('size', 200);
assert.equal(feature.properties.size, 200);
});