-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the mapping functions from state to properties so they can be…
… tested One option for testing mapStateToProps directly would have required exporting private code as public, just for the purpose of testing. That's a bit of a code smell. Another option would be to pass a mock store into the component. That was something we could do in react-redux v5 and which was restored in v7, but it was removed in v6, which is what we are on. See reduxjs/react-redux#1161 This seems like the best way of testing this mapping without upgrading react-redux or unnecessarily breaking encapsulation.
- Loading branch information
Showing
5 changed files
with
84 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2019 Stanford University see LICENSE for license | ||
|
||
import { getCurrentUser, getCurrentSession, getAuthenticationError, getAuthenticationState } from '../src/selectors'; | ||
|
||
describe('getCurrentUser', () => { | ||
const currentUser = { hello: 'world' } | ||
|
||
const state = { | ||
authenticate: { | ||
authenticationState: { | ||
currentUser: currentUser | ||
} | ||
} | ||
}; | ||
|
||
it('returns user', () => { | ||
expect(getCurrentUser(state)).toBe(currentUser) | ||
}) | ||
}) | ||
|
||
describe('getCurrentSession', () => { | ||
const currentSession = { hello: 'world' } | ||
|
||
const state = { | ||
authenticate: { | ||
authenticationState: { | ||
currentSession: currentSession | ||
} | ||
} | ||
}; | ||
|
||
it('returns currentSession', () => { | ||
expect(getCurrentSession(state)).toBe(currentSession) | ||
}) | ||
}) | ||
|
||
describe('getAuthenticationError', () => { | ||
const authenticationError = { hello: 'world' } | ||
|
||
const state = { | ||
authenticate: { | ||
authenticationState: { | ||
authenticationError: authenticationError | ||
} | ||
} | ||
}; | ||
|
||
it('returns authentication error', () => { | ||
expect(getAuthenticationError(state)).toBe(authenticationError) | ||
}) | ||
}) | ||
|
||
describe('getAuthenticationState', () => { | ||
const authenticationState = { authenticationError: 'broken' } | ||
|
||
const state = { | ||
authenticate: { | ||
authenticationState: authenticationState | ||
} | ||
}; | ||
|
||
it('returns a copy of the authentication state', () => { | ||
const result = getAuthenticationState(state) | ||
expect(result).not.toBe(authenticationState) | ||
expect(result).toEqual(authenticationState) | ||
}) | ||
}) |
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,8 @@ | ||
|
||
export const getCurrentUser = (state) => (getAuthenticationState(state)?.currentUser) | ||
|
||
export const getCurrentSession = (state) => (getAuthenticationState(state)?.currentSession) | ||
|
||
export const getAuthenticationError = (state) => (getAuthenticationState(state)?.authenticationError) | ||
|
||
export const getAuthenticationState = (state) => ({...state.authenticate.authenticationState}) |