Skip to content

Commit

Permalink
Fix failing renderApp test
Browse files Browse the repository at this point in the history
- by switching sendTelemetry to async/await syntax instead of promise-based syntax
- since the code was expecting Promise back but the http test mock was sending a generic jest.fn

+ update sendTelemetry tests to use httpServiceMock, now that we're no longer requiring returned promises
  • Loading branch information
cee-chen committed Apr 28, 2020
1 parent 020ab86 commit cb9708f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
import React from 'react';
import { mount } from 'enzyme';

import { sendTelemetry, SendAppSearchTelemetry } from './';
import { httpServiceMock } from 'src/core/public/mocks';
import { mountWithKibanaContext } from '../../test_utils/helpers';
import { sendTelemetry, SendAppSearchTelemetry } from './';

describe('Shared Telemetry Helpers', () => {
const httpMock = httpServiceMock.createSetupContract();

beforeEach(() => {
jest.clearAllMocks();
});

describe('sendTelemetry', () => {
it('successfully calls the server-side telemetry endpoint', () => {
const httpMock = { put: jest.fn(() => Promise.resolve()) };

sendTelemetry({
http: httpMock,
product: 'enterprise_search',
Expand All @@ -29,19 +34,13 @@ describe('Shared Telemetry Helpers', () => {
});

it('throws an error if the telemetry endpoint fails', () => {
const httpMock = { put: jest.fn(() => Promise.reject()) };
const httpRejectMock = { put: () => Promise.reject() };

expect(sendTelemetry({ http: httpMock })).rejects.toThrow('Unable to send telemetry');
expect(sendTelemetry({ http: httpRejectMock })).rejects.toThrow('Unable to send telemetry');
});
});

describe('React component helpers', () => {
const httpMock = { put: jest.fn(() => Promise.resolve()) };

beforeEach(() => {
jest.clearAllMocks();
});

it('SendAppSearchTelemetry component', () => {
const wrapper = mountWithKibanaContext(
<SendAppSearchTelemetry action="clicked" metric="button" />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ interface ISendTelemetry extends ISendTelemetryProps {
* Base function - useful for non-component actions, e.g. clicks
*/

export const sendTelemetry = ({ http, product, action, metric }: ISendTelemetry) => {
return http
.put(`/api/${product}/telemetry`, {
export const sendTelemetry = async ({ http, product, action, metric }: ISendTelemetry) => {
try {
await http.put(`/api/${product}/telemetry`, {
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ action, metric }),
})
.catch(error => {
throw new Error('Unable to send telemetry');
});
} catch (error) {
throw new Error('Unable to send telemetry');
}
};

/**
Expand Down

0 comments on commit cb9708f

Please sign in to comment.