Replace chai with unexpected.js

This commit is contained in:
SleepWalker
2016-07-30 13:44:43 +03:00
parent ae11dbdc97
commit 9abbe2ebab
12 changed files with 221 additions and 144 deletions

View File

@@ -1,6 +1,9 @@
import expect from 'unexpected';
import request from 'services/request';
import {
setLoadingState,
oAuthValidate,
oAuthComplete,
setClient,
@@ -19,21 +22,29 @@ const oauthData = {
};
describe('components/auth/actions', () => {
const dispatch = sinon.stub();
const getState = sinon.stub();
const dispatch = sinon.stub().named('dispatch');
const getState = sinon.stub().named('getState');
const callThunk = function(fn, ...args) {
function callThunk(fn, ...args) {
const thunk = fn(...args);
return thunk(dispatch, getState);
};
}
function expectDispatchCalls(calls) {
expect(dispatch, 'to have calls satisfying', [
[setLoadingState(true)]
].concat(calls).concat([
[setLoadingState(false)]
]));
}
beforeEach(() => {
dispatch.reset();
getState.reset();
getState.returns({});
sinon.stub(request, 'get');
sinon.stub(request, 'post');
sinon.stub(request, 'get').named('request.get');
sinon.stub(request, 'post').named('request.post');
});
afterEach(() => {
@@ -42,10 +53,10 @@ describe('components/auth/actions', () => {
});
describe('#oAuthValidate()', () => {
it('should dispatch setClient, setOAuthRequest and setScopes', () => {
// TODO: the assertions may be splitted up to one per test
let resp;
const resp = {
beforeEach(() => {
resp = {
client: {id: 123},
oAuth: {state: 123},
session: {
@@ -54,12 +65,21 @@ describe('components/auth/actions', () => {
};
request.get.returns(Promise.resolve(resp));
});
it('should send get request to an api', () => {
return callThunk(oAuthValidate, oauthData).then(() => {
sinon.assert.calledWith(request.get, '/api/oauth2/v1/validate');
sinon.assert.calledWith(dispatch, setClient(resp.client));
sinon.assert.calledWith(dispatch, setOAuthRequest(resp.oAuth));
sinon.assert.calledWith(dispatch, setScopes(resp.session.scopes));
expect(request.get, 'to have a call satisfying', ['/api/oauth2/v1/validate', {}]);
});
});
it('should dispatch setClient, setOAuthRequest and setScopes', () => {
return callThunk(oAuthValidate, oauthData).then(() => {
expectDispatchCalls([
[setClient(resp.client)],
[setOAuthRequest(resp.oAuth)],
[setScopes(resp.session.scopes)]
]);
});
});
});
@@ -73,8 +93,20 @@ describe('components/auth/actions', () => {
});
});
it('should post to api/oauth2/complete', () => {
request.post.returns(Promise.resolve({
redirectUri: ''
}));
return callThunk(oAuthComplete).then(() => {
expect(request.post, 'to have a call satisfying', [
'/api/oauth2/v1/complete?client_id=&redirect_uri=&response_type=&scope=&state=',
{}
]);
});
});
it('should dispatch setOAuthCode for static_page redirect', () => {
// TODO: it may be split on separate url and dispatch tests
const resp = {
success: true,
redirectUri: 'static_page?code=123&state='
@@ -83,12 +115,15 @@ describe('components/auth/actions', () => {
request.post.returns(Promise.resolve(resp));
return callThunk(oAuthComplete).then(() => {
sinon.assert.calledWithMatch(request.post, /\/api\/oauth2\/v1\/complete/);
sinon.assert.calledWith(dispatch, setOAuthCode({
success: true,
code: '123',
displayCode: false
}));
expectDispatchCalls([
[
setOAuthCode({
success: true,
code: '123',
displayCode: false
})
]
]);
});
});
@@ -102,7 +137,7 @@ describe('components/auth/actions', () => {
request.post.returns(Promise.reject(resp));
return callThunk(oAuthComplete).then((resp) => {
expect(resp).to.be.deep.equal({
expect(resp, 'to equal', {
success: false,
redirectUri: 'redirectUri'
});
@@ -118,8 +153,10 @@ describe('components/auth/actions', () => {
request.post.returns(Promise.reject(resp));
return callThunk(oAuthComplete).catch((resp) => {
expect(resp.acceptRequired).to.be.true;
sinon.assert.calledWith(dispatch, requirePermissionsAccept());
expect(resp.acceptRequired, 'to be true');
expectDispatchCalls([
[requirePermissionsAccept()]
]);
});
});
});

View File

@@ -1,3 +1,4 @@
import expect from 'unexpected';
import React from 'react';
import { shallow } from 'enzyme';
@@ -8,16 +9,16 @@ describe('<ChangePassword />', () => {
it('renders two <Input /> components', () => {
const component = shallow(<ChangePassword onSubmit={() => {}} />);
expect(component.find('Input')).to.have.length(2);
expect(component.find('Input'), 'to satisfy', {length: 2});
});
it('should call onSubmit if passwords entered', () => {
const onSubmit = sinon.spy();
const onSubmit = sinon.spy().named('onSubmit');
const component = shallow(<ChangePassword onSubmit={onSubmit} />);
component.find('Form').simulate('submit');
sinon.assert.calledOnce(onSubmit);
expect(onSubmit, 'was called');
});
});

View File

@@ -1,3 +1,5 @@
import expect from 'unexpected';
import React from 'react';
import { shallow, mount } from 'enzyme';
@@ -22,7 +24,7 @@ describe('<PopupStack />', () => {
};
const component = shallow(<PopupStack {...props} />);
expect(component.find(DummyPopup)).to.have.length(2);
expect(component.find(DummyPopup), 'to satisfy', {length: 2});
});
it('should pass onClose as props', () => {
@@ -35,7 +37,7 @@ describe('<PopupStack />', () => {
popups: [
{
Popup: (props = {}) => {
expect(props.onClose).to.be.a('function');
expect(props.onClose, 'to be a', 'function');
return <DummyPopup {...expectedProps} />;
}
@@ -45,8 +47,8 @@ describe('<PopupStack />', () => {
const component = mount(<PopupStack {...props} />);
const popup = component.find(DummyPopup);
expect(popup).to.have.length(1);
expect(popup.props()).to.deep.equal(expectedProps);
expect(popup, 'to satisfy', {length: 1});
expect(popup.props(), 'to equal', expectedProps);
});
it('should hide popup, when onClose called', () => {
@@ -59,20 +61,22 @@ describe('<PopupStack />', () => {
Popup: DummyPopup
}
],
destroy: sinon.stub()
destroy: sinon.stub().named('props.destroy')
};
const component = shallow(<PopupStack {...props} />);
component.find(DummyPopup).last().prop('onClose')();
sinon.assert.calledOnce(props.destroy);
sinon.assert.calledWith(props.destroy, sinon.match.same(props.popups[1]));
expect(props.destroy, 'was called once');
expect(props.destroy, 'to have a call satisfying', [
expect.it('to be', props.popups[1])
]);
});
it('should hide popup, when overlay clicked', () => {
const preventDefault = sinon.stub();
const preventDefault = sinon.stub().named('event.preventDefault');
const props = {
destroy: sinon.stub(),
destroy: sinon.stub().named('props.destroy'),
popups: [
{
Popup: DummyPopup
@@ -84,13 +88,13 @@ describe('<PopupStack />', () => {
const overlay = component.find(`.${styles.overlay}`);
overlay.simulate('click', {target: 1, currentTarget: 1, preventDefault});
sinon.assert.calledOnce(props.destroy);
sinon.assert.calledOnce(preventDefault);
expect(props.destroy, 'was called once');
expect(preventDefault, 'was called once');
});
it('should hide popup on overlay click if disableOverlayClose', () => {
const props = {
destroy: sinon.stub(),
destroy: sinon.stub().named('props.destroy'),
popups: [
{
Popup: DummyPopup,
@@ -103,12 +107,12 @@ describe('<PopupStack />', () => {
const overlay = component.find(`.${styles.overlay}`);
overlay.simulate('click', {target: 1, currentTarget: 1, preventDefault() {}});
sinon.assert.notCalled(props.destroy);
expect(props.destroy, 'was not called');
});
it('should hide popup, when esc pressed', () => {
const props = {
destroy: sinon.stub(),
destroy: sinon.stub().named('props.destroy'),
popups: [
{
Popup: DummyPopup
@@ -121,12 +125,12 @@ describe('<PopupStack />', () => {
event.which = 27;
document.dispatchEvent(event);
sinon.assert.calledOnce(props.destroy);
expect(props.destroy, 'was called once');
});
it('should hide first popup in stack if esc pressed', () => {
const props = {
destroy: sinon.stub(),
destroy: sinon.stub().named('props.destroy'),
popups: [
{
Popup() {return null;}
@@ -142,13 +146,15 @@ describe('<PopupStack />', () => {
event.which = 27;
document.dispatchEvent(event);
sinon.assert.calledOnce(props.destroy);
sinon.assert.calledWithExactly(props.destroy, props.popups[1]);
expect(props.destroy, 'was called once');
expect(props.destroy, 'to have a call satisfying', [
expect.it('to be', props.popups[1])
]);
});
it('should NOT hide popup on esc pressed if disableOverlayClose', () => {
const props = {
destroy: sinon.stub(),
destroy: sinon.stub().named('props.destroy'),
popups: [
{
Popup: DummyPopup,
@@ -162,6 +168,6 @@ describe('<PopupStack />', () => {
event.which = 27;
document.dispatchEvent(event);
sinon.assert.notCalled(props.destroy);
expect(props.destroy, 'was not called');
});
});

View File

@@ -1,3 +1,5 @@
import expect from 'unexpected';
import reducer from 'components/ui/popup/reducer';
import {create, destroy} from 'components/ui/popup/actions';
@@ -5,8 +7,8 @@ describe('popup/reducer', () => {
it('should have no popups by default', () => {
const actual = reducer(undefined, {});
expect(actual.popups).to.be.an('array');
expect(actual.popups).to.be.empty;
expect(actual.popups, 'to be an', 'array');
expect(actual.popups, 'to be empty');
});
describe('#create', () => {
@@ -15,7 +17,7 @@ describe('popup/reducer', () => {
Popup: FakeComponent
}));
expect(actual.popups[0]).to.be.deep.equal({
expect(actual.popups[0], 'to equal', {
Popup: FakeComponent
});
});
@@ -23,7 +25,7 @@ describe('popup/reducer', () => {
it('should support shortcut popup creation', () => {
const actual = reducer(undefined, create(FakeComponent));
expect(actual.popups[0]).to.be.deep.equal({
expect(actual.popups[0], 'to equal', {
Popup: FakeComponent
});
});
@@ -36,13 +38,13 @@ describe('popup/reducer', () => {
Popup: FakeComponent
}));
expect(actual.popups[1]).to.be.deep.equal({
expect(actual.popups[1], 'to equal', {
Popup: FakeComponent
});
});
it('throws when no type provided', () => {
expect(() => reducer(undefined, create())).to.throw('Popup is required');
expect(() => reducer(undefined, create()), 'to throw', 'Popup is required');
});
});
@@ -56,11 +58,11 @@ describe('popup/reducer', () => {
});
it('should remove popup', () => {
expect(state.popups).to.have.length(1);
expect(state.popups, 'to have length', 1);
state = reducer(state, destroy(popup));
expect(state.popups).to.have.length(0);
expect(state.popups, 'to have length', 0);
});
it('should not remove something, that it should not', () => {
@@ -70,8 +72,8 @@ describe('popup/reducer', () => {
state = reducer(state, destroy(popup));
expect(state.popups).to.have.length(1);
expect(state.popups[0]).to.not.equal(popup);
expect(state.popups, 'to have length', 1);
expect(state.popups[0], 'not to be', popup);
});
});
});

View File

@@ -1,3 +1,5 @@
import expect from 'unexpected';
import { routeActions } from 'react-router-redux';
import request from 'services/request';
@@ -9,8 +11,8 @@ import {
describe('components/user/actions', () => {
const dispatch = sinon.stub();
const getState = sinon.stub();
const dispatch = sinon.stub().named('dispatch');
const getState = sinon.stub().named('getState');
const callThunk = function(fn, ...args) {
const thunk = fn(...args);
@@ -22,8 +24,8 @@ describe('components/user/actions', () => {
dispatch.reset();
getState.reset();
getState.returns({});
sinon.stub(request, 'get');
sinon.stub(request, 'post');
sinon.stub(request, 'get').named('request.get');
sinon.stub(request, 'post').named('request.post');
});
afterEach(() => {
@@ -42,14 +44,16 @@ describe('components/user/actions', () => {
request.post.returns(new Promise((resolve) => {
setTimeout(() => {
// we must not overwrite user's token till request starts
sinon.assert.notCalled(dispatch);
expect(dispatch, 'was not called');
resolve();
}, 0);
}));
return callThunk(logout).then(() => {
sinon.assert.calledWith(request.post, '/api/authentication/logout');
expect(request.post, 'to have a call satisfying', [
'/api/authentication/logout'
]);
});
});
@@ -63,10 +67,12 @@ describe('components/user/actions', () => {
request.post.returns(Promise.resolve());
return callThunk(logout).then(() => {
sinon.assert.calledWith(dispatch, setUser({
lang: 'foo',
isGuest: true
}));
expect(dispatch, 'to have a call satisfying', [
setUser({
lang: 'foo',
isGuest: true
})
]);
});
});
@@ -80,7 +86,9 @@ describe('components/user/actions', () => {
request.post.returns(Promise.resolve());
return callThunk(logout).then(() => {
sinon.assert.calledWith(dispatch, routeActions.push('/login'));
expect(dispatch, 'to have a call satisfying', [
routeActions.push('/login')
]);
});
});
});