Skip to content

Commit

Permalink
post publish v3.0.0-beta.13
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmantz committed Nov 21, 2017
1 parent b3908c0 commit 3261460
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 44 deletions.
2 changes: 1 addition & 1 deletion dist/redux-oidc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-oidc",
"version": "3.0.0-beta.12",
"version": "3.0.0-beta.13",
"description": "A package for managing OpenID Connect authentication in redux apps",
"main": "dist/redux-oidc.js",
"types": "./index.d.ts",
Expand Down
7 changes: 4 additions & 3 deletions src/helpers/loadUser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { userFound, userExpired, loadUserError } from '../actions';
import { userFound, userExpired, loadUserError, loadingUser } from '../actions';

// stores the redux store here to be accessed by all functions
let reduxStore;
Expand All @@ -17,7 +17,7 @@ export function getReduxStore() {
export function getUserCallback(user) {
if (user && !user.expired) {
reduxStore.dispatch(userFound(user));
} else if (user && user.expired) {
} else if (!user || (user && user.expired)) {
reduxStore.dispatch(userExpired());
}
return user;
Expand All @@ -41,8 +41,9 @@ export default function loadUser(store, userManager) {
}

reduxStore = store;
store.dispatch(loadingUser());

userManager.getUser()
return userManager.getUser()
.then(getUserCallback)
.catch(errorCallback);
}
77 changes: 42 additions & 35 deletions src/reducer/reducer-immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,50 @@ import {
let reducer;

try {
const fromJS = require('immutable').fromJS;
const { fromJS, Seq } = require('immutable');

const initialState = fromJS({
user: null,
isLoadingUser: false
});

reducer = (state = initialState, action) => {
switch (action.type) {
case USER_EXPIRED:
return fromJS({
user: null,
isLoadingUser: false
});
case SILENT_RENEW_ERROR:
return fromJS({
user: null,
isLoadingUser: false
});
case SESSION_TERMINATED:
case USER_SIGNED_OUT:
return fromJS({
user: null,
isLoadingUser: false
});
case REDIRECT_SUCCESS:
case USER_FOUND:
return fromJS({
user: action.payload,
isLoadingUser: false
});
case LOADING_USER:
return state.set('isLoadingUser', true);
default:
return state;
const fromJSGreedy = (js) => {
return typeof js !== 'object' || js === null ? js :
Array.isArray(js) ?
Seq(js).map(fromJSGreedy).toList() :
Seq(js).map(fromJSGreedy).toMap();
}
};

const initialState = fromJS({
user: null,
isLoadingUser: false
});

reducer = (state = initialState, action) => {
switch (action.type) {
case USER_EXPIRED:
return fromJS({
user: null,
isLoadingUser: false
});
case SILENT_RENEW_ERROR:
return fromJS({
user: null,
isLoadingUser: false
});
case SESSION_TERMINATED:
case USER_SIGNED_OUT:
return fromJS({
user: null,
isLoadingUser: false
});
case REDIRECT_SUCCESS:
case USER_FOUND:
return fromJSGreedy({
user: action.payload,
isLoadingUser: false
});
case LOADING_USER:
return state.set('isLoadingUser', true);
default:
return state;
}
};
} catch (error) {
reducer = () => {
console.error("You must install immutable-js for the immutable reducer to work!");
Expand Down
23 changes: 19 additions & 4 deletions tests/helpers/loadUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '../setup';
import sinon from 'sinon';
import expect from 'expect';
import loadUserHandler, { getUserCallback, errorCallback, setReduxStore, getReduxStore } from '../../src/helpers/loadUser';
import { userExpired, userFound, loadUserError } from '../../src/actions';
import { userExpired, userFound, loadUserError, loadingUser } from '../../src/actions';

describe('helper - loadUser()', () => {
let userManagerMock;
Expand All @@ -26,9 +26,7 @@ describe('helper - loadUser()', () => {
dispatch: dispatchStub
};

getUserStub.returns({
then: thenStub
});
getUserStub.returns(new Promise(() => {}));

thenStub.returns({
catch: catchStub
Expand Down Expand Up @@ -82,4 +80,21 @@ describe('helper - loadUser()', () => {
const result2 = getUserCallback(expiredUser);
expect(result2).toEqual(expiredUser);
});

it ('loadUserCallback should dispatch USER_EXPIRED when the user returned is null', () => {
getUserCallback(null);
expect(dispatchStub.calledWith(userExpired())).toEqual(true);
});

it('loadUser should dispatch LOADING_USER', () => {
loadUserHandler(storeMock, userManagerMock);

expect(dispatchStub.calledWith(loadingUser())).toEqual(true);
});

it('loadUser should return a promise', () => {
const promise = loadUserHandler(storeMock, userManagerMock);

expect(promise).toBeA(Promise);
});
});
14 changes: 14 additions & 0 deletions tests/reducer/reducer-immutable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,18 @@ describe('immutable reducer', () => {

expect(reducer(expectedResult, { type: 'UNKNOWN' })).toEqual(expectedResult);
});

it('should handle USER_FOUND correctly when payload has non-object prototype', () => {
function NonObject() {
this.type = 'not-an-object'
}
const user = { some: 'user' };
const nonObjectUser = Object.setPrototypeOf({ some: 'user' }, new NonObject());
const expectedResult = fromJS({
user,
isLoadingUser: false
});

expect(reducer(fromJS(initialState), userFound(nonObjectUser))).toEqual(expectedResult);
});
});

0 comments on commit 3261460

Please sign in to comment.