-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeTextHistogram.test.js
More file actions
65 lines (64 loc) · 3.08 KB
/
Copy pathmakeTextHistogram.test.js
File metadata and controls
65 lines (64 loc) · 3.08 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
describe('makeTextHistogram', () => {
it('should create a text histogram from an object of counts', () => {
const data = { apple: 10, banana: 5, cherry: 15 }
const histogram = makeTextHistogram(data)
const expected = ` apple | █████████████ 10
banana | ███████ 5
cherry | ████████████████████ 15`
expect(histogram).toBe(expected)
})
it('should use the specified character for the bars', () => {
const data = { apple: 10, banana: 5, cherry: 15 }
const histogram = makeTextHistogram(data, { char: '#' })
const expected = ` apple | ############# 10
banana | ####### 5
cherry | #################### 15`
expect(histogram).toBe(expected)
})
it('should limit the length of the bars to the specified maximum', () => {
const data = { apple: 10, banana: 5, cherry: 15 }
const histogram = makeTextHistogram(data, { length: 10 })
const expected = ` apple | ███████ 10
banana | ███ 5
cherry | ██████████ 15`
expect(histogram).toBe(expected)
})
it('should sort the entries by the specified function', () => {
const data = { cherry: 15, apple: 10, banana: 5 }
const histogram = makeTextHistogram(data, { sortBy: (key, value) => value })
const expected = `banana | ███████ 5
apple | █████████████ 10
cherry | ████████████████████ 15`
expect(histogram).toBe(expected)
})
it('should sort the entries by the specified function (other function)', () => {
const data = { cherry: 15, apple: 10, banana: 5 }
const histogram = makeTextHistogram(data, { sortBy: (key, value) => key })
const expected = ` apple | █████████████ 10
banana | ███████ 5
cherry | ████████████████████ 15`
expect(histogram).toBe(expected)
})
it('should handle an empty object', () => {
const data = {}
const histogram = makeTextHistogram(data)
const expected = ``
expect(histogram).toBe(expected)
})
it('should handle values of zero', () => {
const data = { apple: 0, banana: 5, cherry: 10 }
const histogram = makeTextHistogram(data)
const expected = ` apple | 0
banana | ██████████ 5
cherry | ████████████████████ 10`
expect(histogram).toBe(expected)
})
it('should handle all values being the same', () => {
const data = { apple: 10, banana: 10, cherry: 10 }
const histogram = makeTextHistogram(data)
const expected = ` apple | ████████████████████ 10
banana | ████████████████████ 10
cherry | ████████████████████ 10`
expect(histogram).toBe(expected)
})
})