Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repeated test runs #8

Merged
merged 1 commit into from
May 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/linodes/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not no default argument for state and just if (!state) {? Also, this appears to be identical to what it was before except basically ignoring default arguments. Could you explain how this solved the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=== > truthy compares imo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this postpones talking to localStorage until the reducer is invoked.

const view = getStorage("linodes/view") || "grid";
state = { view, selected: { } };
}

switch (action.type) {
case CHANGE_VIEW:
const { view } = action;
Expand Down
38 changes: 19 additions & 19 deletions src/reducers/authentication.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions test/reducers/authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -33,6 +35,7 @@ describe("authentication reducer", () => {
});

it('should handle anything else', () => {
window.localStorage.clear();
const state = authentication(undefined, {});
const auth = { token: null };

Expand Down
2 changes: 2 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
window.localStorage.clear();

import '../src/index';