-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added referrer to account profile
- Loading branch information
Showing
7 changed files
with
195 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -19,7 +19,7 @@ import React from 'react'; | |
import { when } from 'jest-when'; | ||
import _ from 'lodash/fp'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { useNavigate, useParams } from 'react-router-dom'; | ||
import { useNavigate, useParams, useSearchParams } from 'react-router-dom'; | ||
import * as terrasoApi from 'terraso-client-shared/terrasoApi/api'; | ||
|
||
import AccountProfile from 'account/components/AccountProfile'; | ||
|
@@ -30,6 +30,7 @@ jest.mock('react-router-dom', () => ({ | |
...jest.requireActual('react-router-dom'), | ||
useParams: jest.fn(), | ||
useNavigate: jest.fn(), | ||
useSearchParams: jest.fn(), | ||
})); | ||
|
||
const setup = async ( | ||
|
@@ -63,6 +64,7 @@ const setup = async ( | |
beforeEach(() => { | ||
useNavigate.mockReturnValue(jest.fn()); | ||
useParams.mockReturnValue({}); | ||
useSearchParams.mockReturnValue([new URLSearchParams(), () => {}]); | ||
}); | ||
|
||
test('AccountProfile: Display Avatar', async () => { | ||
|
@@ -438,5 +440,67 @@ test('AccountProfile: Complete profile', async () => { | |
await act(async () => | ||
fireEvent.click(screen.getByRole('button', { name: 'Save Profile' })) | ||
); | ||
expect(navigate).toHaveBeenCalledWith('/account/profile'); | ||
expect(navigate).toHaveBeenCalledWith('/account/profile', { replace: true }); | ||
}); | ||
|
||
test('AccountProfile: Navigate to referrer after complete profile', async () => { | ||
const navigate = jest.fn(); | ||
useNavigate.mockReturnValue(navigate); | ||
const searchParams = new URLSearchParams(); | ||
const referrer = encodeURIComponent('groups?sort=-name&other=1'); | ||
searchParams.set('referrer', referrer); | ||
useSearchParams.mockReturnValue([searchParams]); | ||
useParams.mockReturnValue({ | ||
completeProfile: 'completeProfile', | ||
}); | ||
|
||
when(terrasoApi.requestGraphQL) | ||
.calledWith(expect.stringContaining('query userProfile'), expect.anything()) | ||
.mockReturnValue( | ||
Promise.resolve( | ||
_.set( | ||
'users.edges[0].node', | ||
{ | ||
id: 'user-id', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
email: '[email protected]', | ||
profileImage: '', | ||
preferences: { edges: [] }, | ||
}, | ||
{} | ||
) | ||
) | ||
); | ||
|
||
when(terrasoApi.requestGraphQL) | ||
.calledWith( | ||
expect.stringContaining('mutation updateUser'), | ||
expect.anything() | ||
) | ||
.mockResolvedValue( | ||
_.set( | ||
'updateUser.user', | ||
{ | ||
id: '1', | ||
firstName: 'Pablo', | ||
lastName: 'Perez', | ||
email: '[email protected]', | ||
profileImage: 'https://www.group.org/image.jpg', | ||
preferences: { | ||
edges: [{ node: { key: 'language', value: 'es-ES' } }], | ||
}, | ||
}, | ||
{} | ||
) | ||
); | ||
|
||
await setup(); | ||
|
||
await act(async () => | ||
fireEvent.click(screen.getByRole('button', { name: 'Save Profile' })) | ||
); | ||
expect(navigate).toHaveBeenCalledWith('groups?sort=-name&other=1', { | ||
replace: true, | ||
}); | ||
}); |
This file contains 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 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 |
---|---|---|
|
@@ -17,7 +17,12 @@ | |
import { render, screen } from 'tests/utils'; | ||
import React from 'react'; | ||
import _ from 'lodash/fp'; | ||
import { useLocation, useNavigate, useParams } from 'react-router-dom'; | ||
import { | ||
useLocation, | ||
useNavigate, | ||
useParams, | ||
useSearchParams, | ||
} from 'react-router-dom'; | ||
import { getToken, getUserEmail } from 'terraso-client-shared/account/auth'; | ||
import * as terrasoApi from 'terraso-client-shared/terrasoApi/api'; | ||
|
||
|
@@ -37,6 +42,7 @@ jest.mock('react-router-dom', () => ({ | |
useParams: jest.fn(), | ||
useLocation: jest.fn(), | ||
useNavigate: jest.fn(), | ||
useSearchParams: jest.fn(), | ||
Navigate: props => <div>To: {props.to}</div>, | ||
})); | ||
|
||
|
@@ -47,6 +53,7 @@ const IS_FIRST_LOGIN_TOKEN = | |
beforeEach(() => { | ||
global.fetch = jest.fn(); | ||
useNavigate.mockReturnValue(jest.fn()); | ||
useSearchParams.mockReturnValue([new URLSearchParams(), () => {}]); | ||
}); | ||
|
||
test('Auth: test redirect', async () => { | ||
|
@@ -216,6 +223,35 @@ test('Auth: Test redirect complete profile', async () => { | |
expect(navigate).toHaveBeenCalledWith('/account/profile/completeProfile'); | ||
}); | ||
|
||
test('Auth: Test redirect to profile with referrer', async () => { | ||
useLocation.mockReturnValue({ | ||
pathname: REDIRECT_PATHNAME, | ||
search: REDIRECT_SEARCH, | ||
}); | ||
|
||
const navigate = jest.fn(); | ||
useNavigate.mockReturnValue(navigate); | ||
getToken.mockResolvedValue(IS_FIRST_LOGIN_TOKEN); | ||
await render( | ||
<RequireAuth> | ||
<div /> | ||
</RequireAuth>, | ||
{ | ||
account: { | ||
currentUser: { | ||
data: { | ||
email: '[email protected]', | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
expect(navigate).toHaveBeenCalledWith( | ||
'/account/profile/completeProfile?referrer=%2Fgroups%3Fsort%3D-name%26other%3D1' | ||
); | ||
}); | ||
|
||
test('Auth: Avoid redirect if profile complete already displayed for user', async () => { | ||
const navigate = jest.fn(); | ||
useNavigate.mockReturnValue(navigate); | ||
|
Oops, something went wrong.