forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor-formatters.spec.js
111 lines (101 loc) · 2.9 KB
/
color-formatters.spec.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict'
const { test, given, forCases } = require('sazerac')
const { expect } = require('chai')
const {
coveragePercentage,
colorScale,
letterScore,
age,
version,
} = require('./color-formatters')
describe('Color formatters', function() {
const byPercentage = colorScale([Number.EPSILON, 80, 90, 100])
test(byPercentage, () => {
given(-1).expect('red')
given(0).expect('red')
given(0.5).expect('yellow')
given(1).expect('yellow')
given(50).expect('yellow')
given(80).expect('yellowgreen')
given(85).expect('yellowgreen')
given(90).expect('green')
given(100).expect('brightgreen')
given(101).expect('brightgreen')
forCases(
[-1, 0, 0.5, 1, 50, 80, 85, 90, 100, 101].map(v =>
given(v).expect(coveragePercentage(v))
)
).should("return '%s', for parity with coveragePercentage()")
})
context('when reversed', function() {
test(colorScale([7, 30, 180, 365, 730], undefined, true), () => {
given(3).expect('brightgreen')
given(7).expect('green')
given(10).expect('green')
given(60).expect('yellowgreen')
given(250).expect('yellow')
given(400).expect('orange')
given(800).expect('red')
})
})
test(letterScore, () => {
given('A').expect('brightgreen')
given('B').expect('green')
given('C').expect('yellowgreen')
given('D').expect('yellow')
given('E').expect('orange')
given('F').expect('red')
given('Z').expect('red')
})
const monthsAgo = months => {
const result = new Date()
// This looks wack but it works.
result.setMonth(result.getMonth() - months)
return result
}
test(age, () => {
given(Date.now())
.describe('when given the current timestamp')
.expect('brightgreen')
given(new Date())
.describe('when given the current Date')
.expect('brightgreen')
given(new Date(2001, 1, 1))
.describe('when given a Date many years ago')
.expect('red')
given(monthsAgo(2))
.describe('when given a Date two months ago')
.expect('yellowgreen')
given(monthsAgo(15))
.describe('when given a Date 15 months ago')
.expect('orange')
})
test(version, () => {
forCases([given('1.0'), given(9), given(1.0)]).expect('blue')
forCases([
given(0.1),
given('0.9'),
given('1.0-Beta'),
given('1.1-alpha'),
given('6.0-SNAPSHOT'),
given('1.0.1-dev'),
given('2.1.6-prerelease'),
]).expect('orange')
expect(() => version(null)).to.throw(
Error,
"Can't generate a version color for null"
)
expect(() => version(undefined)).to.throw(
Error,
"Can't generate a version color for undefined"
)
expect(() => version(true)).to.throw(
Error,
"Can't generate a version color for true"
)
expect(() => version({})).to.throw(
Error,
"Can't generate a version color for [object Object]"
)
})
})