accounts-frontend/tests/components/user/actions.test.js

96 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-07-30 16:14:43 +05:30
import expect from 'unexpected';
import { routeActions } from 'react-router-redux';
import request from 'services/request';
import {
logout,
setUser
} from 'components/user/actions';
describe('components/user/actions', () => {
2016-07-30 16:14:43 +05:30
const dispatch = sinon.stub().named('dispatch');
const getState = sinon.stub().named('getState');
const callThunk = function(fn, ...args) {
const thunk = fn(...args);
return thunk(dispatch, getState);
};
beforeEach(() => {
dispatch.reset();
getState.reset();
getState.returns({});
2016-07-30 16:14:43 +05:30
sinon.stub(request, 'get').named('request.get');
sinon.stub(request, 'post').named('request.post');
});
afterEach(() => {
request.get.restore();
request.post.restore();
});
describe('#logout()', () => {
it('should post to /api/authentication/logout with user jwt', () => {
getState.returns({
user: {
lang: 'foo'
}
});
request.post.returns(new Promise((resolve) => {
setTimeout(() => {
// we must not overwrite user's token till request starts
2016-07-30 16:14:43 +05:30
expect(dispatch, 'was not called');
resolve();
}, 0);
}));
return callThunk(logout).then(() => {
2016-07-30 16:14:43 +05:30
expect(request.post, 'to have a call satisfying', [
'/api/authentication/logout'
]);
});
});
it('should change user to guest', () => {
getState.returns({
user: {
lang: 'foo'
}
});
request.post.returns(Promise.resolve());
return callThunk(logout).then(() => {
2016-07-30 16:14:43 +05:30
expect(dispatch, 'to have a call satisfying', [
setUser({
lang: 'foo',
isGuest: true
})
]);
});
});
it('should redirect to /login', () => {
getState.returns({
user: {
lang: 'foo'
}
});
request.post.returns(Promise.resolve());
return callThunk(logout).then(() => {
2016-07-30 16:14:43 +05:30
expect(dispatch, 'to have a call satisfying', [
routeActions.push('/login')
]);
});
});
});
});