Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pugnascotia committed Dec 11, 2018
1 parent 58c384a commit 46965bd
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/services/color/color_palette.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { colorPalette} from './color_palette';

describe('colorPalette', () => {
it('should generate the expected palette', () => {
const actualPalette = colorPalette('#FFFF6D', '#1EA593');
expect(actualPalette).toEqual([
'#FFFF6D',
'#E6F571',
'#CDEB75',
'#B4E17A',
'#9BD77E',
'#82CD82',
'#69C386',
'#50B98B',
'#37AF8F',
'#1EA593',
]);
});

it('should generate a palette with the specified spread', () => {
const actualPalette = colorPalette('#FFFF6D', '#1EA593', 6);
expect(actualPalette).toEqual([
'#FFFF6D',
'#D2ED75',
'#A5DB7C',
'#78C984',
'#4BB78B',
'#1EA593',
]);
});
});
20 changes: 20 additions & 0 deletions src/services/color/hex_to_rgb.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { hexToRgb } from './hex_to_rgb';

describe('hexToRgb ', () => {

it('should handle 3 digit codes without a hash prefix', () => {
expect(hexToRgb('0a8')).toEqual([0, 170, 136]);
});

it('should handle 3 digit codes with a hash prefix', () => {
expect(hexToRgb('#0a8')).toEqual([0, 170, 136]);
});

it('should handle 6 digit codes without a hash prefix', () => {
expect(hexToRgb('00aa88')).toEqual([0, 170, 136]);
});

it('should handle 6 digit codes with a hash prefix', () => {
expect(hexToRgb('#00aa88')).toEqual([0, 170, 136]);
});
});
19 changes: 19 additions & 0 deletions src/services/color/rgb_to_hex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { rgbToHex } from './rgb_to_hex';

describe('rgbToHex ', () => {
it('should handle rgb() without whitespace', () => {
expect(rgbToHex('rgb(12,34,56)')).toEqual('#0c2238');
});

it('should handle rgb() with whitespace', () => {
expect(rgbToHex('rgb ( 12 , 34 , 56 )')).toEqual('#0c2238');
});

it('should handle rgba() without whitespace', () => {
expect(rgbToHex('rgba(12,34,56,0.4)')).toEqual('#0c2238');
});

it('should handle rgba() with whitespace', () => {
expect(rgbToHex('rgba ( 12 , 34 , 56 , 0.4 )')).toEqual('#0c2238');
});
});
77 changes: 77 additions & 0 deletions src/services/random.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import moment from 'moment';
import { Random } from './random';

describe('Random', () => {
it('should generate booleans', () => {
const trueRandom = new Random(() => 0.51);
const falseRandom = new Random(() => 0.5);

expect(trueRandom.boolean()).toEqual(true);
expect(falseRandom.boolean()).toEqual(false);
});

it('should generate numbers', () => {
const random = new Random(() => 0.42);

expect(random.number()).toEqual(7.550311166421726e+307);
expect(random.number({ min: 5 })).toEqual(7.550311166421726e+307);
expect(random.number({ max: 10 })).toEqual(4.2);
expect(random.number({ min: 5, max: 10 })).toEqual(7.1);
expect(random.number({ min: -10, max: 10 })).toBeCloseTo(-1.60);
});

it('should generate integers', () => {
const random = new Random(() => 0.42);

expect(random.integer({ min: 0, max: 10 })).toEqual(4);
expect(random.integer({ min: -10, max: 10 })).toEqual(-2);
});

it('should select an array value', () => {
const random = new Random(() => 0.42);
const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

expect(random.oneOf(values)).toEqual(5);
});

it('should select the specified array value', () => {
const random = new Random(() => 0.42);
const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

expect(random.oneToOne(values, 4)).toEqual(5);
});

it('should select a set of random size', () => {
const random = new Random(() => 0.42);
const input = [12, 34, 56, 78, 90, 3434, 12313212, 3, 0];

expect(random.setOf(input)).toEqual([78, 90, 56, 3434]);
});

it('should select a set of random size, with a size constraint', () => {
const random = new Random(() => 0.42);
const input = [12, 34, 56, 78, 90, 3434, 12313212, 3, 0];

expect(random.setOf(input, { max: 5 })).toEqual([78, 90]);
});

it('should generate a date', () => {
const random = new Random(() => 0.42);

// The default max value is now, so we must specify a max in order to keep
// the test deterministic.
const actual = random.date({ max: new Date(Date.parse('2018-12-25T12:23:34.123')) });

expect(actual).toEqual(new Date(Date.parse('1990-07-29T00:24:17.932Z')));
});

it('should generate a moment', () => {
const random = new Random(() => 0.42);

// The default max value is now, so we must specify a max in order to keep
// the test deterministic.
const actual = random.moment({ max: moment('2018-12-25T12:23:34.123') });

expect(actual.toISOString()).toEqual('1990-07-29T00:24:17.932Z');
});
});
2 changes: 1 addition & 1 deletion src/services/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Random {
return values[index];
}

setOf = <T>(values: T[], options: { min?: number, max?: number }): T[] => {
setOf = <T>(values: T[], options: { min?: number, max?: number } = {}): T[] => {
const count = this.integer({ min: 0, max: values.length, ...options });
const copy = [...values];
return times(count, () => {
Expand Down

0 comments on commit 46965bd

Please sign in to comment.