-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslation.test.ts
47 lines (32 loc) · 1.77 KB
/
translation.test.ts
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
import { test, vi } from 'vitest';
import { Translation } from '../src/translation';
const width = 320;
const height = 200;
const padding = 20;
const units = (width - 2 * padding) / 14;
const canvas = {
width: () => width,
height: () => height,
};
test('translate from relative to absolute units', ({ expect }) => {
const translation = new Translation(canvas);
expect(translation.translateRelative(0, 0)).toEqual([width / 2, height / 2]);
// Edges
expect(translation.translateRelative(-7, 0)).toEqual([padding, height / 2]);
expect(translation.translateRelative(7, 0)).toEqual([width - padding, height / 2]);
expect(translation.translateRelative(0, 4)).toEqual([width / 2, padding]);
expect(translation.translateRelative(0, -4)).toEqual([width / 2, height - padding]);
// Non-edges
expect(translation.translateRelative(-2, 2)).toEqual([width / 2 - 2 * units, height / 2 - 2 * units]);
expect(translation.translateRelative(2, -2)).toEqual([width / 2 + 2 * units, height / 2 + 2 * units]);
});
test('translate from absolute to relative units', ({ expect }) => {
const translation = new Translation(canvas);
expect(translation.translateAbsolute(width / 2, height / 2)).toEqual([0, 0]);
expect(translation.translateAbsolute(padding, height / 2)).toEqual([-7, 0]);
expect(translation.translateAbsolute(width - padding, height / 2)).toEqual([7, 0]);
expect(translation.translateAbsolute(width / 2, padding)).toEqual([0, 4]);
expect(translation.translateAbsolute(width / 2, height - padding)).toEqual([0, -4]);
expect(translation.translateAbsolute(width / 2 - 2 * units, height / 2 - 2 * units)).toEqual([-2, 2]);
expect(translation.translateAbsolute(width / 2 + 2 * units, height / 2 + 2 * units)).toEqual([2, -2]);
});