|
| 1 | +import React, { PropsWithChildren } from 'react'; |
| 2 | +import { renderHook } from '@testing-library/react'; |
| 3 | +import { Session } from 'next-auth'; |
| 4 | +import { useSession } from 'next-auth/react'; |
| 5 | +import { renderToString } from 'react-dom/server'; |
| 6 | +import TestRouter from '__tests__/util/TestRouter'; |
| 7 | +import { IntegrationAccordion } from 'src/components/Shared/Forms/Accordions/AccordionEnum'; |
| 8 | +import { useOauthUrl } from './useOauthUrl'; |
| 9 | + |
| 10 | +jest.mock('next-auth/react'); |
| 11 | + |
| 12 | +const accountListId = 'account-list-1'; |
| 13 | +const apiToken = 'apiToken'; |
| 14 | + |
| 15 | +const Wrapper = ({ children }: PropsWithChildren) => ( |
| 16 | + <TestRouter router={{ query: { accountListId } }}>{children}</TestRouter> |
| 17 | +); |
| 18 | + |
| 19 | +const renderUseOauthUrl = () => |
| 20 | + renderHook(() => useOauthUrl(), { wrapper: Wrapper }); |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + process.env.OAUTH_URL = 'https://auth.mpdx.org'; |
| 24 | + (useSession as jest.Mock).mockReturnValue({ |
| 25 | + data: { user: { apiToken } } as Session, |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +describe('useOauthUrl', () => { |
| 30 | + it('builds the Google OAuth url with the redirect back to integrations', () => { |
| 31 | + const { result } = renderUseOauthUrl(); |
| 32 | + |
| 33 | + expect(result.current.getGoogleOauthUrl()).toBe( |
| 34 | + `https://auth.mpdx.org/auth/user/google?account_list_id=${accountListId}&redirect_to=${encodeURIComponent( |
| 35 | + `http://localhost/accountLists/${accountListId}/settings/integrations?selectedTab=${IntegrationAccordion.Google}`, |
| 36 | + )}&access_token=${apiToken}`, |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it('includes the organization id in the DonorHub OAuth url', () => { |
| 41 | + const { result } = renderUseOauthUrl(); |
| 42 | + |
| 43 | + expect(result.current.getOrganizationOauthUrl('org-1')).toBe( |
| 44 | + `https://auth.mpdx.org/auth/user/donorhub?account_list_id=${accountListId}&redirect_to=${encodeURIComponent( |
| 45 | + `http://localhost/accountLists/${accountListId}/settings/integrations?selectedTab=${IntegrationAccordion.Organization}`, |
| 46 | + )}&access_token=${apiToken}&organization_id=org-1`, |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('renders on the server when window is undefined', () => { |
| 51 | + const Probe = () => { |
| 52 | + const { getGoogleOauthUrl } = useOauthUrl(); |
| 53 | + return <a href={getGoogleOauthUrl()}>{'link'}</a>; |
| 54 | + }; |
| 55 | + |
| 56 | + const originalWindow = global.window; |
| 57 | + // @ts-expect-error simulating a server environment with no window |
| 58 | + delete global.window; |
| 59 | + |
| 60 | + try { |
| 61 | + let html = ''; |
| 62 | + expect(() => { |
| 63 | + html = renderToString( |
| 64 | + <Wrapper> |
| 65 | + <Probe /> |
| 66 | + </Wrapper>, |
| 67 | + ); |
| 68 | + }).not.toThrow(); |
| 69 | + |
| 70 | + expect(html).toContain( |
| 71 | + `redirect_to=${encodeURIComponent( |
| 72 | + `/accountLists/${accountListId}/settings/integrations?selectedTab=${IntegrationAccordion.Google}`, |
| 73 | + )}`, |
| 74 | + ); |
| 75 | + } finally { |
| 76 | + global.window = originalWindow; |
| 77 | + } |
| 78 | + }); |
| 79 | +}); |
0 commit comments