From bc6d5704b3b29327d12f8d2dfc8e04e9228aad4a Mon Sep 17 00:00:00 2001 From: Caleb Cox Date: Mon, 13 May 2024 12:16:55 -0500 Subject: [PATCH] Add DataDog tests --- src/components/DataDog/DataDog.test.tsx | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/components/DataDog/DataDog.test.tsx diff --git a/src/components/DataDog/DataDog.test.tsx b/src/components/DataDog/DataDog.test.tsx new file mode 100644 index 0000000000..fb46415d48 --- /dev/null +++ b/src/components/DataDog/DataDog.test.tsx @@ -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 = () => ( + + + +); + +describe('DataDog', () => { + it('calls setDataDogUser with the user', () => { + render(); + + expect(setDataDogUser).toHaveBeenCalledWith({ + accountListId: 'account-list-1', + email: 'first.last@cru.org', + name: 'First Last', + userId: 'user-1', + }); + }); + + it('does not call setDataDogUser if there is no session', () => { + (useSession as jest.MockedFn).mockReturnValueOnce({ + data: null, + status: 'unauthenticated', + update: () => Promise.resolve(null), + }); + + render(); + + expect(setDataDogUser).not.toHaveBeenCalled(); + }); + + it('handles missing accountListId', () => { + render( + + + , + ); + + expect(setDataDogUser).toHaveBeenCalledWith({ + accountListId: null, + email: 'first.last@cru.org', + name: 'First Last', + userId: 'user-1', + }); + }); +});