Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MPDX-7894] Save all of the user's past account list ids in the DataDog session #943

Merged
merged 9 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pages/logout.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import { styled } from '@mui/material/styles';
import { signOut } from 'next-auth/react';
import { useTranslation } from 'react-i18next';
import { clearDataDogUser } from 'src/hooks/useDataDog';
import useGetAppSettings from 'src/hooks/useGetAppSettings';
import { clearDataDogUser } from 'src/lib/dataDog';

Check warning on line 11 in pages/logout.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/logout.page.tsx#L11

Added line #L11 was not covered by tests
import { loadSession } from './api/utils/pagePropsHelpers';

const BoxWrapper = styled(Box)(({ theme }) => ({
Expand Down
59 changes: 59 additions & 0 deletions src/components/DataDog/DataDog.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { render } from '@testing-library/react';
import { useSession } from 'next-auth/react';
import TestRouter from '__tests__/util/TestRouter';
import { setDataDogUser } from 'src/lib/dataDog';
import DataDog from './DataDog';

jest.mock('src/lib/dataDog');

const accountListId = 'account-list-1';
const router = {
query: { accountListId },
isReady: true,
};

const TestComponent: React.FC = () => (
<TestRouter router={router}>
<DataDog />
</TestRouter>
);

describe('DataDog', () => {
it('calls setDataDogUser with the user', () => {
render(<TestComponent />);

expect(setDataDogUser).toHaveBeenCalledWith({
accountListId: 'account-list-1',
email: '[email protected]',
name: 'First Last',
userId: 'user-1',
});
});

it('does not call setDataDogUser if there is no session', () => {
(useSession as jest.MockedFn<typeof useSession>).mockReturnValueOnce({
data: null,
status: 'unauthenticated',
update: () => Promise.resolve(null),
});

render(<TestComponent />);

expect(setDataDogUser).not.toHaveBeenCalled();
});

it('handles missing accountListId', () => {
render(
<TestRouter router={{ query: {}, isReady: true }}>
<DataDog />
</TestRouter>,
);

expect(setDataDogUser).toHaveBeenCalledWith({
accountListId: null,
email: '[email protected]',
name: 'First Last',
userId: 'user-1',
});
});
});
4 changes: 2 additions & 2 deletions src/components/DataDog/DataDog.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRouter } from 'next/router';
import React, { useEffect } from 'react';
import { useSession } from 'next-auth/react';
import { setDataDogUser } from 'src/hooks/useDataDog';
import { setDataDogUser } from 'src/lib/dataDog';

const DataDog: React.FC = () => {
const { query } = useRouter();
Expand All @@ -11,7 +11,7 @@ const DataDog: React.FC = () => {
? Array.isArray(query.accountListId)
? query.accountListId[0]
: query.accountListId
: '';
: null;

const user = session?.user;
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { styled } from '@mui/material/styles';
import { signOut } from 'next-auth/react';
import { useTranslation } from 'react-i18next';
import { NextLinkComposed } from 'src/components/common/Links/NextLinkComposed';
import { clearDataDogUser } from 'src/hooks/useDataDog';
import { clearDataDogUser } from 'src/lib/dataDog';
import { useAccountListId } from '../../../../../../hooks/useAccountListId';
import theme from '../../../../../../theme';
import HandoffLink from '../../../../../HandoffLink';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { styled } from '@mui/material/styles';
import { signOut } from 'next-auth/react';
import { useSnackbar } from 'notistack';
import { useTranslation } from 'react-i18next';
import { clearDataDogUser } from 'src/hooks/useDataDog';
import { useRequiredSession } from 'src/hooks/useRequiredSession';
import { clearDataDogUser } from 'src/lib/dataDog';
import { useAccountListId } from '../../../../../../hooks/useAccountListId';
import theme from '../../../../../../theme';
import HandoffLink from '../../../../../HandoffLink';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import {
} from 'src/components/common/Modal/ActionButtons/ActionButtons';
import Modal from 'src/components/common/Modal/Modal';
import { Organization } from 'src/graphql/types.generated';
import { clearDataDogUser } from 'src/hooks/useDataDog';
import useGetAppSettings from 'src/hooks/useGetAppSettings';
import { clearDataDogUser } from 'src/lib/dataDog';
import { articles, showArticle } from 'src/lib/helpScout';
import theme from 'src/theme';
import { useOauthUrl } from '../../useOauthUrl';
Expand Down
61 changes: 0 additions & 61 deletions src/hooks/__tests__/useDataDog.test.ts

This file was deleted.

55 changes: 0 additions & 55 deletions src/hooks/useDataDog.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/apollo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { onError } from '@apollo/client/link/error';
import { LocalStorageWrapper, persistCache } from 'apollo3-cache-persist';
import { signOut } from 'next-auth/react';
import { clearDataDogUser } from 'src/hooks/useDataDog';
import { clearDataDogUser } from 'src/lib/dataDog';

Check warning on line 5 in src/lib/apollo/client.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/apollo/client.ts#L5

Added line #L5 was not covered by tests
import snackNotifications from '../../components/Snackbar/Snackbar';
import { dispatch } from '../analytics';
import { createCache } from './cache';
Expand Down
117 changes: 117 additions & 0 deletions src/lib/dataDog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import {
accountListIdsStorageKey,
clearDataDogUser,
isDataDogConfigured,
setDataDogUser,
} from './dataDog';

const setDataDogUserMock = {
userId: '123456',
accountListId: '1234-4567-8910-1112-1314',
name: 'Roger',
email: '[email protected]',
};

describe('dataDog', () => {
beforeEach(() => {
window.DD_RUM = {
setUser: jest.fn(),
clearUser: jest.fn(),
};
});

describe('when DataDog is not configured', () => {
it('isDataDogConfigured should return false', () => {
expect(isDataDogConfigured()).toEqual(false);
});

it('setDataDogUser should not call DD_RUM methods', () => {
setDataDogUser(setDataDogUserMock);
expect(window.DD_RUM.clearUser).not.toHaveBeenCalled();
expect(window.DD_RUM.setUser).not.toHaveBeenCalled();
});
});

describe('when DataDog is configured', () => {
beforeEach(() => {
process.env.DATADOG_CONFIGURED = 'true';
});

//#region Default Tests
it('isDataDogConfigured should return true', () => {
expect(isDataDogConfigured()).toEqual(true);
});

it('clearDataDogUser should clear the user', () => {
clearDataDogUser();
expect(window.DD_RUM.clearUser).toHaveBeenCalled();
});

it('setDataDogUser should set the new user', () => {
setDataDogUser(setDataDogUserMock);
expect(window.DD_RUM.setUser).toHaveBeenCalled();
});
});

describe('setDataDogUser', () => {
beforeEach(() => {
process.env.DATADOG_CONFIGURED = 'true';
});

it('adds new account list ids to the list', () => {
window.localStorage.setItem(accountListIdsStorageKey, 'previous');

setDataDogUser(setDataDogUserMock);
expect(window.DD_RUM.setUser).toHaveBeenCalledWith(
expect.objectContaining({
accountListIds: ['previous', setDataDogUserMock.accountListId],
}),
);
expect(window.localStorage.getItem(accountListIdsStorageKey)).toBe(
`previous,${setDataDogUserMock.accountListId}`,
);
});

it('does not add null account list ids to the list', () => {
window.localStorage.removeItem(accountListIdsStorageKey);

setDataDogUser({ ...setDataDogUserMock, accountListId: null });
expect(window.DD_RUM.setUser).toHaveBeenCalledWith(
expect.objectContaining({ accountListIds: [] }),
);
expect(window.localStorage.getItem(accountListIdsStorageKey)).toBeNull();
});

it('does not add duplicate account list ids to the list', () => {
window.localStorage.setItem(
accountListIdsStorageKey,
setDataDogUserMock.accountListId,
);

setDataDogUser(setDataDogUserMock);
expect(window.DD_RUM.setUser).toHaveBeenCalledWith(
expect.objectContaining({
accountListIds: [setDataDogUserMock.accountListId],
}),
);
expect(window.localStorage.getItem(accountListIdsStorageKey)).toBe(
setDataDogUserMock.accountListId,
);
});

it('resets the account list ids list after calling clearDataDogUser', () => {
window.localStorage.setItem(accountListIdsStorageKey, 'previous');
clearDataDogUser();

setDataDogUser(setDataDogUserMock);
expect(window.DD_RUM.setUser).toHaveBeenCalledWith(
expect.objectContaining({
accountListIds: [setDataDogUserMock.accountListId],
}),
);
expect(window.localStorage.getItem(accountListIdsStorageKey)).toBe(
setDataDogUserMock.accountListId,
);
});
});
});
Loading
Loading