UNPKG

2.59 kBPlain TextView Raw
1import Point from '@mapbox/point-geometry';
2
3import {offsetLine} from './query_utils';
4
5const defaultPrecision = 10;
6
7const closeTo = (expected, precision = defaultPrecision) => ({
8 asymmetricMatch: (actual) => Math.abs(expected - actual) < Math.pow(10, -precision) / 2
9});
10
11describe('offsetLine', () => {
12 test('line two points east', () => {
13 const line = [
14 new Point(0, 0),
15 new Point(1, 0)
16 ];
17 const offset = 1;
18
19 expect(offsetLine([line], offset)).toEqual([[
20 new Point(0, 1),
21 new Point(1, 1)
22 ]]);
23 });
24
25 test('line two points west', () => {
26 const line = [
27 new Point(10, 10),
28 new Point(5, 10)
29 ];
30 const offset = 2;
31
32 expect(offsetLine([line], offset)).toEqual([[
33 new Point(10, 8),
34 new Point(5, 8)
35 ]]);
36 });
37
38 test('line two points south', () => {
39 const line = [
40 new Point(0, -1),
41 new Point(0, 1)
42 ];
43 const offset = 1;
44
45 expect(offsetLine([line], offset)).toEqual([[
46 new Point(-1, -1),
47 new Point(-1, 1)
48 ]]);
49 });
50
51 test('line three points north', () => {
52 const line = [
53 new Point(0, 1),
54 new Point(0, 0),
55 new Point(0, -1)
56 ];
57 const offset = 1;
58
59 expect(offsetLine([line], offset)).toEqual([[
60 new Point(1, 1),
61 new Point(1, 0),
62 new Point(1, -1)
63 ]]);
64 });
65
66 test('line three points north east', () => {
67 const line = [
68 new Point(-1, 1),
69 new Point(0, 0),
70 new Point(1, -1)
71 ];
72 const offset = Math.sqrt(2);
73
74 expect(offsetLine([line], offset)).toEqual([[
75 {
76 x: closeTo(0),
77 y: closeTo(2)
78 },
79 {
80 x: closeTo(1),
81 y: closeTo(1)
82 },
83 {
84 x: closeTo(2),
85 y: closeTo(0)
86 }
87 ]]);
88 });
89
90 test('ring five points square', () => {
91 const ring = [
92 new Point(0, 0),
93 new Point(10, 0),
94 new Point(10, -10),
95 new Point(0, -10),
96 new Point(0, 0)
97 ];
98 const offset = 2;
99 expect(offsetLine([ring], offset)).toEqual([[
100 new Point(0, 2),
101 new Point(12, 2),
102 new Point(12, -12),
103 new Point(-2, -12),
104 new Point(-2, 0)
105 ]]);
106 });
107});