Skip to content

Commit

Permalink
Add DataDog tests
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed May 13, 2024
1 parent 44c17b9 commit bc6d570
Showing 1 changed file with 59 additions and 0 deletions.
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',
});
});
});

0 comments on commit bc6d570

Please sign in to comment.