1
|
|
|
/* eslint-disable no-undef */ |
2
|
|
|
import { roundPoint, formatNumber } from '../src/js/number'; |
3
|
|
|
import { createCurves, createVerticalCurves, createPath } from '../src/js/path'; |
4
|
|
|
import { generateLegendBackground, areEqual } from '../src/js/graph'; |
5
|
|
|
import generateRandomIdString from '../src/js/random'; |
6
|
|
|
import FunnelGraph from '../index'; |
7
|
|
|
|
8
|
|
|
const assert = require('assert'); |
9
|
|
|
|
10
|
|
|
describe('Check randomly generated ids', () => { |
11
|
|
|
const generatedIds = []; |
12
|
|
|
it('don\'t collide often', () => { |
13
|
|
|
for(let i = 0; i < 1000; i++) { |
14
|
|
|
const newlyGeneratedId = generateRandomIdString(); |
15
|
|
|
for(let j = 0; j < generatedIds.length; j++) { |
16
|
|
|
const previouslyGeneratedId = generatedIds[j]; |
17
|
|
|
assert.notEqual(newlyGeneratedId, previouslyGeneratedId); |
18
|
|
|
} |
19
|
|
|
generatedIds.push(newlyGeneratedId); |
20
|
|
|
} |
21
|
|
|
}); |
22
|
|
|
it('have correct prefix', () => { |
23
|
|
|
['test', '__', '-prefix-'].forEach(prefix => { |
24
|
|
|
const generatedId = generateRandomIdString(prefix); |
25
|
|
|
assert.equal(0, generatedId.indexOf(prefix)); |
26
|
|
|
}); |
27
|
|
|
}); |
28
|
|
|
it('are longer than just the prefix', () => { |
29
|
|
|
['test', '__', '-prefix-', ''].forEach(prefix => { |
30
|
|
|
const generatedId = generateRandomIdString(prefix); |
31
|
|
|
assert.equal(true, generatedId.length > prefix.length); |
32
|
|
|
}); |
33
|
|
|
}); |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
describe('Test number functions', () => { |
37
|
|
|
it('round number test', () => { |
38
|
|
|
assert.equal(roundPoint(19.99999999998), 20); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
it('number format test', () => { |
42
|
|
|
assert.equal(formatNumber(12500), '12,500'); |
43
|
|
|
}); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
describe('Add tests for paths', () => { |
47
|
|
|
it('can create points for curves', () => { |
48
|
|
|
assert.equal(createCurves(0, 0, 6, 2), ' C3,0 3,2 6,2'); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
it('can create points for vertical curves', () => { |
52
|
|
|
assert.equal(createVerticalCurves(0, 0, 6, 2), ' C0,1 6,1 6,2'); |
53
|
|
|
}); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
describe('Add tests for background color generator', () => { |
57
|
|
|
it('can generate a solid background', () => { |
58
|
|
|
assert.equal(generateLegendBackground('red'), 'background-color: red'); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
it('can generate a solid background from an array with single element', () => { |
62
|
|
|
assert.equal(generateLegendBackground(['red']), 'background-color: red'); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
it('can generate a gradient background', () => { |
66
|
|
|
assert.equal( |
67
|
|
|
generateLegendBackground(['red', 'orange']), |
68
|
|
|
'background-image: linear-gradient(to right, red, orange)' |
69
|
|
|
); |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
it('can generate a vertical gradient background', () => { |
73
|
|
|
assert.equal( |
74
|
|
|
generateLegendBackground(['red', 'orange'], 'vertical'), |
75
|
|
|
'background-image: linear-gradient(red, orange)' |
76
|
|
|
); |
77
|
|
|
}); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
describe('Add tests for equality method', () => { |
81
|
|
|
it('can compare one dimensional arrays', () => { |
82
|
|
|
assert.strictEqual(areEqual([10, 20, 30], [10, 20, 30]), true); |
83
|
|
|
assert.notStrictEqual(areEqual([10, 20, 31], [10, 20, 30]), true); |
84
|
|
|
assert.notStrictEqual(areEqual([10, 20, 30, 40], [10, 20, 30]), true); |
85
|
|
|
}); |
86
|
|
|
it('can compare two dimensional arrays', () => { |
87
|
|
|
assert.strictEqual(areEqual([ |
88
|
|
|
[10, 20, 30], ['a', 'b', 'c'], [1, 'b', 0] |
89
|
|
|
], [ |
90
|
|
|
[10, 20, 30], ['a', 'b', 'c'], [1, 'b', 0] |
91
|
|
|
]), true); |
92
|
|
|
assert.notStrictEqual(areEqual([ |
93
|
|
|
[10, 20, 30], ['a', 'b', 'c'], [1, 'b', 0] |
94
|
|
|
], [ |
95
|
|
|
[10, 20, 30], ['a', 'b', 'c'], [1, 'b', 'c'] |
96
|
|
|
]), true); |
97
|
|
|
}); |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
describe('Add tests for paths', () => { |
101
|
|
|
const data = { |
102
|
|
|
labels: ['Impressions', 'Add To Cart', 'Buy'], |
103
|
|
|
subLabels: ['Direct', 'Social Media', 'Ads', 'Other'], |
104
|
|
|
colors: [ |
105
|
|
|
['#FFB178', '#FF78B1', '#FF3C8E'], |
106
|
|
|
['#A0BBFF', '#EC77FF'], |
107
|
|
|
['#A0F9FF', '#B377FF'], |
108
|
|
|
'#E478FF' |
109
|
|
|
], |
110
|
|
|
values: [ |
111
|
|
|
[2000, 4000, 6000, 500], |
112
|
|
|
[3000, 1000, 1700, 600], |
113
|
|
|
[800, 300, 130, 400] |
114
|
|
|
] |
115
|
|
|
}; |
116
|
|
|
|
117
|
|
|
const graph = new FunnelGraph({ |
118
|
|
|
container: '.funnel', |
119
|
|
|
gradientDirection: 'horizontal', |
120
|
|
|
data, |
121
|
|
|
displayPercent: true, |
122
|
|
|
direction: 'horizontal', |
123
|
|
|
width: 90, |
124
|
|
|
height: 60 |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
it('can create main axis points for curves', () => { |
128
|
|
|
assert.deepEqual(graph.getMainAxisPoints(), [0, 30, 60, 90]); |
129
|
|
|
}); |
130
|
|
|
|
131
|
|
|
it('can create main axis points for curves', () => { |
132
|
|
|
assert.deepEqual(graph.getCrossAxisPoints(), [ |
133
|
|
|
[0, 14.9, 26.1, 26.1], |
134
|
|
|
[9.6, 29.3, 29.9, 29.9], |
135
|
|
|
[28.8, 34.1, 31.3, 31.3], |
136
|
|
|
[57.6, 42.3, 31.9, 31.9], |
137
|
|
|
[60, 45.1, 33.9, 33.9] |
138
|
|
|
]); |
139
|
|
|
}); |
140
|
|
|
|
141
|
|
|
it('can create all paths', () => { |
142
|
|
|
const length = graph.getCrossAxisPoints().length - 1; |
143
|
|
|
const paths = []; |
144
|
|
|
|
145
|
|
|
for (let i = 0; i < length; i++) { |
146
|
|
|
const X = graph.getMainAxisPoints(); |
147
|
|
|
const Y = graph.getCrossAxisPoints()[i]; |
148
|
|
|
const YNext = graph.getCrossAxisPoints()[i + 1]; |
149
|
|
|
const d = createPath(i, X, Y, YNext); |
150
|
|
|
|
151
|
|
|
paths.push(d); |
152
|
|
|
} |
153
|
|
|
assert.deepEqual(paths, ['M0,0 C15,0 15,14.9 30,14.9 C45,14.9 45,26.1 60,26.1 C75,26.1 75,26.1 90,26.1 L90,29.9 C75,29.9 75,29.9 60,29.9 C45,29.9 45,29.3 30,29.3 C15,29.3 15,9.6 0,9.6 Z', |
154
|
|
|
'M0,9.6 C15,9.6 15,29.3 30,29.3 C45,29.3 45,29.9 60,29.9 C75,29.9 75,29.9 90,29.9 L90,31.3 C75,31.3 75,31.3 60,31.3 C45,31.3 45,34.1 30,34.1 C15,34.1 15,28.8 0,28.8 Z', |
155
|
|
|
'M0,28.8 C15,28.8 15,34.1 30,34.1 C45,34.1 45,31.3 60,31.3 C75,31.3 75,31.3 90,31.3 L90,31.9 C75,31.9 75,31.9 60,31.9 C45,31.9 45,42.3 30,42.3 C15,42.3 15,57.6 0,57.6 Z', |
156
|
|
|
'M0,57.6 C15,57.6 15,42.3 30,42.3 C45,42.3 45,31.9 60,31.9 C75,31.9 75,31.9 90,31.9 L90,33.9 C75,33.9 75,33.9 60,33.9 C45,33.9 45,45.1 30,45.1 C15,45.1 15,60 0,60 Z']); |
157
|
|
|
}); |
158
|
|
|
|
159
|
|
|
it('can update data', () => { |
160
|
|
|
const updatedData = { |
161
|
|
|
values: [ |
162
|
|
|
[3500, 3500, 7500], |
163
|
|
|
[3300, 5400, 5000], |
164
|
|
|
[600, 600, 6730] |
165
|
|
|
] |
166
|
|
|
}; |
167
|
|
|
|
168
|
|
|
graph.values = FunnelGraph.getValues({ data: updatedData }); |
169
|
|
|
|
170
|
|
|
assert.deepEqual(graph.getMainAxisPoints(), [0, 30, 60, 90]); |
171
|
|
|
assert.deepEqual(graph.getCrossAxisPoints(), [ |
172
|
|
|
[0, 1.7, 13.6, 13.6], |
173
|
|
|
[14.5, 15.3, 16.1, 16.1], |
174
|
|
|
[29, 37.6, 18.6, 18.6], |
175
|
|
|
[60, 58.3, 46.4, 46.4] |
176
|
|
|
]); |
177
|
|
|
}); |
178
|
|
|
}); |
179
|
|
|
|