Skip to content

Commit

Permalink
Add test confirming users can sign out (#1851)
Browse files Browse the repository at this point in the history
  • Loading branch information
Onokaev authored Jun 23, 2022
1 parent d2ee831 commit 078e0e8
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/app/services/actions/auth-action-creators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ import {
import configureMockStore from 'redux-mock-store';

import thunk from 'redux-thunk';
import { HOME_ACCOUNT_KEY } from '../graph-constants';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
window.open = jest.fn();
jest.spyOn(window.sessionStorage.__proto__, 'clear');

jest.spyOn(window.localStorage.__proto__, 'setItem');
jest.spyOn(window.localStorage.__proto__, 'getItem');
jest.spyOn(window.localStorage.__proto__, 'removeItem');

window.localStorage.setItem(HOME_ACCOUNT_KEY, '1234567');

describe('Auth Action Creators', () => {
it('should dispatch AUTHENTICATION_PENDING when setAuthenticationPending() is called', () => {
// Arrange
Expand Down Expand Up @@ -61,7 +70,7 @@ describe('Auth Action Creators', () => {
expect(action).toEqual(expectedAction);
});

it('tests sign out success action', () => {
it('should dispatch LOGOUT_SUCCESS when signOutSuccess() is called', () => {
// Arrange
const response: boolean = true;
const expectedAction = {
Expand Down Expand Up @@ -106,7 +115,7 @@ describe('Auth Action Creators', () => {
const store = mockStore({ authToken: {} });

// @ts-ignore
store.dispatch(signIn(response));
store.dispatch(signIn());

// Assert
expect(store.getActions()).toEqual([expectedAction]);
Expand All @@ -131,21 +140,34 @@ describe('Auth Action Creators', () => {

});

it('should dispatch AUTHENTICATION_PENDING when signOut() is called', () => {
it('should confirm that a user can sign out', () => {
// Arrange
window.open = jest.fn();
const store = mockStore({});
const expectedActions = [
{
type: AUTHENTICATION_PENDING,
response: true
},
{
type: LOGOUT_SUCCESS,
response: true
}
];

// Act and assert
// home account key is available before signing out
expect(window.localStorage.getItem(HOME_ACCOUNT_KEY)).toBe('1234567');
//@ts-ignore
store.dispatch(signOut())
const actions = store.getActions();

// logoutPop launches a popup window.
expect(window.open).toHaveBeenCalled();
expect(window.localStorage.removeItem).toHaveBeenCalledWith(HOME_ACCOUNT_KEY);

expect(store.getActions()[0].type).toEqual(expectedActions[0].type);
// after signing out, the home account key is removed
expect(window.localStorage.getItem(HOME_ACCOUNT_KEY)).toBe(null);
expect(actions).toEqual(expectedActions);

});
})

0 comments on commit 078e0e8

Please sign in to comment.