From e5c1d08191aa59c12ed45e39bdf4fbf9420b3a7b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 20 May 2016 10:29:12 -0400 Subject: [PATCH] Fix repeated test runs If you ran the tests several times, some things would be saved in localStorage, causing tests to possibly fail. --- src/linodes/reducers/index.js | 14 +++++----- src/reducers/authentication.js | 38 ++++++++++++++-------------- test/reducers/authentication.spec.js | 3 +++ test/setup.js | 2 ++ 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/linodes/reducers/index.js b/src/linodes/reducers/index.js index 9c983a74c78..817ec5949a6 100644 --- a/src/linodes/reducers/index.js +++ b/src/linodes/reducers/index.js @@ -8,16 +8,14 @@ import { TOGGLE_SELECTED } from '../actions/index'; -const view = getStorage("linodes/view") || "grid"; - -const default_state = { - view, - selected: { } // set -}; - const array_to_set = arr => arr.reduce((s, v) => ({ ...s, [v]: true }), { }); -export function index(state=default_state, action) { +export function index(state=null, action) { + if (state === null) { + const view = getStorage("linodes/view") || "grid"; + state = { view, selected: { } }; + } + switch (action.type) { case CHANGE_VIEW: const { view } = action; diff --git a/src/reducers/authentication.js b/src/reducers/authentication.js index 0d2f88ac606..1696a00fe72 100644 --- a/src/reducers/authentication.js +++ b/src/reducers/authentication.js @@ -1,23 +1,23 @@ import { CALLBACK, SET_TOKEN } from '../actions/authentication'; import { getStorage, setStorage } from '~/storage'; -const token = getStorage("authentication/oauth-token"); - -const default_state = { token }; - -export default function authentication(state = default_state, action) { - switch (action.type) { - case SET_TOKEN: - let newState = { - ...state, - scopes: action.scopes, - username: action.username, - email: action.email, - token: action.token - }; - setStorage("authentication/oauth-token", action.token); - return newState; - default: - return state; - } +export default function authentication(state = null, action) { + if (state === null) { + const token = getStorage("authentication/oauth-token"); + state = { token }; + } + switch (action.type) { + case SET_TOKEN: + let newState = { + ...state, + scopes: action.scopes, + username: action.username, + email: action.email, + token: action.token + }; + setStorage("authentication/oauth-token", action.token); + return newState; + default: + return state; + } } diff --git a/test/reducers/authentication.spec.js b/test/reducers/authentication.spec.js index 60299eea5f5..c70a8474a8c 100644 --- a/test/reducers/authentication.spec.js +++ b/test/reducers/authentication.spec.js @@ -5,12 +5,14 @@ import * as actions from '../../src/actions/authentication'; describe("authentication reducer", () => { it('should handle initial state', () => { + window.localStorage.clear(); expect( authentication(undefined, {}) ).to.be.eql({ token: null }); }); it('should handle SET_TOKEN', () => { + window.localStorage.clear(); const state = authentication(undefined, {}); const auth = { token: null }; @@ -33,6 +35,7 @@ describe("authentication reducer", () => { }); it('should handle anything else', () => { + window.localStorage.clear(); const state = authentication(undefined, {}); const auth = { token: null }; diff --git a/test/setup.js b/test/setup.js index c991a63beb4..35baab2e5c4 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1 +1,3 @@ +window.localStorage.clear(); + import '../src/index';