-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create custom hook for LOA3 user data in Health Care Application (#32169
- Loading branch information
Showing
3 changed files
with
82 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useEffect } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { fetchEnrollmentStatus } from '../utils/actions/enrollment-status'; | ||
import { fetchTotalDisabilityRating } from '../utils/actions/disability-rating'; | ||
import { selectAuthStatus } from '../utils/selectors/auth-status'; | ||
|
||
export const useLoa3UserData = () => { | ||
const { isUserLOA3 } = useSelector(selectAuthStatus); | ||
const dispatch = useDispatch(); | ||
|
||
const getDisabilityRating = () => dispatch(fetchTotalDisabilityRating()); | ||
const getEnrollmentStatus = () => dispatch(fetchEnrollmentStatus()); | ||
|
||
useEffect( | ||
() => { | ||
if (isUserLOA3) { | ||
getDisabilityRating(); | ||
getEnrollmentStatus(); | ||
} | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[isUserLOA3], | ||
); | ||
}; |
50 changes: 50 additions & 0 deletions
50
src/applications/hca/tests/unit/hooks/useLoa3UserData.unit.spec.js
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,50 @@ | ||
import React from 'react'; | ||
import { Provider } from 'react-redux'; | ||
import { render } from '@testing-library/react'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { useLoa3UserData } from '../../../hooks/useLoa3UserData'; | ||
|
||
// create wrapper component for our hook | ||
const TestComponent = () => { | ||
useLoa3UserData(); | ||
return null; | ||
}; | ||
|
||
describe('hca `useLoa3UserData` hook', () => { | ||
const getData = ({ loggedIn = true }) => ({ | ||
mockStore: { | ||
getState: () => ({ | ||
user: { | ||
login: { currentlyLoggedIn: loggedIn }, | ||
profile: { | ||
loa: { current: loggedIn ? 3 : null }, | ||
loading: false, | ||
}, | ||
}, | ||
}), | ||
subscribe: () => {}, | ||
dispatch: sinon.stub(), | ||
}, | ||
}); | ||
const subject = ({ mockStore }) => | ||
render( | ||
<Provider store={mockStore}> | ||
<TestComponent /> | ||
</Provider>, | ||
); | ||
|
||
it('should fire the dispatch action(s) when the user is logged in', () => { | ||
const { mockStore } = getData({}); | ||
const { dispatch } = mockStore; | ||
subject({ mockStore }); | ||
expect(dispatch.called).to.be.true; | ||
}); | ||
|
||
it('should not fire the dispatch action(s) when the user is logged out', () => { | ||
const { mockStore } = getData({ loggedIn: false }); | ||
const { dispatch } = mockStore; | ||
subject({ mockStore }); | ||
expect(dispatch.called).to.be.false; | ||
}); | ||
}); |