{"size": 2515, "ext": "js", "max_stars_repo_path": "tests/helpers/loadUser.test.js", "max_stars_repo_name": "JohnniesCode/redux-oidc", "max_stars_repo_stars_event_min_datetime": "2016-04-09T13:52:52.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-22T06:11:07.000Z", "max_issues_repo_path": "tests/helpers/loadUser.test.js", "max_issues_repo_name": "JohnniesCode/redux-oidc", "max_issues_repo_issues_event_min_datetime": "2016-04-25T06:47:52.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T11:49:52.000Z", "max_forks_repo_path": "tests/helpers/loadUser.test.js", "max_forks_repo_name": "JohnniesCode/redux-oidc", "max_forks_repo_forks_event_min_datetime": "2016-07-20T11:45:37.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-08T15:20:02.000Z"}
{"max_stars_count": 420, "max_issues_count": 180, "max_forks_count": 137, "avg_line_length": 24.900990099, "max_line_length": 123, "alphanum_fraction": 0.6596421471}
import '../setup';
import sinon from 'sinon';
import expect from 'expect';
import loadUserHandler, { getUserCallback, errorCallback, setReduxStore, getReduxStore } from '../../src/helpers/loadUser';
import { userExpired, userFound, loadUserError, loadingUser } from '../../src/actions';

describe('helper - loadUser()', () => {
  let userManagerMock;
  let storeMock;
  let getUserStub;
  let dispatchStub;
  let thenStub;
  let catchStub;

  beforeEach(() => {
    dispatchStub = sinon.stub();
    getUserStub = sinon.stub();
    thenStub = sinon.stub();
    catchStub = sinon.stub();

    userManagerMock = {
      getUser: getUserStub
    };

    storeMock = {
      dispatch: dispatchStub
    };

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

    thenStub.returns({
      catch: catchStub
    });

    setReduxStore(storeMock);
  });

  it('should dispatch a valid user to the store', () => {
    const validUser = { some: 'user' };

    getUserCallback(validUser);

    expect(dispatchStub.calledWith(userFound(validUser))).toEqual(true);
  });

  it('should dispatch USER_EXPIRED when no valid user is present', () => {
    const invalidUser = { expired: true };

    getUserCallback(invalidUser);

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

  it('should set the redux store', () => {
    loadUserHandler(storeMock, userManagerMock);

    expect(getReduxStore() === storeMock).toEqual(true);
  });

  it('errorCallback should dispatch LOAD_USER_ERROR', () => {
    setReduxStore(storeMock);

    errorCallback({ message: 'Some message!'});

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

  it('loadUserCallback returns the user', () => {
    const user = {
      some: 'user'
    };

    const result1 = getUserCallback(user);
    expect(result1).toEqual(user);

    const expiredUser = {
      expired: true
    };

    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);
  });
});
