From cfb5790aa58270ae8a364dfe737c69057d8982ba Mon Sep 17 00:00:00 2001 From: Spencer Stolworthy Date: Fri, 31 Jul 2020 09:28:17 -0500 Subject: [PATCH] =?UTF-8?q?fixed=20unexpected=20behaviors=20around=20savin?= =?UTF-8?q?g=20an=20engagement=20and=20polling=20fo=E2=80=A6=20(#285)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fixed unexpected behaviors around saving an engagement and polling for updates * removed environment class * removed useless comments --- .../__tests__/engagement_context.spec.tsx | 12 ++++--- .../engagement_context/engagement_context.tsx | 23 +++++++------ .../__snapshots__/dashboard.spec.tsx.snap | 32 ++++++++++++++----- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/context/engagement_context/__tests__/engagement_context.spec.tsx b/src/context/engagement_context/__tests__/engagement_context.spec.tsx index 556904ff4..a8d4d14f3 100644 --- a/src/context/engagement_context/__tests__/engagement_context.spec.tsx +++ b/src/context/engagement_context/__tests__/engagement_context.spec.tsx @@ -24,9 +24,9 @@ describe('Engagement Context', () => { cleanup(); }); - test('by default, engagements are undefined', () => { + test('by default, engagements are an empty array', () => { const { result } = getHook(); - expect(result.current.engagements).toEqual(undefined); + expect(result.current.engagements).toEqual([]); }); test('Fetch Engagements', async () => { @@ -246,7 +246,11 @@ describe('Engagement Context', () => { result.current.getEngagements(); await waitForNextUpdate(); }); - expect(result.current.engagements[0]).toEqual(initialEngagement); + await act(async () => { + result.current.setCurrentEngagement(result.current.engagements[0]); + await waitForNextUpdate(); + }); + expect(result.current.currentEngagement).toEqual(initialEngagement); let modifiedEngagement = { ...initialEngagement, customer_contact_email: 'tennessee@nasa.gov', @@ -255,7 +259,7 @@ describe('Engagement Context', () => { result.current.saveEngagement(modifiedEngagement); await waitForNextUpdate(); }); - expect(result.current.engagements[0]).toEqual(modifiedEngagement); + expect(result.current.currentEngagement).toEqual(modifiedEngagement); }); test('saveEngagement reverts to the initial engagement when the save was unsuccessful', async () => { const initialEngagement = Engagement.fromFake(true); diff --git a/src/context/engagement_context/engagement_context.tsx b/src/context/engagement_context/engagement_context.tsx index fcab8b36d..5cac0d517 100644 --- a/src/context/engagement_context/engagement_context.tsx +++ b/src/context/engagement_context/engagement_context.tsx @@ -74,7 +74,7 @@ export const EngagementProvider = ({ const [error] = useState(); const [isLoading] = useState(false); - const [engagements, setEngagements] = useState(undefined); + const [engagements, setEngagements] = useState([]); const [currentEngagement, setCurrentEngagement] = useState< Engagement | undefined >(); @@ -179,6 +179,15 @@ export const EngagementProvider = ({ [_updateEngagementInPlace, engagementService, _validateAuthStatus] ); + const _checkHasUpdateRef = useRef(async () => false); + + useEffect(() => { + const checkUpdate = async () => { + return await engagementService.checkHasUpdates(currentEngagement); + }; + _checkHasUpdateRef.current = checkUpdate; + }, [currentEngagement, engagementService]); + const createEngagementPoll = useCallback( async (engagement: Engagement): Promise => { await _validateAuthStatus(); @@ -186,9 +195,7 @@ export const EngagementProvider = ({ new EngagementPollIntervalStrategy( setInterval(async () => { await _validateAuthStatusRef.current(); - const hasUpdates = await engagementService.checkHasUpdates( - engagement - ); + const hasUpdates = await _checkHasUpdateRef.current(); if (hasUpdates) { feedbackContext.showAlert( 'Another user edited this engagement. In order to continue, you must refresh the page. By refreshing, your unsaved changes will be overwritten."', @@ -206,12 +213,7 @@ export const EngagementProvider = ({ ) ); }, - [ - _validateAuthStatus, - _refreshEngagementData, - engagementService, - feedbackContext, - ] + [_validateAuthStatus, _refreshEngagementData, feedbackContext] ); const getEngagement = useCallback( @@ -335,6 +337,7 @@ export const EngagementProvider = ({ ); feedbackContext.hideLoader(); _updateEngagementInPlace(returnedEngagement); + setCurrentEngagement(returnedEngagement); } catch (e) { _updateEngagementInPlace(oldEngagement); feedbackContext.hideLoader(); diff --git a/src/routes/dashboard/__snapshots__/dashboard.spec.tsx.snap b/src/routes/dashboard/__snapshots__/dashboard.spec.tsx.snap index 384057f44..a0688b8f2 100644 --- a/src/routes/dashboard/__snapshots__/dashboard.spec.tsx.snap +++ b/src/routes/dashboard/__snapshots__/dashboard.spec.tsx.snap @@ -65,7 +65,9 @@ Object { class="" data-cy="numbers_of_l" data-pf-content="true" - /> + > + 0 +