Skip to content

Commit

Permalink
Add redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
caleballdrin committed Oct 14, 2024
1 parent 708ad91 commit 2487843
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { NextRouter } from 'next/router';
import React from 'react';
import { ThemeProvider } from '@mui/material/styles';
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { getSession } from 'next-auth/react';
import TestRouter from '__tests__/util/TestRouter';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
import { GetUserOptionsQuery } from 'src/components/Contacts/ContactFlow/GetUserOptions.generated';
Expand All @@ -25,7 +27,7 @@ const mockEnqueue = jest.fn();
const mutationSpy = jest.fn();
const push = jest.fn();

const router = {
const defaultRouter = {
query: { accountListId },
pathname: '/accountLists/[accountListId]/settings/preferences',
isReady: true,
Expand All @@ -48,13 +50,15 @@ interface MocksProvidersProps {
canUserExportData: boolean;
singleOrg?: boolean;
setup?: string;
router?: Partial<NextRouter> | undefined;
}

const MocksProviders: React.FC<MocksProvidersProps> = ({
children,
canUserExportData,
singleOrg,
setup,
router = defaultRouter,
}) => (
<ThemeProvider theme={theme}>
<TestRouter router={router}>
Expand Down Expand Up @@ -213,6 +217,41 @@ describe('Preferences page', () => {
expect(queryByText('Primary Organization')).not.toBeInTheDocument(),
);
});
describe('Export Redirect', () => {
const apiToken = 'apiToken';
beforeEach(() => {
(getSession as jest.Mock).mockResolvedValue({
apiToken,
});
});
it('redirects when exportId is provided in the URL', () => {
const router = {
isReady: true,
query: {
exportId: 'export_id',
accountListId,
},
};

const {} = render(
<MocksProviders
canUserExportData={false}
singleOrg={true}
router={router}
>
<Preferences />
</MocksProviders>,
);

expect(window.location.replace).toHaveBeenCalledWith(
`${
process.env.REST_API_URL
}/account_lists/${accountListId}/exports/${encodeURIComponent(
'export_id',
)}.xml?access_token=${apiToken}`,
);
});
});

describe('Setup Tour', () => {
it('should not show setup banner and accordions should not be disabled', async () => {
Expand Down
20 changes: 20 additions & 0 deletions pages/accountLists/[accountListId]/settings/preferences.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { AccordionGroup } from 'src/components/Shared/Forms/Accordions/Accordion
import { StickyBox } from 'src/components/Shared/Header/styledComponents';
import { useAccountListId } from 'src/hooks/useAccountListId';
import { useGetTimezones } from 'src/hooks/useGetTimezones';
import { useRequiredSession } from 'src/hooks/useRequiredSession';
import { getCountries } from 'src/lib/data/countries';
import { SettingsWrapper } from './Wrapper';

Expand All @@ -46,6 +47,7 @@ const Preferences: React.FC = () => {
const { push, query } = useRouter();
const { enqueueSnackbar } = useSnackbar();
const { onSetupTour } = useSetupContext();
const session = useRequiredSession();

const setupAccordions = ['locale', 'monthly goal', 'home country'];
const [setup, setSetup] = useState(0);
Expand All @@ -57,6 +59,24 @@ const Preferences: React.FC = () => {

const [updateUserOptions] = useUpdateUserOptionsMutation();

useEffect(() => {
const redirectToDownloadExportedData = (exportDataExportId: string) => {
if (!exportDataExportId) {return;}

const url = `${
process.env.REST_API_URL
}/account_lists/${accountListId}/exports/${encodeURIComponent(
exportDataExportId,
)}.xml?access_token=${session.apiToken}`;

window.location.replace(url);
};

if (query.exportId && typeof query.exportId === 'string') {
redirectToDownloadExportedData(query.exportId);
}
}, [query.exportId, accountListId]);

const { data: personalPreferencesData, loading: personalPreferencesLoading } =
useGetPersonalPreferencesQuery({
variables: {
Expand Down

0 comments on commit 2487843

Please sign in to comment.