Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
☔ Add diff and helper tests
  • Loading branch information
thiamsantos committed May 16, 2017
commit 735499e14ed5cd0f4a95d162be897f97833d9835
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
},
"eslintConfig": {
"extends": "airbnb",
"rules": {
"no-console": "off"
},
"env": {
"browser": true
"browser": true,
"mocha": true
}
},
"nyc": {
Expand All @@ -37,6 +41,7 @@
"example",
"lib",
"dist",
"coverage",
"rollup.config.js"
]
},
Expand Down
115 changes: 115 additions & 0 deletions spec/diff.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import sinon from 'sinon';
import { expect } from 'chai';
import { style, render, default as diffLogger } from '../src/diff';

context('Diff', () => {
describe('style', () => {
it('return css rules for the given kind of diff changes', () => {
expect(style('E')).to.equal('color: #2196F3; font-weight: bold');
expect(style('N')).to.equal('color: #4CAF50; font-weight: bold');
expect(style('D')).to.equal('color: #F44336; font-weight: bold');
expect(style('A')).to.equal('color: #2196F3; font-weight: bold');
});
});

describe('render', () => {
it('should return an array indicating the changes', () => {
expect(render({
kind: 'E',
path: ['capitain', 'name'],
lhs: 'kirk',
rhs: 'picard',
})).to.eql(['capitain.name', 'kirk', '→', 'picard']);
});

it('should return an array indicating an added property/element', () => {
expect(render({
kind: 'N',
path: ['crew', 'engineer'],
rhs: 'geordi',
})).to.eql(['crew.engineer', 'geordi']);
});

it('should return an array indicating a removed property/element', () => {
expect(render({
kind: 'D',
path: ['crew', 'security'],
})).to.eql(['crew.security']);
});

it('should return an array indicating a changed index', () => {
expect(render({
kind: 'A',
path: ['crew'],
index: 2,
item: {
kind: 'N',
rhs: 'after',
},
})).to.eql(['crew[2]', {
kind: 'N',
rhs: 'after',
}]);
});

it('should return an empty array', () => {
expect(render({})).to.eql([]);
});
});

describe('diffLogger', () => {
let logger

beforeEach(() => {
logger = {
log: sinon.spy(),
groupCollapsed: sinon.spy(),
groupEnd: sinon.spy(),
group: sinon.spy(),
};
});

it('should show no diff with group collapsed', () => {
diffLogger({}, {}, logger, true);

expect(logger.group.calledOnce).to.be.false;
expect(logger.groupCollapsed.calledOnce).to.be.true;
expect(logger.groupEnd.calledOnce).to.be.true;
expect(logger.log.calledOnce).to.be.true;
expect(logger.log.calledWith('—— no diff ——')).to.be.true;
});

it('should show no diff with group not collapsed', () => {
diffLogger({}, {}, logger, false);

expect(logger.group.calledOnce).to.be.true;
expect(logger.groupCollapsed.calledOnce).to.be.false;
expect(logger.groupEnd.calledOnce).to.be.true;
expect(logger.log.calledOnce).to.be.true;
expect(logger.log.calledWith('—— no diff ——')).to.be.true;
});

it('should log no diff without group', () => {
const loggerWithNoGroupCollapsed = Object.assign({}, logger, {
groupCollapsed: () => {
throw new Error()
},
groupEnd: () => {
throw new Error()
},
});

diffLogger({}, {}, loggerWithNoGroupCollapsed, true);

expect(loggerWithNoGroupCollapsed.log.calledWith('diff')).to.be.true;
expect(loggerWithNoGroupCollapsed.log.calledWith('—— no diff ——')).to.be.true;
expect(loggerWithNoGroupCollapsed.log.calledWith('—— diff end —— ')).to.be.true;
});

it('should log the diffs', () => {
diffLogger({name: 'kirk'}, {name: 'picard'}, logger, false);

expect(logger.log.calledWithExactly('%c CHANGED:', 'color: #2196F3; font-weight: bold', 'name', 'kirk', '→', 'picard')).to.be.true;
});
});
});
23 changes: 23 additions & 0 deletions spec/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect } from 'chai';
import { repeat, pad, formatTime } from '../src/helpers';

context('Helpers', () => {
describe('repeat', () => {
it('should repeat a string the number of indicated times', () => {
expect(repeat('teacher', 3)).to.equal('teacherteacherteacher');
});
});

describe('pad', () => {
it('should add leading zeros to a number given a maximun length', () => {
expect(pad(56, 4)).to.equal('0056');
});
});

describe('formatTime', () => {
it('should format a time given a Date object', () => {
const time = new Date('December 25, 1995 23:15:30');
expect(formatTime(time)).to.equal('23:15:30.000');
});
});
});
38 changes: 13 additions & 25 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,47 @@
import { expect } from 'chai';
import sinon from 'sinon';

import { applyMiddleware, createStore } from 'redux';
import { default as logger, createLogger } from '../src';

import { repeat } from '../src/helpers';
import logger, { createLogger } from '../src';

context(`Helpers`, () => {
describe(`repeat`, () => {
it(`should repeat a string the number of indicated times`, () => {
expect(repeat(`teacher`, 3)).to.equal(`teacherteacherteacher`);
});
});
});

context(`default logger`, () => {
describe(`init`, () => {
context('default logger', () => {
describe('init', () => {
beforeEach(() => {
sinon.spy(console, `error`);
sinon.spy(console, 'error');
});

afterEach(() => {
console.error.restore();
});

it(`should be ok`, () => {
it('should be ok', () => {
const store = createStore(() => ({}), applyMiddleware(logger));

store.dispatch({ type: `foo` });
store.dispatch({ type: 'foo' });
sinon.assert.notCalled(console.error);
});
});
});

context(`createLogger`, () => {
describe(`init`, () => {
context('createLogger', () => {
describe('init', () => {
beforeEach(() => {
sinon.spy(console, `error`);
sinon.spy(console, 'error');
});

afterEach(() => {
console.error.restore();
});

it(`should throw error if passed direct to applyMiddleware`, () => {
it('should throw error if passed direct to applyMiddleware', () => {
const store = createStore(() => ({}), applyMiddleware(createLogger));

store.dispatch({ type: `foo` });
store.dispatch({ type: 'foo' });
sinon.assert.calledOnce(console.error);
});

it(`should be ok`, () => {
it('should be ok', () => {
const store = createStore(() => ({}), applyMiddleware(createLogger()));

store.dispatch({ type: `foo` });
store.dispatch({ type: 'foo' });
sinon.assert.notCalled(console.error);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const dictionary = {
},
};

function style(kind) {
export function style(kind) {
return `color: ${dictionary[kind].color}; font-weight: bold`;
}

function render(diff) {
export function render(diff) {
const { kind, path, lhs, rhs, index, item } = diff;

switch (kind) {
Expand Down