Skip to content

Commit

Permalink
fix: Invite token tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui authored and paulschreiber committed Sep 5, 2023
1 parent 1bb1ef7 commit 4a89980
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/localization/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@
"addMemberships.unique": "One or more users are already collaborators of this story map.",
"approved_membership": "You have been added to “{{storyMapTitle}}”",
"approve_invite_success": "You have been added to “{{storyMapTitle}}”",
"approve_invite_error": "Failed to accept Story Map invite to “{{storyMapTitle}}”",
"approve_invite_error": "Failed to accept Story Map invite",
"invite_document_title": "Story Map Invite"
},
"site": {
Expand Down
18 changes: 5 additions & 13 deletions src/storyMap/components/StoryMapInvite.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ const StoryMapInvite = () => {
addMessage({
severity: 'error',
content: 'storyMap.approve_invite_error',
params: {
storyMapTitle: storyMap.title,
},
})
);
}
Expand All @@ -93,18 +90,13 @@ const StoryMapInvite = () => {
return <PageLoader />;
}

if (success) {
return null;
}

return (
<PageContainer>
<Alert severity={success ? 'success' : 'error'}>
{t(
success
? 'storyMap.approve_invite_success'
: 'storyMap.approve_invite_error',
{
storyMapTitle: storyMap.title,
}
)}
</Alert>
<Alert severity="error">{t('storyMap.approve_invite_error')}</Alert>
</PageContainer>
);
};
Expand Down
89 changes: 89 additions & 0 deletions src/storyMap/components/StoryMapInvite.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { render, screen, waitFor, within } from 'tests/utils';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { mockTerrasoAPIrequestGraphQL } from 'tests/apiUtils';

import StoryMapInvite from './StoryMapInvite';

// Generated token with https://jwt.io/
// {
// "membershipId": "membership-id-1",
// "pendingEmail": null,
// "sub": "user-id-1",
// "email": "[email protected]"
// }
const TOKEN =
'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJtZW1iZXJzaGlwSWQiOiJtZW1iZXJzaGlwLWlkLTEiLCJwZW5kaW5nRW1haWwiOm51bGwsInN1YiI6InVzZXItaWQtMSIsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSJ9.p_eLvIrnx7vuY_7SgOand9FRoFZM6Hwen9cbGjStwXLy8htHkNW_if2tjaDKjteDKLklvM5ro4WScQAqfTLJag';

jest.mock('terraso-client-shared/terrasoApi/api');

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: jest.fn(),
useSearchParams: jest.fn(),
}));

const setup = async initialState => {
await render(<StoryMapInvite />, initialState);
};

test('StoryMapInvite: Valid token', async () => {
const navigate = jest.fn();
useNavigate.mockReturnValue(navigate);

const searchParams = new URLSearchParams();
searchParams.set('token', TOKEN);
useSearchParams.mockReturnValue([searchParams]);

mockTerrasoAPIrequestGraphQL({
'mutation approveMembershipToken': Promise.resolve({
approveStoryMapMembershipToken: {
membership: {
id: 'membership-id-1',
},
storyMap: {
id: 'story-map-id-1',
title: 'Hello world',
storyMapId: 'story-map-id-1',
slug: 'hello-world',
},
},
}),
});

await setup();

await waitFor(() =>
expect(
within(screen.getByRole('alert')).getByText(
/You have been added to Hello world/i
)
).toBeInTheDocument()
);

expect(navigate.mock.calls[0]).toEqual([
'/tools/story-maps/story-map-id-1/hello-world/edit',
]);
});

test('StoryMapInvite: Invalid token', async () => {
const navigate = jest.fn();
useNavigate.mockReturnValue(navigate);

const searchParams = new URLSearchParams();
searchParams.set('token', TOKEN);
useSearchParams.mockReturnValue([searchParams]);

mockTerrasoAPIrequestGraphQL({
'mutation approveMembershipToken': Promise.reject('Error'),
});

await setup();

await waitFor(() =>
expect(
within(screen.getByRole('alert')).getByText(
/Failed to accept Story Map invite/i
)
).toBeInTheDocument()
);
});
2 changes: 0 additions & 2 deletions src/tests/apiUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as terrasoApi from 'terraso-client-shared/terrasoApi/api';

jest.mock('terraso-client-shared/terrasoApi/api');

export const mockTerrasoAPIrequestGraphQL = mockedResponses => {
terrasoApi.requestGraphQL.mockImplementation(query => {
const trimmedQuery = query.trim();
Expand Down

0 comments on commit 4a89980

Please sign in to comment.