Skip to content

Commit

Permalink
test: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Oct 28, 2020
1 parent 33a60a6 commit 1cd9d3d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 6 deletions.
18 changes: 17 additions & 1 deletion src/data/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { formatAPITime } from './utils';
import { formatAPITime, parseAPITime } from './utils';

describe('formatAPITime', () => {
test('matches api format', () => {
const d = new Date(2020, 1, 2);
expect(formatAPITime(d)).toBe('20200202');
});
});

describe('parseAPITime', () => {
test('default', () => {
const d = new Date(2020, 1, 2);
expect(parseAPITime(20200202).valueOf()).toBe(d.valueOf());
});
test('round trip', () => {
const d = new Date(2020, 5, 8);
expect(parseAPITime(formatAPITime(d)).valueOf()).toBe(d.valueOf());
});
test('strip to date', () => {
const d = new Date(2020, 5, 8, 5, 2);
const day = new Date(2020, 5, 8);
expect(parseAPITime(formatAPITime(d)).valueOf()).toBe(day.valueOf());
});
});
6 changes: 3 additions & 3 deletions src/maps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import countyRaw from './processed/county.csv.js';
// import neighborhoodRaw from './processed/swpa/neighborhood.csv.js';
// import zipRaw from './processed/swpa/zip.csv.js';
// import swpaFilterInfo from './processed/swpa/filterInfo.json';
import { levelMegaCounty } from '../stores/constants';

const levelMegaCountyId = 'mega-county';
/**
* @typedef {object} NameInfo
* @property {string} name name for param
Expand Down Expand Up @@ -47,7 +47,7 @@ const megaCountyInfo = stateInfo.map((info) => ({
name: `Rest of ${info.name}`,
displayName: `Rest of ${info.displayName}`,
population: null,
level: levelMegaCounty.id,
level: levelMegaCountyId,
lat: null,
long: null,
}));
Expand All @@ -61,7 +61,7 @@ export const bounds = boundsInfo;
export function loadSources(additionalProperties = {}) {
// mark to be loaded as fast as possible
return import(/* webpackPreload: true */ './geo').then((r) =>
r.default(stateInfo, countyInfo, msaInfo, levelMegaCounty.id, additionalProperties),
r.default(stateInfo, countyInfo, msaInfo, levelMegaCountyId, additionalProperties),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/stores/colorScales.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const lookup = {
interpolateYlOrBr,
};

const DEFAULT_COLOR_SCALE = interpolateYlOrRd;
export const DEFAULT_COLOR_SCALE = interpolateYlOrRd;

export function resolveColorScale(colorScale) {
if (!colorScale) {
Expand Down
20 changes: 20 additions & 0 deletions src/stores/colorScales.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { resolveColorScale, DEFAULT_COLOR_SCALE } from './colorScales';
import { interpolateBlues } from 'd3-scale-chromatic';

describe('colorScales', () => {
test('default', () => {
expect(typeof resolveColorScale()).toBe('function');
expect(typeof resolveColorScale()(0.5)).toBe('string');
expect(resolveColorScale()).toBe(DEFAULT_COLOR_SCALE);
});
test('named', () => {
expect(resolveColorScale('interpolateBlues')).toBe(interpolateBlues);
});
test('invalid name', () => {
expect(resolveColorScale('interpolateBluesX')).toBe(DEFAULT_COLOR_SCALE);
});
test('function', () => {
const v = () => 'red';
expect(resolveColorScale(v)).toBe(v);
});
});
3 changes: 2 additions & 1 deletion src/stores/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { formatAPITime, isCasesSignal, isDeathSignal, isPropSignal, isCountSignal } from '../data';
import { isCasesSignal, isDeathSignal, isPropSignal, isCountSignal } from '../data/signals';
import { formatAPITime } from '../data/utils';
import { format } from 'd3-format';
import descriptions from './descriptions.generated.json';
import '!file-loader?name=descriptions.raw.txt!./descriptions.raw.txt';
Expand Down
29 changes: 29 additions & 0 deletions src/stores/constants.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference types="jest" />
import { defaultSensorId, EPIDATA_CASES_OR_DEATH_VALUES, getLevelInfo, sensorList } from './constants';

describe('defaultSensorId', () => {
test('matches', () => {
expect(defaultSensorId).toBe('doctor-visits');
});
});

describe('sensorList', () => {
test.each(sensorList.map((d) => [d]))('has structure %s', (sensor) => {
expect(typeof sensor.key).toBe('string');
expect(typeof sensor.name).toBe('string');
if (sensor.isCasesOrDeath) {
expect(Object.keys(sensor.casesOrDeathSignals)).toEqual(EPIDATA_CASES_OR_DEATH_VALUES);
}
});
});

describe('getLevelInfo', () => {
test('existing', () => {
expect(getLevelInfo('county').id).toBe('county');
expect(getLevelInfo('county').label).toBe('County');
});
test('invalid', () => {
expect(getLevelInfo('test').id).toBe('test');
expect(getLevelInfo('test').label).toBe('TEST');
});
});
33 changes: 33 additions & 0 deletions src/util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getTextColorBasedOnBackground, zip } from './util';
import { rgb } from 'd3-color';

describe('getTextColorBasedOnBackground', () => {
test('white text on black bg', () => {
expect(rgb(getTextColorBasedOnBackground('black')).formatHex()).toBe('#ffffff');
});
test('black text on dark grey bg', () => {
expect(rgb(getTextColorBasedOnBackground('darkgrey')).formatHex()).toBe('#000000');
});
test('black text on white bg', () => {
expect(rgb(getTextColorBasedOnBackground('white')).formatHex()).toBe('#000000');
});
});

describe('zip', () => {
test('simple', () => {
expect(zip([0, 1, 2], [1, 2, 4])).toEqual([
[0, 1],
[1, 2],
[2, 4],
]);
expect(zip([0, 1, 2], [1, 2])).toEqual([
[0, 1],
[1, 2],
[2, undefined],
]);
expect(zip([0, 1], [1, 2, 3])).toEqual([
[0, 1],
[1, 2],
]);
});
});

0 comments on commit 1cd9d3d

Please sign in to comment.