From aa0d6cb1bafc79475d24232204f54cdcc2cafd8a Mon Sep 17 00:00:00 2001 From: jay-hodgson Date: Fri, 25 Oct 2024 13:40:16 -0700 Subject: [PATCH] update tests and clean imports --- .../session/ApplicationSession.test.tsx | 37 +++++++------------ .../session/ApplicationSessionManager.tsx | 2 - 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/packages/synapse-react-client/src/utils/AppUtils/session/ApplicationSession.test.tsx b/packages/synapse-react-client/src/utils/AppUtils/session/ApplicationSession.test.tsx index cca5261174..05b68df76a 100644 --- a/packages/synapse-react-client/src/utils/AppUtils/session/ApplicationSession.test.tsx +++ b/packages/synapse-react-client/src/utils/AppUtils/session/ApplicationSession.test.tsx @@ -12,10 +12,7 @@ import { FullContextProvider, SynapseClient, } from '../../../index' -import { - MOCK_USER_ID, - mockUserProfileData, -} from '../../../mocks/user/mock_user_profile' +import { MOCK_USER_ID } from '../../../mocks/user/mock_user_profile' import { SynapseClientError } from '@sage-bionetworks/synapse-client/util/SynapseClientError' import { MemoryRouter } from 'react-router-dom' import * as UseDetectSSOCodeModule from '../../hooks/useDetectSSOCode' @@ -101,7 +98,6 @@ describe('ApplicationSessionManager tests', () => { 'getAccessTokenFromCookie', ) const signOutSpy = jest.spyOn(SynapseClient, 'signOut') - const mockGetUserProfile = jest.spyOn(SynapseClient, 'getUserProfile') const mockAuthenticatedOn = jest.spyOn(SynapseClient, 'getAuthenticatedOn') const mockTermsOfServiceStatus = jest.spyOn( SynapseClient, @@ -122,7 +118,6 @@ describe('ApplicationSessionManager tests', () => { it('Bootstraps when signed in', async () => { mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN) - mockGetUserProfile.mockResolvedValue(mockUserProfileData) mockTermsOfServiceStatus.mockResolvedValue( TERMS_OF_SERVICE_STATUS_UP_TO_DATE, ) @@ -130,7 +125,7 @@ describe('ApplicationSessionManager tests', () => { await waitFor(() => { expect(mockGetAccessToken).toHaveBeenCalled() - expect(mockGetUserProfile).toHaveBeenCalled() + expect(mockTermsOfServiceStatus).toHaveBeenCalled() expect(signOutSpy).not.toHaveBeenCalled() expect(context.result.current).toMatchObject(EXPECTED_AUTH_STATE) }) @@ -141,19 +136,12 @@ describe('ApplicationSessionManager tests', () => { TERMS_OF_SERVICE_STATUS_MUST_AGREE_NOW, ) mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN) - mockGetUserProfile.mockRejectedValue( - new SynapseClientError( - 403, - 'Terms of use have not been signed.', - expect.getState().currentTestName!, - ), - ) const context = render() await waitFor(() => { expect(mockGetAccessToken).toHaveBeenCalled() - expect(mockGetUserProfile).toHaveBeenCalled() + expect(mockTermsOfServiceStatus).toHaveBeenCalled() expect(signOutSpy).not.toHaveBeenCalled() expect(context.result.current).toMatchObject( EXPECTED_AUTH_STATE_TERMS_NOT_ACCEPTED, @@ -179,7 +167,9 @@ describe('ApplicationSessionManager tests', () => { // At this point, useDetectSSOCode would use the URL query parameters to finish sign in mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN) - mockGetUserProfile.mockResolvedValue(mockUserProfileData) + mockTermsOfServiceStatus.mockResolvedValue( + TERMS_OF_SERVICE_STATUS_UP_TO_DATE, + ) mockUseDetectSSOCode.mockReturnValue({ isLoading: false }) // useDetectSSOCode will then invoke onSignInComplete to refresh the session @@ -297,14 +287,13 @@ describe('ApplicationSessionManager tests', () => { ) // Using the app's Login component, the user logs in. the component invokes refreshSession once the token is stored mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN) - mockGetUserProfile.mockResolvedValue(mockUserProfileData) // Call under test await context.result.current.refreshSession() await waitFor(() => { expect(mockGetAccessToken).toHaveBeenCalled() - expect(mockGetUserProfile).toHaveBeenCalled() + expect(mockTermsOfServiceStatus).toHaveBeenCalled() expect(context.result.current).toMatchObject(EXPECTED_AUTH_STATE) }) }) @@ -315,13 +304,12 @@ describe('ApplicationSessionManager tests', () => { TERMS_OF_SERVICE_STATUS_UP_TO_DATE, ) mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN) - mockGetUserProfile.mockResolvedValue(mockUserProfileData) const context = render() await waitFor(() => { expect(mockGetAccessToken).toHaveBeenCalled() - expect(mockGetUserProfile).toHaveBeenCalled() + expect(mockTermsOfServiceStatus).toHaveBeenCalled() expect(signOutSpy).not.toHaveBeenCalled() expect(context.result.current).toMatchObject(EXPECTED_AUTH_STATE) }) @@ -344,7 +332,6 @@ describe('ApplicationSessionManager tests', () => { TERMS_OF_SERVICE_STATUS_UP_TO_DATE, ) mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN) - mockGetUserProfile.mockResolvedValue(mockUserProfileData) mockAuthenticatedOn.mockResolvedValue({ authenticatedOn, }) @@ -355,7 +342,7 @@ describe('ApplicationSessionManager tests', () => { await waitFor(() => { expect(mockGetAccessToken).toHaveBeenCalled() - expect(mockGetUserProfile).toHaveBeenCalled() + expect(mockTermsOfServiceStatus).toHaveBeenCalled() expect(mockAuthenticatedOn).toHaveBeenCalled() expect(signOutSpy).not.toHaveBeenCalled() expect(context.result.current).toMatchObject(EXPECTED_AUTH_STATE) @@ -368,7 +355,9 @@ describe('ApplicationSessionManager tests', () => { const maxAge = 60 * 60 const token = 'asdf' mockGetAccessToken.mockResolvedValue(token) - mockGetUserProfile.mockResolvedValue(mockUserProfileData) + mockTermsOfServiceStatus.mockResolvedValue( + TERMS_OF_SERVICE_STATUS_UP_TO_DATE, + ) console.log(dayjs.utc().subtract(24, 'hours').format()) mockAuthenticatedOn.mockResolvedValue({ @@ -382,7 +371,7 @@ describe('ApplicationSessionManager tests', () => { await waitFor(() => { expect(mockGetAccessToken).toHaveBeenCalled() expect(mockAuthenticatedOn).toHaveBeenCalled() - expect(mockGetUserProfile).not.toHaveBeenCalled() + expect(mockTermsOfServiceStatus).not.toHaveBeenCalled() expect(signOutSpy).not.toHaveBeenCalled() expect(context.result.current).toMatchObject(EXPECTED_ANONYMOUS_STATE) }) diff --git a/packages/synapse-react-client/src/utils/AppUtils/session/ApplicationSessionManager.tsx b/packages/synapse-react-client/src/utils/AppUtils/session/ApplicationSessionManager.tsx index e644cc7988..c5e1a4000a 100644 --- a/packages/synapse-react-client/src/utils/AppUtils/session/ApplicationSessionManager.tsx +++ b/packages/synapse-react-client/src/utils/AppUtils/session/ApplicationSessionManager.tsx @@ -8,8 +8,6 @@ import { ApplicationSessionContextProvider } from './ApplicationSessionContext' import { SynapseContextProvider, SynapseContextType } from '../../context' import dayjs from 'dayjs' import { useTermsOfServiceStatus } from '../../../synapse-queries/termsOfService/useTermsOfService' -import { PresentToAll } from '@mui/icons-material' -import { TermsOfServiceState } from '@sage-bionetworks/synapse-types' export type ApplicationSessionManagerProps = React.PropsWithChildren<{ downloadCartPageUrl?: string