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/package.json b/package.json index 2c39699..9c576be 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,10 @@ "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", + "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": { @@ -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..39cd6fa --- /dev/null +++ b/src/jasmine.js @@ -0,0 +1,62 @@ +/* eslint-env jasmine */ +import { assertions } from 'redux-actions-assertions-js'; + +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 }; + } + }; +} + +function toNotDispatchActions() { + return { + compare(action, expectedActions, done) { + assertions.toNotDispatchActions(action, expectedActions, done, done.fail); + return { pass: true }; + } + }; +} + +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 }; + } + }; +} + +function toNotDispatchActionsWithState() { + return { + compare(action, state, expectedActions, done) { + assertions.toNotDispatchActionsWithState(state, action, expectedActions, done, done.fail); + return { pass: true }; + } + }; +} + +const matchers = { + toDispatchActions, + toNotDispatchActions, + toDispatchActionsWithState, + toNotDispatchActionsWithState +}; + +function 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..a8d4582 --- /dev/null +++ b/test/jasmine/index.js @@ -0,0 +1,79 @@ +/* eslint-env jasmine */ +import thunk from 'redux-thunk'; +import { registerMiddlewares } from '../../src'; +import { registerAssertions } from '../../src/jasmine'; +import actions from '../testingData/actions'; + +registerMiddlewares([thunk]); + +beforeEach(registerAssertions); + +describe('jasmine', () => { + 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); + }); + }); + + 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); + }); + }); +}); 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" + ] +}