Skip to content

Commit

Permalink
Migrate unit tests to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
apendua committed Jun 4, 2018
1 parent 5442d52 commit 01f5b36
Show file tree
Hide file tree
Showing 49 changed files with 2,397 additions and 2,502 deletions.
6 changes: 4 additions & 2 deletions ddp-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "Connect ddp-redux client to react components",
"main": "lib/index.js",
"scripts": {
"test": "mocha --compilers js:babel-core/register 'src/**/*.test.*'",
"test-watch": "mocha --watch --compilers js:babel-core/register 'src/**/*.test.*'",
"test": "jest",
"test-watch": "jest --watch",
"coverage": "jest --coverage",
"build": "babel src/ -d lib/",
"build-watch": "watch 'npm run build' ./src",
"prepublish": "npm run build"
Expand Down Expand Up @@ -52,6 +53,7 @@
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-react": "^7.1.0",
"jest": "^23.1.0",
"watch": "^1.0.2"
}
}
17 changes: 5 additions & 12 deletions ddp-redux/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
"description": "A redux based DDP client",
"main": "lib/index.js",
"scripts": {
"test": "mocha --require src/shim.js --compilers js:babel-core/register 'src/**/*.test.*'",
"test-watch": "mocha --require src/shim.js --watch --compilers js:babel-core/register 'src/**/*.test.*'",
"test-lib": "mocha --require lib/shim.js 'lib/**/*.test.*'",
"coverage": "babel-node $(npm bin)/isparta cover _mocha -- 'src/**/*.test.js'",
"coverage-report": "http-server ./coverage/lcov-report",
"test": "jest",
"test-watch": "jest --watch",
"coverage": "jest --coverage",
"build": "babel src/ -d lib/",
"build-watch": "watch 'npm run build' ./src",
"prepublish": "npm run build"
Expand Down Expand Up @@ -38,21 +36,16 @@
"babel-preset-flow": "^6.23.0",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",
"chai": "^4.1.0",
"chai-as-promised": "^7.1.1",
"chai": "^4.1.2",
"eslint": "^4.3.0",
"eslint-config-airbnb": "^15.1.0",
"eslint-import-resolver-meteor": "^0.4.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-react": "^7.1.0",
"flow-bin": "^0.52.0",
"isparta": "^4.0.0",
"mocha": "^3.5.0",
"jest": "^23.1.0",
"redux": "^3.7.2",
"redux-mock-store": "^1.2.3",
"sinon": "^2.4.1",
"sinon-chai": "^2.12.0",
"watch": "^1.0.2"
},
"dependencies": {
Expand Down
40 changes: 19 additions & 21 deletions ddp-redux/src/DDPClient.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
/* eslint-env jest */

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import configureStore from 'redux-mock-store';
import DDPClient from './DDPClient';

chai.should();
chai.use(sinonChai);
chai.use(chaiAsPromised);

describe('Test DDPClient', () => {
beforeEach(function () {
this.ddpClient = new DDPClient();
let testContext;

beforeEach(() => {
testContext = {};
});

beforeEach(() => {
testContext.ddpClient = new DDPClient();
});

describe('Given I have a ddp middleware', () => {
beforeEach(function () {
this.middleware = this.ddpClient.middleware();
this.mockStore = configureStore([
this.middleware,
beforeEach(() => {
testContext.middleware = testContext.ddpClient.middleware();
testContext.mockStore = configureStore([
testContext.middleware,
]);
});

it('should accept function as an action', function () {
const store = this.mockStore();
test('should accept function as an action', () => {
const store = testContext.mockStore();
store.dispatch((dispatch) => {
dispatch({
type: 'test_action',
});
});
store.getActions().should.have.deep.members([
expect(store.getActions()).toEqual(expect.arrayContaining([
{ type: 'test_action' },
]);
]));
});
});

it('should be ok', function () {
this.ddpClient.should.be.ok;
test('should be ok', () => {
expect(testContext.ddpClient).toBeTruthy();
});
});
61 changes: 30 additions & 31 deletions ddp-redux/src/DDPEmitter.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
/* eslint-env jest */

import chai from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import DDPEmitter from './DDPEmitter';

chai.should();
chai.use(sinonChai);

describe('Test DDPEmitter', () => {
beforeEach(function () {
this.emitter = new DDPEmitter();
this.m1 = sinon.spy();
this.m2 = sinon.spy();
this.m3 = sinon.spy();
this.emitter.on('m1', this.m1);
this.emitter.on('m2', this.m2);
this.emitter.on('m2', this.m3);
let testContext;

beforeEach(() => {
testContext = {};
});

beforeEach(() => {
testContext.emitter = new DDPEmitter();
testContext.m1 = jest.fn();
testContext.m2 = jest.fn();
testContext.m3 = jest.fn();
testContext.emitter.on('m1', testContext.m1);
testContext.emitter.on('m2', testContext.m2);
testContext.emitter.on('m2', testContext.m3);
});

it('should not do anyghing there are no listeners', function () {
this.emitter.emit('mx');
this.m1.should.not.be.called;
this.m2.should.not.be.called;
this.m3.should.not.be.called;
test('should not do anyghing there are no listeners', () => {
testContext.emitter.emit('mx');
expect(testContext.m1).not.toBeCalled();
expect(testContext.m2).not.toBeCalled();
expect(testContext.m3).not.toBeCalled();
});

it('should trigger one listener', function () {
this.emitter.emit('m1');
this.m1.should.be.called;
this.m2.should.not.be.called;
this.m3.should.not.be.called;
test('should trigger one listener', () => {
testContext.emitter.emit('m1');
expect(testContext.m1).toBeCalled();
expect(testContext.m2).not.toBeCalled();
expect(testContext.m3).not.toBeCalled();
});

it('should trigger two listeners', function () {
this.emitter.emit('m2');
this.m1.should.not.be.called;
this.m2.should.be.called;
this.m3.should.be.called;
test('should trigger two listeners', () => {
testContext.emitter.emit('m2');
expect(testContext.m1).not.toBeCalled();
expect(testContext.m2).toBeCalled();
expect(testContext.m3).toBeCalled();
});
});
63 changes: 31 additions & 32 deletions ddp-redux/src/DDPSocket.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
/* eslint-env jest */

import chai from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import DDPSocket from './DDPSocket';

chai.should();
chai.use(sinonChai);

class Socket {
constructor(endpoint) {
this.messages = [];
Expand Down Expand Up @@ -39,45 +32,51 @@ class Socket {
}

describe('Test DDPSocket', () => {
beforeEach(function () {
this.socket = new DDPSocket({
let testContext;

beforeEach(() => {
testContext = {};
});

beforeEach(() => {
testContext.socket = new DDPSocket({
endpoint: 'ws://example.com',
SocketConstructor: Socket,
});
this.onMessage = sinon.spy();
this.onClose = sinon.spy();
this.onOpen = sinon.spy();
this.socket.on('message', this.onMessage);
this.socket.on('close', this.onClose);
this.socket.on('open', this.onOpen);
this.socket.open('ws://example.com');
testContext.onMessage = jest.fn();
testContext.onClose = jest.fn();
testContext.onOpen = jest.fn();
testContext.socket.on('message', testContext.onMessage);
testContext.socket.on('close', testContext.onClose);
testContext.socket.on('open', testContext.onOpen);
testContext.socket.open('ws://example.com');
});

it('should trigger onOpen callback', function () {
this.socket.rawSocket.triggerOpen();
this.onOpen.should.have.been.called;
test('should trigger onOpen callback', () => {
testContext.socket.rawSocket.triggerOpen();
expect(testContext.onOpen).toBeCalled();
});

it('should trigger onClose callback', function () {
this.socket.close();
this.onClose.should.have.been.called;
test('should trigger onClose callback', () => {
testContext.socket.close();
expect(testContext.onClose).toBeCalled();
});

it('should connect to the right endpoint', function () {
this.socket.rawSocket.endpoint.should.equal('ws://example.com');
test('should connect to the right endpoint', () => {
expect(testContext.socket.rawSocket.endpoint).toBe('ws://example.com');
});

it('should send a stringified DDP message', function () {
this.socket.send({
test('should send a stringified DDP message', () => {
testContext.socket.send({
msg: 'connect',
});
this.socket.rawSocket.messages.should.have.members([
expect(testContext.socket.rawSocket.messages).toEqual(expect.arrayContaining([
'{"msg":"connect"}',
]);
]));
});

it('should receive a parsed DDP message', function () {
this.socket.rawSocket.triggerMessage('{"msg":"ping"}');
this.onMessage.should.have.been.calledWith({ msg: 'ping' });
test('should receive a parsed DDP message', () => {
testContext.socket.rawSocket.triggerMessage('{"msg":"ping"}');
expect(testContext.onMessage).toBeCalledWith({ msg: 'ping' });
});
});
8 changes: 4 additions & 4 deletions ddp-redux/src/ejson/tinytest.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import chai from 'chai';

const test = {
isTrue: chai.assert.isTrue,
isFalse: chai.assert.isFalse,
equal: chai.assert.deepEqual,
throws: chai.assert.throws,
isTrue: chai.assert.isTrue,
isFalse: chai.assert.isFalse,
equal: chai.assert.deepEqual,
throws: chai.assert.throws,
notEqual: chai.assert.notDeepEqual,
};

Expand Down
Loading

0 comments on commit 01f5b36

Please sign in to comment.