Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 57 additions & 0 deletions documentation/jasmine.md
Original file line number Diff line number Diff line change
@@ -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);
```
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
62 changes: 62 additions & 0 deletions src/jasmine.js
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to expose matchers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to import them from Jest and making the registration step more explicit on the jest's setupTestFrameworkScriptFile file (instead of simply calling jasmine's registerAssertions() there)

};
79 changes: 79 additions & 0 deletions test/jasmine/index.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
9 changes: 9 additions & 0 deletions test/jasmine/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"spec_dir": "test/jasmine",
"spec_files": [
"*.js"
],
"helpers": [
"../../node_modules/babel-register/lib/node.js"
]
}