jiminy
Version:
Library for inferring which type of data visualization can be rendered from a JSON dataset, and with which data field(s)
113 lines (88 loc) • 3.45 kB
JavaScript
import chai from 'chai';
import Dataset from 'dataset';
const expect = chai.expect;
describe('Dataset', () => {
describe('#constructor', () => {
it('should throw an error if no argument', () => {
expect(() => new Dataset()).to.throw(Error);
});
it('should throw an error if argument isn\'t an array', () => {
expect(() => new Dataset('Vizzuality')).to.throw(Error);
expect(() => new Dataset(5)).to.throw(Error);
expect(() => new Dataset({})).to.throw(Error);
expect(() => new Dataset(() => {})).to.throw(Error);
});
it('should throw an error if array is empty', () => {
expect(() => new Dataset([])).to.throw(Error);
});
it('should not throw an error if the dataset is valid', () => {
expect(() => new Dataset([{ name: 'Vizzuality' }])).to.not.throw(Error);
});
});
describe('getFirstValidValue', () => {
const data = [
{ name: undefined, employees: null },
{ name: null, employees: undefined },
{ name: NaN, employees: null },
{ name: undefined, employees: null },
{ name: null, employees: 1 }
];
const dataset = new Dataset(data);
it('should return null if no valid value in a column', () => {
expect(dataset.getFirstValidValue('name')).to.equal(null);
});
it('should return the first valid value', () => {
expect(dataset.getFirstValidValue('employees')).to.equal(1);
});
});
describe('#atLeast5DistinctValues', () => {
const data = [
{ name: 'Vizzuality', employees: 1 },
{ name: 'Vizzuality', employees: 2 },
{ name: 'Vizzuality', employees: 3 },
{ name: 'Vizzuality', employees: 4 },
{ name: 'Vizzuality', employees: 5 }
];
const dataset = new Dataset(data);
it('should return false if a column has less than 5 different values', () => {
expect(dataset.atLeast5DistinctValues('name')).to.be.false;
});
it('should return true if a column has at least 5 different values', () => {
expect(dataset.atLeast5DistinctValues('employees')).to.be.true;
});
});
describe('#getColumnNames', () => {
const data = [{ name: 'Vizzuality', active: true }];
const dataset = new Dataset(data);
it('should return the columns of the dataset', () => {
const res = Object.keys(data[0]);
expect(dataset.getColumnNames()).to.deep.equal(res);
});
});
describe('#_validValue', () => {
let dataset;
before(() => {
dataset = new Dataset([{ name: 'Vizzuality' }]);
});
it('should return false when value is undefined', () => {
expect(dataset._validValue(undefined)).to.be.false;
});
it('should return false when the value is null', () => {
expect(dataset._validValue(null)).to.be.false;
});
it('should return false when the value is NaN', () => {
expect(dataset._validValue(NaN)).to.be.false;
});
it('should return false when the value is an array', () => {
expect(dataset._validValue(['Vizzuality'])).to.be.false;
});
it('should retuen false when the value is an object', () => {
expect(dataset._validValue({ name: 'Vizzuality' })).to.be.false;
});
it('should return true for a string, a number or a boolean', () => {
expect(dataset._validValue(false)).to.be.true;
expect(dataset._validValue('Vizzuality')).to.be.true;
expect(dataset._validValue(3)).to.be.true;
});
});
});