Skip to content

Commit

Permalink
Cleanup: Remove old sendTelemetry fn
Browse files Browse the repository at this point in the history
+ update tests
  • Loading branch information
cee-chen committed Nov 2, 2020
1 parent 4a71269 commit 580481e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

export { TelemetryLogic } from './telemetry_logic';
export { sendTelemetry } from './send_telemetry';
export {
SendEnterpriseSearchTelemetry,
SendAppSearchTelemetry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,50 @@
* you may not use this file except in compliance with the Elastic License.
*/

import '../../__mocks__/kea.mock';
import '../../__mocks__/shallow_useeffect.mock';
import { mockHttpValues } from '../../__mocks__';
import { mockTelemetryActions } from '../../__mocks__';

import React from 'react';
import { shallow } from 'enzyme';

import { JSON_HEADER as headers } from '../../../../common/constants';

import {
sendTelemetry,
SendEnterpriseSearchTelemetry,
SendAppSearchTelemetry,
SendWorkplaceSearchTelemetry,
} from './';

describe('Shared Telemetry Helpers', () => {
describe('Telemetry component helpers', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('sendTelemetry', () => {
it('successfully calls the server-side telemetry endpoint', () => {
sendTelemetry({
http: mockHttpValues.http,
product: 'enterprise_search',
action: 'viewed',
metric: 'setup_guide',
});

expect(mockHttpValues.http.put).toHaveBeenCalledWith('/api/enterprise_search/stats', {
headers,
body: '{"product":"enterprise_search","action":"viewed","metric":"setup_guide"}',
});
});

it('throws an error if the telemetry endpoint fails', () => {
const httpRejectMock = sendTelemetry({
http: { put: () => Promise.reject() },
} as any);
it('SendEnterpriseSearchTelemetry', () => {
shallow(<SendEnterpriseSearchTelemetry action="viewed" metric="page" />);

expect(httpRejectMock).rejects.toThrow('Unable to send telemetry');
expect(mockTelemetryActions.sendTelemetry).toHaveBeenCalledWith({
action: 'viewed',
metric: 'page',
product: 'enterprise_search',
});
});

describe('React component helpers', () => {
it('SendEnterpriseSearchTelemetry component', () => {
shallow(<SendEnterpriseSearchTelemetry action="viewed" metric="page" />);
it('SendAppSearchTelemetry', () => {
shallow(<SendAppSearchTelemetry action="clicked" metric="button" />);

expect(mockHttpValues.http.put).toHaveBeenCalledWith('/api/enterprise_search/stats', {
headers,
body: '{"product":"enterprise_search","action":"viewed","metric":"page"}',
});
});

it('SendAppSearchTelemetry component', () => {
shallow(<SendAppSearchTelemetry action="clicked" metric="button" />);

expect(mockHttpValues.http.put).toHaveBeenCalledWith('/api/enterprise_search/stats', {
headers,
body: '{"product":"app_search","action":"clicked","metric":"button"}',
});
expect(mockTelemetryActions.sendTelemetry).toHaveBeenCalledWith({
action: 'clicked',
metric: 'button',
product: 'app_search',
});
});

it('SendWorkplaceSearchTelemetry component', () => {
shallow(<SendWorkplaceSearchTelemetry action="error" metric="not_found" />);
it('SendWorkplaceSearchTelemetry', () => {
shallow(<SendWorkplaceSearchTelemetry action="error" metric="not_found" />);

expect(mockHttpValues.http.put).toHaveBeenCalledWith('/api/enterprise_search/stats', {
headers,
body: '{"product":"workplace_search","action":"error","metric":"not_found"}',
});
expect(mockTelemetryActions.sendTelemetry).toHaveBeenCalledWith({
action: 'error',
metric: 'not_found',
product: 'workplace_search',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,40 @@
*/

import React, { useEffect } from 'react';
import { useValues } from 'kea';
import { useActions } from 'kea';

import { HttpSetup } from 'src/core/public';
import { JSON_HEADER as headers } from '../../../../common/constants';
import { HttpLogic } from '../http';

interface ISendTelemetryProps {
action: 'viewed' | 'error' | 'clicked';
metric: string; // e.g., 'setup_guide'
}

interface ISendTelemetry extends ISendTelemetryProps {
http: HttpSetup;
product: 'app_search' | 'workplace_search' | 'enterprise_search';
}

/**
* Base function - useful for non-component actions, e.g. clicks
*/

export const sendTelemetry = async ({ http, product, action, metric }: ISendTelemetry) => {
try {
const body = JSON.stringify({ product, action, metric });
await http.put('/api/enterprise_search/stats', { headers, body });
} catch (error) {
throw new Error('Unable to send telemetry');
}
};
import { TelemetryLogic, TSendTelemetry } from './telemetry_logic';

/**
* React component helpers - useful for on-page-load/views
*/

export const SendEnterpriseSearchTelemetry: React.FC<ISendTelemetryProps> = ({
action,
metric,
}) => {
const { http } = useValues(HttpLogic);
export const SendEnterpriseSearchTelemetry: React.FC<TSendTelemetry> = ({ action, metric }) => {
const { sendTelemetry } = useActions(TelemetryLogic);

useEffect(() => {
sendTelemetry({ http, action, metric, product: 'enterprise_search' });
}, [action, metric, http]);
sendTelemetry({ action, metric, product: 'enterprise_search' });
}, [action, metric]);

return null;
};

export const SendAppSearchTelemetry: React.FC<ISendTelemetryProps> = ({ action, metric }) => {
const { http } = useValues(HttpLogic);
export const SendAppSearchTelemetry: React.FC<TSendTelemetry> = ({ action, metric }) => {
const { sendTelemetry } = useActions(TelemetryLogic);

useEffect(() => {
sendTelemetry({ http, action, metric, product: 'app_search' });
}, [action, metric, http]);
sendTelemetry({ action, metric, product: 'app_search' });
}, [action, metric]);

return null;
};

export const SendWorkplaceSearchTelemetry: React.FC<ISendTelemetryProps> = ({ action, metric }) => {
const { http } = useValues(HttpLogic);
export const SendWorkplaceSearchTelemetry: React.FC<TSendTelemetry> = ({ action, metric }) => {
const { sendTelemetry } = useActions(TelemetryLogic);

useEffect(() => {
sendTelemetry({ http, action, metric, product: 'workplace_search' });
}, [action, metric, http]);
sendTelemetry({ action, metric, product: 'workplace_search' });
}, [action, metric]);

return null;
};

0 comments on commit 580481e

Please sign in to comment.