-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracting user from localStorage (#852)
closes #743 The idea is to intercept editor actions (i.e. actions that could call the API) and update the user object in state prior to that call being made. That way if the user object changes for whatever reason (like cred renewal) the latest user object will be used for the API call
- Loading branch information
Showing
7 changed files
with
144 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const localStorageUserMiddleware = | ||
(setUser) => (store) => (next) => (action) => { | ||
if (action.type.startsWith("editor")) { | ||
const authKey = localStorage.getItem("authKey"); | ||
if (authKey) { | ||
const localStorageUser = JSON.parse(localStorage.getItem(authKey)); | ||
if ( | ||
JSON.stringify(store.getState().auth.user) !== | ||
JSON.stringify(localStorageUser) | ||
) { | ||
store.dispatch(setUser(localStorageUser)); | ||
} | ||
} | ||
} | ||
next(action); | ||
}; | ||
|
||
export default localStorageUserMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import configureStore from "redux-mock-store"; | ||
|
||
import localStorageUserMiddleware from "./localStorageUserMiddleware"; | ||
|
||
describe(`localStorageUserMiddleware`, () => { | ||
let store; | ||
let next = jest.fn(); | ||
let setUser = jest.fn(); | ||
let user = { user: "some-user" }; | ||
const middleware = [localStorageUserMiddleware(setUser)]; | ||
const mockStore = configureStore({ | ||
reducer: setUser, | ||
middleware, | ||
}); | ||
|
||
describe("as a cold user", () => { | ||
beforeEach(() => { | ||
const initialState = { | ||
editor: {}, | ||
auth: {}, | ||
}; | ||
store = mockStore(initialState); | ||
store.dispatch = jest.fn(); | ||
}); | ||
|
||
describe(`when user is set in local storage`, () => { | ||
beforeEach(() => { | ||
localStorage.setItem("authKey", "some-key"); | ||
localStorage.setItem("some-key", JSON.stringify(user)); | ||
}); | ||
|
||
it(`expects setUser to be called`, () => { | ||
localStorageUserMiddleware(setUser)(store)(next)({ | ||
type: "editor/someAction", | ||
}); | ||
expect(setUser).toBeCalledWith(user); | ||
}); | ||
}); | ||
|
||
describe(`when authKey is not set in local storage`, () => { | ||
beforeEach(() => { | ||
localStorage.removeItem("authKey"); | ||
}); | ||
|
||
it(`expects setUser to not be called`, () => { | ||
localStorageUserMiddleware(setUser)(store)(next)({ | ||
type: "editor/someAction", | ||
}); | ||
expect(setUser).not.toBeCalled(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("as a logged in user", () => { | ||
const updatedUser = { user: "updated-user" }; | ||
|
||
beforeEach(() => { | ||
const initialState = { | ||
editor: {}, | ||
auth: { user }, | ||
}; | ||
store = mockStore(initialState); | ||
store.dispatch = jest.fn(); | ||
}); | ||
|
||
describe(`when user in local storage has been updated`, () => { | ||
beforeEach(() => { | ||
localStorage.setItem("authKey", "some-key"); | ||
localStorage.setItem("some-key", JSON.stringify(updatedUser)); | ||
}); | ||
|
||
it(`expects setUser to be called`, () => { | ||
localStorageUserMiddleware(setUser)(store)(next)({ | ||
type: "editor/someAction", | ||
}); | ||
expect(setUser).toBeCalledWith(updatedUser); | ||
}); | ||
}); | ||
|
||
describe(`when authKey is not set in local storage`, () => { | ||
beforeEach(() => { | ||
localStorage.removeItem("authKey"); | ||
localStorage.setItem("some-key", JSON.stringify(updatedUser)); | ||
}); | ||
|
||
it(`expects setUser to not be called`, () => { | ||
localStorageUserMiddleware(setUser)(store)(next)({ | ||
type: "editor/someAction", | ||
}); | ||
expect(setUser).not.toBeCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters