2023-02-21 00:35:35 -08:00
|
|
|
import { PostHog } from 'posthog-node';
|
|
|
|
import { PostHogClient } from '@/posthog';
|
|
|
|
import config from '@/config';
|
|
|
|
|
|
|
|
jest.mock('posthog-node');
|
|
|
|
|
|
|
|
describe('PostHog', () => {
|
|
|
|
const instanceId = 'test-id';
|
|
|
|
const userId = 'distinct-id';
|
|
|
|
const apiKey = 'api-key';
|
|
|
|
const apiHost = 'api-host';
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
config.set('diagnostics.config.posthog.apiKey', apiKey);
|
|
|
|
config.set('diagnostics.config.posthog.apiHost', apiHost);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
config.set('diagnostics.enabled', true);
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inits PostHog correctly', async () => {
|
|
|
|
const ph = new PostHogClient();
|
|
|
|
await ph.init(instanceId);
|
|
|
|
|
2023-05-02 01:37:19 -07:00
|
|
|
expect(PostHog.prototype.constructor).toHaveBeenCalledWith(apiKey, { host: apiHost });
|
2023-02-21 00:35:35 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not initialize or track if diagnostics are not enabled', async () => {
|
|
|
|
config.set('diagnostics.enabled', false);
|
|
|
|
|
|
|
|
const ph = new PostHogClient();
|
|
|
|
await ph.init(instanceId);
|
|
|
|
|
|
|
|
ph.track({
|
|
|
|
userId: 'test',
|
|
|
|
event: 'test',
|
|
|
|
properties: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(PostHog.prototype.constructor).not.toHaveBeenCalled();
|
|
|
|
expect(PostHog.prototype.capture).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('captures PostHog events', async () => {
|
|
|
|
const event = 'test event';
|
|
|
|
const properties = {
|
|
|
|
user_id: 'test',
|
|
|
|
test: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const ph = new PostHogClient();
|
|
|
|
await ph.init(instanceId);
|
|
|
|
|
|
|
|
ph.track({
|
|
|
|
userId,
|
|
|
|
event,
|
|
|
|
properties,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(PostHog.prototype.capture).toHaveBeenCalledWith({
|
|
|
|
distinctId: userId,
|
|
|
|
event,
|
|
|
|
userId,
|
|
|
|
properties,
|
|
|
|
sendFeatureFlags: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets feature flags', async () => {
|
|
|
|
const createdAt = new Date();
|
|
|
|
const ph = new PostHogClient();
|
|
|
|
await ph.init(instanceId);
|
|
|
|
|
2023-05-23 17:01:45 -07:00
|
|
|
await ph.getFeatureFlags({
|
2023-02-21 00:35:35 -08:00
|
|
|
id: userId,
|
|
|
|
createdAt,
|
|
|
|
});
|
|
|
|
|
2023-05-02 01:37:19 -07:00
|
|
|
expect(PostHog.prototype.getAllFlags).toHaveBeenCalledWith(`${instanceId}#${userId}`, {
|
|
|
|
personProperties: {
|
|
|
|
created_at_timestamp: createdAt.getTime().toString(),
|
|
|
|
},
|
|
|
|
});
|
2023-02-21 00:35:35 -08:00
|
|
|
});
|
2023-05-02 01:37:19 -07:00
|
|
|
});
|