Skip to content

Commit

Permalink
feat: expose postHog capture function
Browse files Browse the repository at this point in the history
Refs: SHELL-244 (#505)
  • Loading branch information
CataldoMazzilli authored Sep 17, 2024
1 parent 94b6bc4 commit 20b512d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions api-extractor/carbonio-shell-ui.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { FC } from 'react';
import type { i18n } from 'i18next';
import type { LinkProps } from 'react-router-dom';
import type { ModalProps } from '@zextras/carbonio-design-system';
import type { PostHog } from 'posthog-js';
import { default as React_2 } from 'react';
import type { TFunction } from 'i18next';
import type { To } from 'history';
Expand Down Expand Up @@ -1468,6 +1469,8 @@ export type Tags = Record<string, Tag>;

// @public (undocumented)
interface Tracker {
// (undocumented)
capture: InstanceType<typeof PostHog>['capture'];
// (undocumented)
enableTracker: (enable: boolean) => void;
// (undocumented)
Expand Down
6 changes: 2 additions & 4 deletions src/boot/loader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ describe('Loader', () => {
const componentsRes = waitForResponse('get', '/static/iris/components.json');
const getInfoRes = waitForResponse('post', '/service/soap/GetInfoRequest');
const enableTrackerFn = jest.fn();
const resetFn = jest.fn();
server.use(
http.post(
'/service/soap/GetInfoRequest',
Expand All @@ -137,7 +136,7 @@ describe('Loader', () => {
);
jest
.spyOn(posthog, 'useTracker')
.mockReturnValue({ enableTracker: enableTrackerFn, reset: resetFn });
.mockReturnValue({ enableTracker: enableTrackerFn, reset: jest.fn(), capture: jest.fn() });
setup(
<span data-testid={'loader'}>
<Loader />
Expand Down Expand Up @@ -195,7 +194,6 @@ describe('Loader', () => {
const componentsRes = waitForResponse('get', '/static/iris/components.json');
const getInfoRes = waitForResponse('post', '/service/soap/GetInfoRequest');
const enableTrackerFn = jest.fn();
const resetFn = jest.fn();
server.use(
http.post(
'/service/soap/GetInfoRequest',
Expand All @@ -204,7 +202,7 @@ describe('Loader', () => {
);
jest
.spyOn(posthog, 'useTracker')
.mockReturnValue({ enableTracker: enableTrackerFn, reset: resetFn });
.mockReturnValue({ enableTracker: enableTrackerFn, reset: jest.fn(), capture: jest.fn() });
setup(
<span data-testid={'loader'}>
<Loader />
Expand Down
11 changes: 11 additions & 0 deletions src/boot/posthog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React from 'react';

import { act, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import type { CaptureOptions } from 'posthog-js';
import * as posthogJsReact from 'posthog-js/react';
import type * as PostHogReact from 'posthog-js/react';

Expand Down Expand Up @@ -57,6 +58,16 @@ describe('Posthog', () => {
expect(posthog.reset).toHaveBeenCalled();
});

it('should call capture ', () => {
const posthog = spyOnPosthog();
const { result } = renderHook(() => useTracker());
const eventName = 'event name';
const properties = { prop1: 'prop1value', prop2: 'prop2value' };
const options: CaptureOptions = { send_instantly: true };
result.current.capture(eventName, properties, options);
expect(posthog.capture).toHaveBeenCalledWith(eventName, properties, options);
});

it('should invoke posthog provider with trackers disabled by default', () => {
const mockProvider = jest.spyOn(posthogJsReact, 'PostHogProvider');
setup(<TrackerProvider></TrackerProvider>);
Expand Down
12 changes: 10 additions & 2 deletions src/boot/posthog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import React, { useCallback, useEffect, useMemo, useState } from 'react';

import type { PostHogConfig } from 'posthog-js';
import type { PostHog, PostHogConfig } from 'posthog-js';
import { PostHogProvider, usePostHog } from 'posthog-js/react';

import { useAccountStore } from '../store/account';
Expand Down Expand Up @@ -36,6 +36,7 @@ export const TrackerProvider = ({
interface Tracker {
enableTracker: (enable: boolean) => void;
reset: () => void;
capture: InstanceType<typeof PostHog>['capture'];
}

const hashToSHA256 = async (value: string): Promise<ArrayBuffer> => {
Expand Down Expand Up @@ -97,5 +98,12 @@ export const useTracker = (): Tracker => {
postHog.reset();
}, [postHog]);

return { enableTracker, reset };
const capture = useCallback<InstanceType<typeof PostHog>['capture']>(
(eventName, properties, options) => {
postHog.capture(eventName, properties, options);
},
[postHog]
);

return { enableTracker, reset, capture };
};
3 changes: 2 additions & 1 deletion src/tests/posthog-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const spyOnPosthog = (): Partial<ReturnType<(typeof PostHogReact)['usePos
disable_surveys: true
} as PostHogConfig,
reset: jest.fn(),
setPersonProperties: jest.fn()
setPersonProperties: jest.fn(),
capture: jest.fn()
} satisfies Partial<ReturnType<(typeof PostHogReact)['usePostHog']>>;
jest
.spyOn(posthogJsReact, 'usePostHog')
Expand Down

0 comments on commit 20b512d

Please sign in to comment.