Files
shields/lib/color-formatters.spec.js
Pyves 7039e68018 Consistent version formatting (#1246)
Provide greater consistency for badges related to versions. Fix #1181.

- the `version` function in `color-formatters` was previously returning the colour of the badge, but also its text with a leading _v_. It was broken down into two separate functions and the text formatting part was moved to `text-formatters`, where it really belongs.

- unit tests were added for these two functions in `color-formatters.spec` and `text-formatters.spec`, using Sazerac.

- as discussed in #1181, the leading _v_ was omitted  for _xxxx-yy-zz_ date patterns. Any future exceptions can easily be added to the `ignoredVersionPatterns` pattern.

- the badge colour was previously switched to orange if a hyphen was found in the version string. This didn't seem ideal, instead pattern matching is done to find keywords such as `beta`, `alpha` or `snapshot`. Of course, this list can easily be extended.

- all badges related to versions now use the `versionText` and `versionColor` functions. There are a few rare exceptions, for instance in cases where the data returned by the service's API allows to figure things out without relying on any parsing/pattern matching (eg. `badgeData.colorscheme = prerelease ? 'orange' : 'blue';`, where `prerelease` is determined from an API's response).
2017-11-11 17:54:38 -05:00

70 lines
2.1 KiB
JavaScript

'use strict';
const { test, given, forCases } = require('sazerac');
const {
coveragePercentage,
colorScale,
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');
});
});
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, () => {
given('1.0').expect('blue');
forCases([
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');
});
});