Skip to content

Commit

Permalink
fix: Added referrer to all login buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Mar 19, 2024
1 parent c8c48bb commit e7b466a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/account/components/AccountProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ const AccountProfile = () => {
response => _.get('meta.requestStatus', response) === 'fulfilled'
);
if (allSuccess) {
goToReferrer('/account/profile');
goToReferrer(completeProfile ? '/' : '/account/profile');
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/account/components/AccountProfile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ test('AccountProfile: Complete profile', async () => {
await act(async () =>
fireEvent.click(screen.getByRole('button', { name: 'Save Profile' }))
);
expect(navigate).toHaveBeenCalledWith('/account/profile', { replace: true });
expect(navigate).toHaveBeenCalledWith('/', { replace: true });
});

test('AccountProfile: Navigate to referrer after complete profile', async () => {
Expand Down
7 changes: 5 additions & 2 deletions src/account/components/OptionalAuth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test('OptionalAuth: Display messages', async () => {

expect(screen.getByRole('link', { name: 'Join Terraso' })).toHaveAttribute(
'href',
'/account'
'/account?referrer=%2Ftools%2Fstory-maps%2Fjqbb8ss%2Ftest-story'
);
expect(
screen.getByText(/and create your own story map for free/i)
Expand All @@ -57,7 +57,10 @@ test('OptionalAuth: Display messages', async () => {
).toBeInTheDocument();
expect(
screen.getByRole('link', { name: 'signing up for Terraso' })
).toHaveAttribute('href', '/account');
).toHaveAttribute(
'href',
'/account?referrer=%2Ftools%2Fstory-maps%2Fjqbb8ss%2Ftest-story'
);
});

test('OptionalAuth: Dont Display messages', async () => {
Expand Down
8 changes: 7 additions & 1 deletion src/account/components/OptionalAuthBottomMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import React from 'react';
import _ from 'lodash/fp';
import { Trans, withTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';
import { Box, Typography } from '@mui/material';

import RouterLink from 'common/components/RouterLink';
import { useOptionalAuth } from 'navigation/components/Routes';
import { generateReferrerUrl } from 'navigation/navigationUtils';

const OptionalAuthBottomMessage = () => {
const location = useLocation();
const { bottomMessage } = useOptionalAuth();
const hasToken = useSelector(_.get('account.hasToken'));

Expand All @@ -48,7 +51,10 @@ const OptionalAuthBottomMessage = () => {
>
<Trans i18nKey={bottomMessage}>
prefix
<RouterLink to="/account" sx={{ textDecoration: 'underline' }}>
<RouterLink
to={generateReferrerUrl('/account', location)}
sx={{ textDecoration: 'underline' }}
>
link
</RouterLink>
suffix
Expand Down
5 changes: 4 additions & 1 deletion src/account/components/OptionalAuthTopMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import React from 'react';
import _ from 'lodash/fp';
import { Trans, withTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';
import { Typography } from '@mui/material';

import RouterLink from 'common/components/RouterLink';
import { useOptionalAuth } from 'navigation/components/Routes';
import { generateReferrerUrl } from 'navigation/navigationUtils';

const OptionalAuthTopMessage = () => {
const location = useLocation();
const { topMessage } = useOptionalAuth();
const hasToken = useSelector(_.get('account.hasToken'));

Expand All @@ -44,7 +47,7 @@ const OptionalAuthTopMessage = () => {
<Trans i18nKey={topMessage}>
prefix
<RouterLink
to="/account"
to={generateReferrerUrl('/account', location)}
sx={{ color: 'white', textDecoration: 'underline' }}
>
link
Expand Down
5 changes: 2 additions & 3 deletions src/layout/AppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import ConditionalLink from 'common/components/ConditionalLink';
import LocalePicker from 'localization/components/LocalePicker';
import { useOptionalAuth } from 'navigation/components/Routes';
import SkipLinks from 'navigation/components/SkipLinks';
import { generateReferrerPath } from 'navigation/navigationUtils';
import { generateReferrerUrl } from 'navigation/navigationUtils';
import AccountAvatar from 'account/components/AccountAvatar';

import logoSquare from 'assets/logo-square.svg';
Expand All @@ -49,8 +49,7 @@ const AppBarComponent = () => {
}, [dispatch]);

const onSignIn = useCallback(() => {
const referrer = generateReferrerPath(location);
const to = referrer ? `/account?referrer=${referrer}` : '/account';
const to = generateReferrerUrl('/account', location);
navigate(to);
}, [location, navigate]);

Expand Down
60 changes: 57 additions & 3 deletions src/layout/AppBar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,28 @@ import { fireEvent, render, screen } from 'tests/utils';
import React from 'react';
import Cookies from 'js-cookie';
import { act } from 'react-dom/test-utils';
import { useLocation, useNavigate } from 'react-router-dom';
import useMediaQuery from '@mui/material/useMediaQuery';

import AppBar from 'layout/AppBar';
import { useOptionalAuth } from 'navigation/components/Routes';

jest.mock('@mui/material/useMediaQuery');
jest.mock('js-cookie');

const setup = async () => {
await render(<AppBar />, {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: jest.fn(),
useLocation: jest.fn(),
}));

jest.mock('navigation/components/Routes', () => ({
...jest.requireActual('navigation/components/Routes'),
useOptionalAuth: jest.fn(),
}));

const setup = async (
initialState = {
account: {
hasToken: true,
currentUser: {
Expand All @@ -37,11 +50,21 @@ const setup = async () => {
},
},
},
});
}
) => {
await render(<AppBar />, initialState);
};

beforeEach(() => {
global.fetch = jest.fn();
useNavigate.mockReturnValue(jest.fn());
useOptionalAuth.mockReturnValue({
enabled: false,
});
useLocation.mockReturnValue({
pathname: '/groups',
search: '?sort=-name&other=1',
});
});

test('AppBar: Dont display if no user', async () => {
Expand Down Expand Up @@ -99,3 +122,34 @@ test('AppBar: Sign out', async () => {
path: '/',
});
});

test('AppBar: Add log in referrer', async () => {
const navigate = jest.fn();
useNavigate.mockReturnValue(navigate);

useOptionalAuth.mockReturnValue({
enabled: true,
});

global.fetch.mockResolvedValueOnce({
status: 200,
});
useMediaQuery.mockReturnValue(false);
await setup({
account: {
hasToken: false,
currentUser: {
fetching: false,
data: {},
},
},
});

await act(async () =>
fireEvent.click(screen.getByRole('button', { name: 'Log In' }))
);

expect(navigate).toHaveBeenCalledWith(
'/account?referrer=%2Fgroups%3Fsort%3D-name%26other%3D1'
);
});

0 comments on commit e7b466a

Please sign in to comment.