-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #943 from CruGlobal/7894-datadog-all-account-list-ids
[MPDX-7894] Save all of the user's past account list ids in the DataDog session
- Loading branch information
Showing
11 changed files
with
246 additions
and
123 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
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', | ||
}); | ||
}); | ||
}); |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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, | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.