-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathconstructor.test.js
More file actions
201 lines (158 loc) · 5.96 KB
/
Copy pathconstructor.test.js
File metadata and controls
201 lines (158 loc) · 5.96 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import '@babel/polyfill';
import { assert, expect, test } from 'vitest';
import { isFunction } from '@flekschas/utils';
import createScatterplot, {
createRegl,
createRenderer,
createSpatialIndex,
createTextureFromUrl,
} from '../src';
import {
DEFAULT_CAMERA_IS_FIXED,
DEFAULT_COLOR_ACTIVE,
DEFAULT_COLOR_BG,
DEFAULT_COLOR_HOVER,
DEFAULT_COLOR_NORMAL,
DEFAULT_GAMMA,
DEFAULT_HEIGHT,
DEFAULT_LASSO_BRUSH_SIZE,
DEFAULT_LASSO_TYPE,
DEFAULT_OPACITY,
DEFAULT_OPACITY_INACTIVE_MAX,
DEFAULT_OPACITY_INACTIVE_SCALE,
DEFAULT_POINT_OUTLINE_WIDTH,
DEFAULT_POINT_SIZE,
DEFAULT_POINT_SIZE_SELECTED,
DEFAULT_WIDTH,
IMAGE_LOAD_ERROR,
} from '../src/constants';
import {
createCanvas,
flatArrayEqual,
} from './utils';
import imageUrl from './assets/image.jpg';
const EPS = 1e-7;
const floatEqual = (a, b) => Math.abs(a - b) <= EPS;
test('createRegl()', () => {
const dim = 200;
const canvas = createCanvas(dim, dim);
const gl = canvas.getContext('webgl');
expect(gl.drawingBufferWidth).toBe(dim);
expect(gl.drawingBufferHeight).toBe(dim);
const regl = createRegl(canvas);
expect(regl).toBeDefined();
regl.destroy();
});
test('createScatterplot()', () => {
const canvas = createCanvas(null, null);
const scatterplot = createScatterplot({ canvas });
expect(scatterplot.get('canvas')).toBe(canvas);
expect(scatterplot.get('backgroundColor')).toBe(DEFAULT_COLOR_BG);
expect(scatterplot.get('pointColor')).toBe(DEFAULT_COLOR_NORMAL);
expect(scatterplot.get('pointColorActive')).toBe(DEFAULT_COLOR_ACTIVE);
expect(scatterplot.get('pointColorHover')).toBe(DEFAULT_COLOR_HOVER);
expect(scatterplot.get('pointSize')).toBe(DEFAULT_POINT_SIZE);
expect(scatterplot.get('pointSizeSelected')).toBe(DEFAULT_POINT_SIZE_SELECTED);
expect(scatterplot.get('pointOutlineWidth')).toBe(DEFAULT_POINT_OUTLINE_WIDTH);
expect(scatterplot.get('opacity')).toBe(DEFAULT_OPACITY);
expect(scatterplot.get('opacityInactiveMax')).toBe(DEFAULT_OPACITY_INACTIVE_MAX);
expect(scatterplot.get('opacityInactiveScale')).toBe(DEFAULT_OPACITY_INACTIVE_SCALE);
expect(scatterplot.get('width')).toBe(DEFAULT_WIDTH);
expect(scatterplot.get('height')).toBe(DEFAULT_HEIGHT);
expect(scatterplot.get('opacityInactiveMax')).toBe(DEFAULT_OPACITY_INACTIVE_MAX);
expect(scatterplot.get('opacityInactiveScale')).toBe(DEFAULT_OPACITY_INACTIVE_SCALE);
expect(scatterplot.get('width')).toBe(DEFAULT_WIDTH);
expect(scatterplot.get('height')).toBe(DEFAULT_HEIGHT);
expect(scatterplot.get('cameraIsFixed')).toBe(DEFAULT_CAMERA_IS_FIXED);
expect(scatterplot.get('lassoType')).toBe(DEFAULT_LASSO_TYPE);
expect(scatterplot.get('lassoBrushSize')).toBe(DEFAULT_LASSO_BRUSH_SIZE);
scatterplot.destroy();
});
test('createScatterplot({ cameraTarget, cameraDistance, cameraRotation, cameraView })', () => {
const cameraTarget = [1, 1];
const cameraDistance = 2;
const cameraRotation = Math.PI / 4;
const scatterplot = createScatterplot({
cameraTarget,
cameraDistance,
cameraRotation,
});
expect(flatArrayEqual([1, 1], scatterplot.get('cameraTarget'), floatEqual)).toBe(true);
expect(floatEqual(cameraDistance, scatterplot.get('cameraDistance'))).toBe(true);
expect(floatEqual(cameraRotation, scatterplot.get('cameraRotation'))).toBe(true);
// biome-ignore format: the array should not be formatted
const cameraView = new Float32Array([
0.5, 0, 0, 0.5,
0, 0.5, 0, 0.5,
0, 0, 0.5, 0,
0, 0, 0, 1,
]);
const scatterplot2 = createScatterplot({ cameraView });
expect(cameraView).toEqual(scatterplot2.get('cameraView'));
scatterplot.destroy();
scatterplot2.destroy();
});
test('createTextureFromUrl()', async ({ skip }) => {
const regl = createRegl(createCanvas());
try {
const texture = await createTextureFromUrl(regl, imageUrl);
expect(texture._reglType).toBe('texture2d');
} catch (e) {
if (e.message === IMAGE_LOAD_ERROR) {
skip('Skipping because image loading timed out');
} else {
assert.fail('Failed to load image from URL');
}
}
regl.destroy();
});
test('createRenderer()', () => {
const canvas = createCanvas();
const regl = createRegl(canvas);
const renderer = createRenderer({ canvas, regl });
expect(!!renderer).toBe(true);
expect(renderer.canvas).toBe(canvas);
expect(renderer.regl).toBe(regl);
expect(renderer.gamma).toBe(DEFAULT_GAMMA);
expect(isFunction(renderer.render)).toBe(true);
expect(isFunction(renderer.onFrame)).toBe(true);
expect(isFunction(renderer.refresh)).toBe(true);
expect(isFunction(renderer.destroy)).toBe(true);
const sp1 = createScatterplot({ renderer });
const sp2 = createScatterplot({ renderer });
expect(sp1.get('renderer')).toBe(renderer);
expect(sp2.get('renderer')).toBe(renderer);
sp1.destroy();
sp2.destroy();
// Renderer should have not been destroyed
expect(renderer.canvas).toBe(canvas);
expect(renderer.regl).toBe(regl);
const sp3 = createScatterplot({ renderer });
expect(sp3.get('renderer')).toBe(renderer);
renderer.gamma = 10;
expect(renderer.gamma).toBe(10);
sp3.destroy();
renderer.destroy();
// Now the renderer should have been destroyed
expect(renderer.canvas).toBeUndefined();
expect(renderer.regl).toBeUndefined();
});
test('createSpatialIndex', async () => {
const points = {
x: [-1, 1, 0, -1, 1],
y: [1, 1, 0, -1, -1],
z: [0.2, 0.4, 0.6, 0.8, 1],
};
const spatialIndex1 = await createSpatialIndex(points);
const spatialIndex2 = await createSpatialIndex(points, {
useWorker: true,
});
const scatterplot = createScatterplot({ canvas: createCanvas() });
await scatterplot.draw(points, { spatialIndex: spatialIndex1 });
await scatterplot.zoomToArea({ x: 0, y: 0, width: 1, height: 1 });
expect(scatterplot.get('pointsInView')).toEqual([1, 2]);
await scatterplot.draw(points, { spatialIndex: spatialIndex2 });
await scatterplot.zoomToArea({ x: -1, y: -1, width: 1, height: 1 });
expect(scatterplot.get('pointsInView')).toEqual([2, 3]);
scatterplot.destroy();
});