-
Notifications
You must be signed in to change notification settings - Fork 369
feat(clerk-js): Correct mounted component/modal behaviour #707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
desiprisg
merged 7 commits into
main
from
george/js-111-add-a-warning-to-opensignin-and
Jan 30, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6146e24
feat(clerk-js): Redirect to home when an unavailable component is mou…
desiprisg 3517c83
feat(clerk-js): Don't render unavailable modals and show warnings
desiprisg bbd0ad5
fix(clerk-js): Only show redirect warning on development
desiprisg ce1172d
test(clerk-js): Add redirectToHome tests
desiprisg a9aca86
refactor(clerk-js): Dry withRedirectToHome HOCs
desiprisg 6357d31
refactor(clerk-js): Refactor clerk requirements for withRedirectToHom…
desiprisg 5d4f6f8
chore(clerk-js): Rename clerkRequirements to componentGuards
desiprisg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
167 changes: 80 additions & 87 deletions
167
packages/clerk-js/src/ui/common/__tests__/withRedirectToHome.test.tsx
This file contains hidden or 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 |
---|---|---|
@@ -1,108 +1,101 @@ | ||
import { render, screen } from '@clerk/shared/testUtils'; | ||
import type { EnvironmentResource } from '@clerk/types'; | ||
import React from 'react'; | ||
|
||
import type { AuthConfig, DisplayConfig } from '../../../core/resources'; | ||
import { CoreSessionContext, useEnvironment } from '../../contexts'; | ||
import { withRedirectToHome } from '../withRedirectToHome'; | ||
import { bindCreateFixtures, render, screen } from '../../../testUtils'; | ||
import { | ||
withRedirectToHomeOrganizationGuard, | ||
withRedirectToHomeSingleSessionGuard, | ||
withRedirectToHomeUserGuard, | ||
} from '../withRedirectToHome'; | ||
|
||
const mockNavigate = jest.fn(); | ||
jest.mock('ui/hooks', () => ({ | ||
useNavigate: () => ({ navigate: mockNavigate }), | ||
})); | ||
const { createFixtures } = bindCreateFixtures('SignIn'); | ||
|
||
jest.mock('ui/contexts', () => ({ | ||
...jest.requireActual('ui/contexts'), | ||
useEnvironment: jest.fn(), | ||
})); | ||
describe('withRedirectToHome', () => { | ||
describe('withRedirectToHomeSingleSessionGuard', () => { | ||
it('redirects if a session is present and single session mode is enabled', async () => { | ||
const { wrapper, fixtures } = await createFixtures(f => { | ||
f.withUser({}); | ||
}); | ||
|
||
const Tester = () => <div>Tester</div>; | ||
const WithHOC = withRedirectToHomeSingleSessionGuard(() => <></>); | ||
|
||
describe('withRedirectToHome(Component)', () => { | ||
beforeEach(() => { | ||
mockNavigate.mockReset(); | ||
}); | ||
render(<WithHOC />, { wrapper }); | ||
|
||
describe('when there is a session', () => { | ||
describe('and the instance is in single session mode', () => { | ||
beforeEach(() => { | ||
(useEnvironment as jest.Mock).mockImplementation( | ||
() => | ||
({ | ||
displayConfig: { | ||
homeUrl: 'http://my-home.com', | ||
} as Partial<DisplayConfig>, | ||
authConfig: { | ||
singleSessionMode: true, | ||
} as Partial<AuthConfig>, | ||
} as Partial<EnvironmentResource>), | ||
); | ||
}); | ||
expect(fixtures.router.navigate).toHaveBeenCalledWith(fixtures.environment.displayConfig.homeUrl); | ||
}); | ||
|
||
it('navigates to home_url', () => { | ||
const Component = withRedirectToHome(Tester); | ||
render( | ||
<CoreSessionContext.Provider value={{ value: { id: 'sess_id' } as any }}> | ||
<Component /> | ||
</CoreSessionContext.Provider>, | ||
); | ||
expect(mockNavigate).toHaveBeenNthCalledWith(1, 'http://my-home.com'); | ||
expect(screen.queryByText('Tester')).not.toBeInTheDocument(); | ||
}); | ||
it('renders the children if is a session is not present', async () => { | ||
const { wrapper } = await createFixtures(); | ||
|
||
const WithHOC = withRedirectToHomeSingleSessionGuard(() => <>test</>); | ||
|
||
render(<WithHOC />, { wrapper }); | ||
|
||
screen.getByText('test'); | ||
}); | ||
|
||
describe('and the instance is not in single session mode', () => { | ||
beforeEach(() => { | ||
(useEnvironment as jest.Mock).mockImplementation( | ||
() => | ||
({ | ||
displayConfig: { | ||
homeUrl: 'http://my-home.com', | ||
} as Partial<DisplayConfig>, | ||
authConfig: { | ||
singleSessionMode: false, | ||
} as Partial<AuthConfig>, | ||
} as Partial<EnvironmentResource>), | ||
); | ||
it('renders the children if multi session mode is enabled and a session is present', async () => { | ||
const { wrapper } = await createFixtures(f => { | ||
f.withUser({}); | ||
f.withMultiSessionMode(); | ||
}); | ||
|
||
it('renders the wrapped component', () => { | ||
const Component = withRedirectToHome(Tester); | ||
render( | ||
<CoreSessionContext.Provider value={{ value: { id: 'sess_id' } as any }}> | ||
<Component /> | ||
</CoreSessionContext.Provider>, | ||
); | ||
expect(mockNavigate).not.toHaveBeenCalled(); | ||
expect(screen.queryByText('Tester')).toBeInTheDocument(); | ||
const WithHOC = withRedirectToHomeSingleSessionGuard(() => <>test</>); | ||
|
||
render(<WithHOC />, { wrapper }); | ||
|
||
screen.getByText('test'); | ||
}); | ||
}); | ||
|
||
describe('redirectToHomeUserGuard', () => { | ||
it('redirects if no user is present', async () => { | ||
const { wrapper, fixtures } = await createFixtures(); | ||
|
||
const WithHOC = withRedirectToHomeUserGuard(() => <></>); | ||
|
||
render(<WithHOC />, { wrapper }); | ||
|
||
expect(fixtures.router.navigate).toHaveBeenCalledWith(fixtures.environment.displayConfig.homeUrl); | ||
}); | ||
|
||
it('renders the children if is a user is present', async () => { | ||
const { wrapper } = await createFixtures(f => { | ||
f.withUser({}); | ||
}); | ||
|
||
const WithHOC = withRedirectToHomeUserGuard(() => <>test</>); | ||
|
||
render(<WithHOC />, { wrapper }); | ||
|
||
screen.getByText('test'); | ||
}); | ||
}); | ||
|
||
describe('when there is no session', () => { | ||
beforeEach(() => { | ||
(useEnvironment as jest.Mock).mockImplementation( | ||
() => | ||
({ | ||
displayConfig: { | ||
homeUrl: 'http://my-home.com', | ||
} as Partial<DisplayConfig>, | ||
authConfig: { | ||
singleSessionMode: true, | ||
} as Partial<AuthConfig>, | ||
} as Partial<EnvironmentResource>), | ||
); | ||
describe('withRedirectToHomeOrganizationGuard', () => { | ||
it('redirects if no organization is active', async () => { | ||
const { wrapper, fixtures } = await createFixtures(f => { | ||
f.withUser({}); | ||
f.withOrganizations(); | ||
}); | ||
|
||
const WithHOC = withRedirectToHomeOrganizationGuard(() => <></>); | ||
|
||
render(<WithHOC />, { wrapper }); | ||
|
||
expect(fixtures.router.navigate).toHaveBeenCalledWith(fixtures.environment.displayConfig.homeUrl); | ||
}); | ||
|
||
it('renders the wrapped component', () => { | ||
const Component = withRedirectToHome(Tester); | ||
render( | ||
<CoreSessionContext.Provider value={{ value: undefined }}> | ||
<Component /> | ||
</CoreSessionContext.Provider>, | ||
); | ||
expect(mockNavigate).not.toHaveBeenCalled(); | ||
expect(screen.queryByText('Tester')).toBeInTheDocument(); | ||
it('renders the children if is an organization is active', async () => { | ||
const { wrapper } = await createFixtures(f => { | ||
f.withUser({ organization_memberships: ['Org1'] }); | ||
f.withOrganizations(); | ||
}); | ||
|
||
const WithHOC = withRedirectToHomeOrganizationGuard(() => <>test</>); | ||
|
||
render(<WithHOC />, { wrapper }); | ||
|
||
screen.getByText('test'); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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 |
---|---|---|
@@ -1,39 +1,65 @@ | ||
import type { SignInProps, SignUpProps } from '@clerk/types'; | ||
import React, { useContext } from 'react'; | ||
import type { ComponentType } from 'react'; | ||
import React from 'react'; | ||
|
||
import { useEnvironment } from '../contexts'; | ||
import { CoreSessionContext } from '../contexts/CoreSessionContext'; | ||
import type { AvailableComponentProps } from '../../ui/types'; | ||
import type { ComponentGuard } from '../../utils'; | ||
import { noOrganizationExists, noUserExists, sessionExistsAndSingleSessionModeEnabled } from '../../utils'; | ||
import { useCoreClerk, useEnvironment, useOptions } from '../contexts'; | ||
import { useNavigate } from '../hooks'; | ||
|
||
export function withRedirectToHome<P extends SignInProps | SignUpProps | { continueExisting?: boolean }>( | ||
Component: React.ComponentType<P>, | ||
displayName?: string, | ||
function withRedirectToHome<P extends AvailableComponentProps>( | ||
Component: ComponentType<P>, | ||
condition: ComponentGuard, | ||
warning?: string, | ||
): (props: P) => null | JSX.Element { | ||
displayName = displayName || Component.displayName || Component.name || 'Component'; | ||
const displayName = Component.displayName || Component.name || 'Component'; | ||
Component.displayName = displayName; | ||
|
||
const HOC = (props: P) => { | ||
const { navigate } = useNavigate(); | ||
const { authConfig, displayConfig } = useEnvironment(); | ||
const { singleSessionMode } = authConfig; | ||
const ctx = useContext(CoreSessionContext); | ||
const session = ctx?.value; | ||
const clerk = useCoreClerk(); | ||
const environment = useEnvironment(); | ||
const options = useOptions(); | ||
|
||
const shouldRedirect = condition(clerk, environment, options); | ||
React.useEffect(() => { | ||
if (singleSessionMode && !!session) { | ||
navigate(displayConfig.homeUrl); | ||
if (shouldRedirect) { | ||
if (warning && environment.displayConfig.instanceEnvironmentType === 'development') { | ||
console.warn(warning); | ||
} | ||
navigate(environment.displayConfig.homeUrl); | ||
} | ||
}, []); | ||
|
||
if (singleSessionMode && !!session) { | ||
if (shouldRedirect) { | ||
return null; | ||
} | ||
|
||
// @ts-ignore | ||
return <Component {...props} />; | ||
}; | ||
|
||
HOC.displayName = `withRedirectToHome(${displayName})`; | ||
|
||
return HOC; | ||
} | ||
|
||
export const withRedirectToHomeSingleSessionGuard = <P extends AvailableComponentProps>(Component: ComponentType<P>) => | ||
withRedirectToHome( | ||
Component, | ||
sessionExistsAndSingleSessionModeEnabled, | ||
'Cannot render the component as a session already exists and single session mode is enabled. Redirecting to the home url.', | ||
); | ||
|
||
export const withRedirectToHomeUserGuard = <P extends AvailableComponentProps>(Component: ComponentType<P>) => | ||
desiprisg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
withRedirectToHome( | ||
Component, | ||
noUserExists, | ||
'Cannot render the component as no user exists. Redirecting to the home url.', | ||
); | ||
|
||
export const withRedirectToHomeOrganizationGuard = <P extends AvailableComponentProps>(Component: ComponentType<P>) => | ||
withRedirectToHome( | ||
Component, | ||
noOrganizationExists, | ||
'Cannot render the component as there is no active organization. Redirecting to the home url.', | ||
); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.