/
linearScale.test.js
96 lines (76 loc) · 2.68 KB
/
linearScale.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
import { linearScale } from '../src/linearScale'
it(`should be able to build a linear scale for x axis`, () => {
const scale = linearScale({ axis: 'x' }, { x: { min: 0, max: 1 } }, 100, 100)
expect(scale(0)).toBe(0)
expect(scale(0.5)).toBe(50)
expect(scale(1)).toBe(100)
})
it(`should be able to build a linear scale for y axis`, () => {
const scale = linearScale({ axis: 'y' }, { y: { min: 0, max: 1 } }, 100, 100)
expect(scale(0)).toBe(100)
expect(scale(0.5)).toBe(50)
expect(scale(1)).toBe(0)
})
it(`should allow to define min value for x axis`, () => {
const scale = linearScale({ axis: 'x', min: 0.5 }, { x: { min: 0, max: 1 } }, 100, 100)
expect(scale.domain()[0]).toBe(0.5)
expect(scale(0)).toBe(-100)
expect(scale(0.5)).toBe(0)
expect(scale(1)).toBe(100)
})
it(`should allow to define min value for y axis`, () => {
const scale = linearScale({ axis: 'y', min: 0.5 }, { y: { min: 0, max: 1 } }, 100, 100)
expect(scale.domain()[0]).toBe(0.5)
expect(scale(0)).toBe(200)
expect(scale(0.5)).toBe(100)
expect(scale(1)).toBe(0)
})
it(`should allow to define max value for x axis`, () => {
const scale = linearScale({ axis: 'x', max: 2 }, { x: { min: 0, max: 1 } }, 100, 100)
expect(scale.domain()[1]).toBe(2)
expect(scale(0)).toBe(0)
expect(scale(0.5)).toBe(25)
expect(scale(1)).toBe(50)
})
it(`should allow to define max value for y axis`, () => {
const scale = linearScale({ axis: 'y', max: 2 }, { y: { min: 0, max: 1 } }, 100, 100)
expect(scale.domain()[1]).toBe(2)
expect(scale(0)).toBe(100)
expect(scale(0.5)).toBe(75)
expect(scale(1)).toBe(50)
})
it(`should allow to reverse domain`, () => {
const scale = linearScale({ axis: 'y', reverse: true }, { y: { min: 0, max: 1 } }, 100, 100)
expect(scale(0)).toBe(0)
expect(scale(0.5)).toBe(50)
expect(scale(1)).toBe(100)
})
it(`should allow to clamping`, () => {
const scale = linearScale(
{ axis: 'y', clamp: true, min: 0.5 },
{ y: { min: 0, max: 1 } },
100,
100
)
expect(scale.domain()[0]).toBe(0.5)
expect(scale(0)).toBe(100)
expect(scale(0.5)).toBe(100)
expect(scale(1)).toBe(0)
})
it(`should allow nice`, () => {
const scale = linearScale(
{ axis: 'x', nice: true },
{ x: { min: 0.243, max: 0.933 } },
100,
100
)
expect(scale(0)).toBe(0)
expect(scale(0.5)).toBe(50)
expect(scale(1)).toBe(100)
})
it(`should allow numeric nice`, () => {
const scale = linearScale({ axis: 'x', nice: 2 }, { x: { min: 0.243, max: 0.933 } }, 100, 100)
expect(scale(0)).toBe(0)
expect(scale(0.5)).toBe(50)
expect(scale(1)).toBe(100)
})