From 6d89a4b46bebe358659917edb40385acdd41624c Mon Sep 17 00:00:00 2001 From: giuband Date: Tue, 10 Jan 2017 21:12:17 +0100 Subject: [PATCH 1/5] Add support for jasmine 2.x --- package.json | 2 ++ src/jasmine.js | 54 ++++++++++++++++++++++++++++++++++++ test/jasmine/index.js | 58 +++++++++++++++++++++++++++++++++++++++ test/jasmine/jasmine.json | 9 ++++++ 4 files changed, 123 insertions(+) create mode 100644 src/jasmine.js create mode 100644 test/jasmine/index.js create mode 100644 test/jasmine/jasmine.json diff --git a/package.json b/package.json index 2c39699..b0c3f92 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "test:chai": "mocha --compilers js:babel-register --reporter spec test/chai/*.js", "test:expect": "mocha --compilers js:babel-register --reporter spec test/expect/*.js", "test:expectjs": "mocha --compilers js:babel-register --reporter spec test/expectjs/*.js", + "test:jasmine": "jasmine JASMINE_CONFIG_PATH=test/jasmine/jasmine.json", "test:should": "mocha --compilers js:babel-register --reporter spec test/should/*.js", "test:tape": "tape --require babel-register test/tape/*.js", "test": "npm run test:index && npm run test:chai && npm run test:expect && npm run test:expectjs && npm run test:should && npm run test:tape", @@ -36,6 +37,7 @@ "eslint-plugin-import": "^1.8.0", "expect": "^1.20.1", "expect.js": "^0.3.1", + "jasmine": "^2.5.2", "mocha": "^2.4.5", "redux-thunk": "^2.1.0", "rimraf": "^2.5.2", diff --git a/src/jasmine.js b/src/jasmine.js new file mode 100644 index 0000000..6085655 --- /dev/null +++ b/src/jasmine.js @@ -0,0 +1,54 @@ +/* eslint-env jasmine */ +import { assertions } from 'redux-actions-assertions-js'; + +const toDispatchActions = () => { + return { + compare(action, expectedActions, done) { + assertions.toDispatchActions(action, expectedActions, done, done.fail); + return { pass: true }; + } + }; +}; + +const toNotDispatchActions = () => { + return { + compare(action, expectedActions, done) { + assertions.toNotDispatchActions(action, expectedActions, done, done.fail); + return { pass: true }; + } + }; +}; + +const toDispatchActionsWithState = () => { + return { + compare(action, state, expectedActions, done) { + assertions.toDispatchActionsWithState(state, action, expectedActions, done, done.fail); + return { pass: true }; + } + }; +}; + +const toNotDispatchActionsWithState = () => { + return { + compare(action, state, expectedActions, done) { + assertions.toNotDispatchActionsWithState(state, action, expectedActions, done, done.fail); + return { pass: true }; + } + }; +}; + +const matchers = { + toDispatchActions, + toNotDispatchActions, + toDispatchActionsWithState, + toNotDispatchActionsWithState +}; + +const registerAssertions = () => { + jasmine.addMatchers(matchers); +}; + +export { + registerAssertions, + matchers +}; diff --git a/test/jasmine/index.js b/test/jasmine/index.js new file mode 100644 index 0000000..13e0435 --- /dev/null +++ b/test/jasmine/index.js @@ -0,0 +1,58 @@ +/* eslint-env jasmine */ +import thunk from 'redux-thunk'; +import { registerMiddlewares } from '../../src'; +import { registerAssertions } from '../../src/jasmine'; +import actions from '../testingData/actions'; + +registerMiddlewares([thunk]); + +describe('jasmine', () => { + beforeEach(() => { registerAssertions(); }); + describe('toDispatchActionsWithState', () => { + it('should accept object', (done) => { + const state = { property: 'value' }; + expect(actions.actionCreatorWithGetState()) + .toDispatchActionsWithState(state, actions.actionWithGetState({ property: 'value' }), done); + }); + }); + + describe('.toDispatchActions', () => { + it('should accept single action', (done) => { + expect(actions.start()).toDispatchActions(actions.start(), done); + }); + + it('should accept array with one action', (done) => { + expect(actions.start()).toDispatchActions([actions.start()], done); + }); + + it('should accept array with multiple actions', (done) => { + expect(actions.asyncActionCreator()) + .toDispatchActions(actions.expectedActions, done); + }); + + it('should accept array with nested async action creators', (done) => { + expect(actions.parentAsyncActionCreator()) + .toDispatchActions(actions.expectedParentActions, done); + }); + }); + + describe('.toNotDispatchActions', () => { + it('should accept single action', (done) => { + expect(actions.start()).toNotDispatchActions(actions.anotherStart(), done); + }); + + it('should accept array with one action', (done) => { + expect(actions.start()).toNotDispatchActions([actions.anotherStart()], done); + }); + + it('should accept array with multiple actions', (done) => { + expect(actions.asyncActionCreator()) + .toNotDispatchActions(actions.anotherExpectedActions, done); + }); + + it('should accept array with nested async action creators', (done) => { + expect(actions.parentAsyncActionCreator()) + .toNotDispatchActions(actions.anotherParentExpectedActions, done); + }); + }); +}); diff --git a/test/jasmine/jasmine.json b/test/jasmine/jasmine.json new file mode 100644 index 0000000..ea21598 --- /dev/null +++ b/test/jasmine/jasmine.json @@ -0,0 +1,9 @@ +{ + "spec_dir": "test/jasmine", + "spec_files": [ + "*.js" + ], + "helpers": [ + "../../node_modules/babel-register/lib/node.js" + ] +} From 50a1bf0196496ac77725806dc8ca09fbeace9f38 Mon Sep 17 00:00:00 2001 From: giuband Date: Tue, 10 Jan 2017 21:18:56 +0100 Subject: [PATCH 2/5] Add tests with jasmine as a step in npm test --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b0c3f92..9c576be 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "test:jasmine": "jasmine JASMINE_CONFIG_PATH=test/jasmine/jasmine.json", "test:should": "mocha --compilers js:babel-register --reporter spec test/should/*.js", "test:tape": "tape --require babel-register test/tape/*.js", - "test": "npm run test:index && npm run test:chai && npm run test:expect && npm run test:expectjs && npm run test:should && npm run test:tape", + "test": "npm run test:index && npm run test:chai && npm run test:expect && npm run test:expectjs && npm run test:jasmine && npm run test:should && npm run test:tape", "prepublish": "rimraf build && babel src --out-dir build --copy-files" }, "repository": { From 8b908f8adfb54e60e2886751bd67be645e1807c9 Mon Sep 17 00:00:00 2001 From: giuband Date: Wed, 11 Jan 2017 10:06:17 +0100 Subject: [PATCH 3/5] Add support for negativeCompare, use function syntax --- src/jasmine.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/jasmine.js b/src/jasmine.js index 6085655..39cd6fa 100644 --- a/src/jasmine.js +++ b/src/jasmine.js @@ -1,41 +1,49 @@ /* eslint-env jasmine */ import { assertions } from 'redux-actions-assertions-js'; -const toDispatchActions = () => { +function toDispatchActions() { return { compare(action, expectedActions, done) { assertions.toDispatchActions(action, expectedActions, done, done.fail); return { pass: true }; + }, + negativeCompare(action, expectedActions, done) { + assertions.toNotDispatchActions(action, expectedActions, done, done.fail); + return { pass: true }; } }; -}; +} -const toNotDispatchActions = () => { +function toNotDispatchActions() { return { compare(action, expectedActions, done) { assertions.toNotDispatchActions(action, expectedActions, done, done.fail); return { pass: true }; } }; -}; +} -const toDispatchActionsWithState = () => { +function toDispatchActionsWithState() { return { compare(action, state, expectedActions, done) { assertions.toDispatchActionsWithState(state, action, expectedActions, done, done.fail); return { pass: true }; + }, + negativeCompare(action, state, expectedActions, done) { + assertions.toNotDispatchActionsWithState(state, action, expectedActions, done, done.fail); + return { pass: true }; } }; -}; +} -const toNotDispatchActionsWithState = () => { +function toNotDispatchActionsWithState() { return { compare(action, state, expectedActions, done) { assertions.toNotDispatchActionsWithState(state, action, expectedActions, done, done.fail); return { pass: true }; } }; -}; +} const matchers = { toDispatchActions, @@ -44,9 +52,9 @@ const matchers = { toNotDispatchActionsWithState }; -const registerAssertions = () => { +function registerAssertions() { jasmine.addMatchers(matchers); -}; +} export { registerAssertions, From f16cb94dfbadb0d8c0e6a917fbbb6cd5eddd65b4 Mon Sep 17 00:00:00 2001 From: giuband Date: Wed, 11 Jan 2017 10:06:27 +0100 Subject: [PATCH 4/5] Add tests for negativeCompare --- test/jasmine/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/jasmine/index.js b/test/jasmine/index.js index 13e0435..75999ba 100644 --- a/test/jasmine/index.js +++ b/test/jasmine/index.js @@ -55,4 +55,24 @@ describe('jasmine', () => { .toNotDispatchActions(actions.anotherParentExpectedActions, done); }); }); + + describe('.not.toDispatchActions', () => { + it('should accept single action', (done) => { + expect(actions.start()).not.toDispatchActions(actions.anotherStart(), done); + }); + + it('should accept array with one action', (done) => { + expect(actions.start()).not.toDispatchActions([actions.anotherStart()], done); + }); + + it('should accept array with multiple actions', (done) => { + expect(actions.asyncActionCreator()) + .not.toDispatchActions(actions.anotherExpectedActions, done); + }); + + it('should accept array with nested async action creators', (done) => { + expect(actions.parentAsyncActionCreator()) + .not.toDispatchActions(actions.anotherParentExpectedActions, done); + }); + }); }); From f63c61e53db44e3c87f60518bc2b5ea73c7f73c5 Mon Sep 17 00:00:00 2001 From: giuband Date: Wed, 11 Jan 2017 21:31:55 +0100 Subject: [PATCH 5/5] Update readme, add documentation for usage with jasmine --- README.md | 1 + documentation/jasmine.md | 57 ++++++++++++++++++++++++++++++++++++++++ test/jasmine/index.js | 3 ++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 documentation/jasmine.md diff --git a/README.md b/README.md index cb5d9bb..d75b5ce 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ It use [redux-mock-store](https://github.com/arnaudbenard/redux-mock-store) to m - [chai](https://redux-things.github.io/redux-actions-assertions/chai.html) - [expect](https://redux-things.github.io/redux-actions-assertions/expect.html) - [expect.js](https://redux-things.github.io/redux-actions-assertions/expectjs.html) +- [jasmine](https://redux-things.github.io/redux-actions-assertions/jasmine.html) - [should](https://redux-things.github.io/redux-actions-assertions/should.html) - [tape](https://redux-things.github.io/redux-actions-assertions/tape.html) - [pure javascript assertion](https://redux-things.github.io/redux-actions-assertions/javascript.html) diff --git a/documentation/jasmine.md b/documentation/jasmine.md new file mode 100644 index 0000000..ddc15fb --- /dev/null +++ b/documentation/jasmine.md @@ -0,0 +1,57 @@ +# [jasmine](https://github.com/jasmine/jasmine) + +## Registration + +```js +// using ES6 modules +import { registerAssertions } from 'redux-actions-assertions/jasmine'; + +// using CommonJS modules +var registerAssertions = require('redux-actions-assertions/jasmine').registerAssertions; + +// registration +beforeEach(registerAssertions); +``` + +## Usage + +### .toDispatchActions + +> `expect(action).toDispatchActions(expectedActions, done)` + +Asserts that when given `action` is dispatched it will dispatch `expectedActions`. `action` can be plain object (action) or function (action creator). `expectedActions` can be can be plain object (action) or function (action creator) or array of objects/functions. + +```js +expect(myActionCreator()) + .toDispatchActions({ type: 'MY_ACTION_START' }, done); +``` + +### .not.toDispatchActions + +> `expect(action).not.toDispatchActions(expectedActions, done)` + +Asserts that when given `action` is dispatched it will not dispatch `expectedActions`. `action` can be plain object (action) or function (action creator). `expectedActions` can be can be plain object (action) or function (action creator) or array of objects/functions. + +```js +expect(myActionCreator()) + .not.toDispatchActions({ type: 'MY_ACTION_START' }, done); +``` + +### .toDispatchActionsWithState + +> `expect(action).toDispatchActionsWithState(state, expectedActions, done)` + +Asserts that store initialised with `state` before `action` is dispatched. + +```js +const state = {property: 'value'}; +const expectedActions = [{ type: 'MY_ACTION_START' }, finishActionCreator()]; +expect(myActionCreator()) + .toDispatchActionsWithState(state, expectedActions, done); +``` +You can also use it with `.not`: + +```js +expect(myActionCreator()) + .not.toDispatchActionsWithState(state, expectedActions, done); +``` diff --git a/test/jasmine/index.js b/test/jasmine/index.js index 75999ba..a8d4582 100644 --- a/test/jasmine/index.js +++ b/test/jasmine/index.js @@ -6,8 +6,9 @@ import actions from '../testingData/actions'; registerMiddlewares([thunk]); +beforeEach(registerAssertions); + describe('jasmine', () => { - beforeEach(() => { registerAssertions(); }); describe('toDispatchActionsWithState', () => { it('should accept object', (done) => { const state = { property: 'value' };