Skip to content

Commit

Permalink
test(session-replay-browser): add expectation for send beacon
Browse files Browse the repository at this point in the history
  • Loading branch information
lewgordon-amplitude committed Sep 9, 2024
1 parent c82fbb1 commit 3db05b4
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/session-replay-browser/test/hooks/beacon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ type TestEvent = {
jest.mock('@amplitude/analytics-client-common');

describe('beacon', () => {
const mockGlobalScope = (globalScope?: Partial<typeof globalThis>) => {
jest.spyOn(AnalyticsClientCommon, 'getGlobalScope').mockReturnValue(globalScope as typeof globalThis);
const mockGlobalScope = (globalScope?: Partial<typeof globalThis>): typeof globalThis => {
const mockedGlobalScope = jest.spyOn(AnalyticsClientCommon, 'getGlobalScope');
mockedGlobalScope.mockReturnValue(globalScope as typeof globalThis);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return AnalyticsClientCommon.getGlobalScope()!;
};

describe('BeaconTransport', () => {
Expand Down Expand Up @@ -55,7 +58,7 @@ describe('beacon', () => {
describe('#send', () => {
test('sends with beacon', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
mockGlobalScope({
const mockedGlobalScope = mockGlobalScope({
navigator: {
sendBeacon: jest.fn().mockImplementation(() => true),
},
Expand All @@ -67,13 +70,20 @@ describe('beacon', () => {
sessionId,
type: 'interaction',
},
{} as SessionReplayJoinedConfig,
{
apiKey,
} as SessionReplayJoinedConfig,
);
transport.send(deviceId, {
Field1: 'foo',
Field2: 1234,
});
expect(xmlMockFns.open).not.toHaveBeenCalled();
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(mockedGlobalScope.navigator.sendBeacon).toHaveBeenCalledWith(
`https://api-sr.amplitude.com/sessions/v2/track?device_id=${deviceId}&session_id=${sessionId}&type=interaction&api_key=${apiKey}`,
JSON.stringify({ Field1: 'foo', Field2: 1234 }),
);
});
test('falls back to xhr', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Expand Down

0 comments on commit 3db05b4

Please sign in to comment.