From fc79c06fc0690945b0c31f726d093153f9ddcb7f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?=
Date: Wed, 29 Apr 2020 10:40:45 +0100
Subject: [PATCH] [Telemetry] Fix inconsistent search behaviour in Advanced
Settings (#64510)
---
...telemetry_management_section.test.tsx.snap | 492 ++++++++++++++++++
.../telemetry_management_section.test.tsx | 284 ++++++++++
.../telemetry_management_section.tsx | 4 +-
3 files changed, 779 insertions(+), 1 deletion(-)
create mode 100644 src/plugins/telemetry_management_section/public/components/__snapshots__/telemetry_management_section.test.tsx.snap
create mode 100644 src/plugins/telemetry_management_section/public/components/telemetry_management_section.test.tsx
diff --git a/src/plugins/telemetry_management_section/public/components/__snapshots__/telemetry_management_section.test.tsx.snap b/src/plugins/telemetry_management_section/public/components/__snapshots__/telemetry_management_section.test.tsx.snap
new file mode 100644
index 0000000000000..8c0117e5a7266
--- /dev/null
+++ b/src/plugins/telemetry_management_section/public/components/__snapshots__/telemetry_management_section.test.tsx.snap
@@ -0,0 +1,492 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`TelemetryManagementSectionComponent renders as expected 1`] = `
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ,
+ }
+ }
+ />
+
+ }
+ />
+
+
+
+
+
+ ,
+ }
+ }
+ />
+
+
+
+
+
+
+ ,
+ "displayName": "Provide usage statistics",
+ "name": "telemetry:enabled",
+ "type": "boolean",
+ "value": true,
+ }
+ }
+ toasts={null}
+ />
+
+
+
+`;
+
+exports[`TelemetryManagementSectionComponent renders null because allowChangingOptInStatus is false 1`] = `
+
+`;
+
+exports[`TelemetryManagementSectionComponent renders null because query does not match the SEARCH_TERMS 1`] = `
+
+`;
+
+exports[`TelemetryManagementSectionComponent test the wrapper (for coverage purposes) 1`] = `""`;
diff --git a/src/plugins/telemetry_management_section/public/components/telemetry_management_section.test.tsx b/src/plugins/telemetry_management_section/public/components/telemetry_management_section.test.tsx
new file mode 100644
index 0000000000000..d0c2bd13f802d
--- /dev/null
+++ b/src/plugins/telemetry_management_section/public/components/telemetry_management_section.test.tsx
@@ -0,0 +1,284 @@
+/*
+ * Licensed to Elasticsearch B.V. under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch B.V. licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React from 'react';
+import { mountWithIntl, shallowWithIntl } from 'test_utils/enzyme_helpers';
+import { TelemetryManagementSection } from './telemetry_management_section';
+import { TelemetryService } from '../../../telemetry/public/services';
+import { coreMock } from '../../../../core/public/mocks';
+import { telemetryManagementSectionWrapper } from './telemetry_management_section_wrapper';
+
+describe('TelemetryManagementSectionComponent', () => {
+ const coreStart = coreMock.createStart();
+ const coreSetup = coreMock.createSetup();
+
+ it('renders as expected', () => {
+ const onQueryMatchChange = jest.fn();
+ const telemetryService = new TelemetryService({
+ config: {
+ enabled: true,
+ url: '',
+ banner: true,
+ allowChangingOptInStatus: true,
+ optIn: true,
+ optInStatusUrl: '',
+ sendUsageFrom: 'browser',
+ },
+ reportOptInStatusChange: false,
+ notifications: coreStart.notifications,
+ http: coreSetup.http,
+ });
+
+ expect(
+ shallowWithIntl(
+
+ )
+ ).toMatchSnapshot();
+ });
+
+ it('renders null because query does not match the SEARCH_TERMS', () => {
+ const onQueryMatchChange = jest.fn();
+ const telemetryService = new TelemetryService({
+ config: {
+ enabled: true,
+ url: '',
+ banner: true,
+ allowChangingOptInStatus: true,
+ optIn: false,
+ optInStatusUrl: '',
+ sendUsageFrom: 'browser',
+ },
+ reportOptInStatusChange: false,
+ notifications: coreStart.notifications,
+ http: coreSetup.http,
+ });
+
+ const component = mountWithIntl(
+
+ );
+ try {
+ expect(
+ component.setProps({ ...component.props(), query: { text: 'asssdasdsad' } })
+ ).toMatchSnapshot();
+ expect(onQueryMatchChange).toHaveBeenCalledWith(false);
+ expect(onQueryMatchChange).toHaveBeenCalledTimes(1);
+ } finally {
+ component.unmount();
+ }
+ });
+
+ it('renders because query matches the SEARCH_TERMS', () => {
+ const onQueryMatchChange = jest.fn();
+ const telemetryService = new TelemetryService({
+ config: {
+ enabled: true,
+ url: '',
+ banner: true,
+ allowChangingOptInStatus: true,
+ optIn: false,
+ optInStatusUrl: '',
+ sendUsageFrom: 'browser',
+ },
+ reportOptInStatusChange: false,
+ notifications: coreStart.notifications,
+ http: coreSetup.http,
+ });
+
+ const component = mountWithIntl(
+
+ );
+ try {
+ expect(
+ component.setProps({ ...component.props(), query: { text: 'TeLEMetry' } }).html()
+ ).not.toBe(''); // Renders something.
+ // I can't check against snapshot because of https://github.com/facebook/jest/issues/8618
+ // expect(component).toMatchSnapshot();
+
+ // It should also render if there is no query at all.
+ expect(component.setProps({ ...component.props(), query: {} }).html()).not.toBe('');
+ expect(onQueryMatchChange).toHaveBeenCalledWith(true);
+
+ // Should only be called once because the second time does not change the result
+ expect(onQueryMatchChange).toHaveBeenCalledTimes(1);
+ } finally {
+ component.unmount();
+ }
+ });
+
+ it('renders null because allowChangingOptInStatus is false', () => {
+ const onQueryMatchChange = jest.fn();
+ const telemetryService = new TelemetryService({
+ config: {
+ enabled: true,
+ url: '',
+ banner: true,
+ allowChangingOptInStatus: false,
+ optIn: true,
+ optInStatusUrl: '',
+ sendUsageFrom: 'browser',
+ },
+ reportOptInStatusChange: false,
+ notifications: coreStart.notifications,
+ http: coreSetup.http,
+ });
+
+ const component = mountWithIntl(
+
+ );
+ try {
+ expect(component).toMatchSnapshot();
+ component.setProps({ ...component.props(), query: { text: 'TeLEMetry' } });
+ expect(onQueryMatchChange).toHaveBeenCalledWith(false);
+ } finally {
+ component.unmount();
+ }
+ });
+
+ it('shows the OptInExampleFlyout', () => {
+ const onQueryMatchChange = jest.fn();
+ const telemetryService = new TelemetryService({
+ config: {
+ enabled: true,
+ url: '',
+ banner: true,
+ allowChangingOptInStatus: true,
+ optIn: false,
+ optInStatusUrl: '',
+ sendUsageFrom: 'browser',
+ },
+ reportOptInStatusChange: false,
+ notifications: coreStart.notifications,
+ http: coreSetup.http,
+ });
+
+ const component = mountWithIntl(
+
+ );
+ try {
+ const toggleExampleComponent = component.find('p > EuiLink[onClick]');
+ const updatedView = toggleExampleComponent.simulate('click');
+ updatedView.find('OptInExampleFlyout');
+ updatedView.simulate('close');
+ } finally {
+ component.unmount();
+ }
+ });
+
+ it('toggles the OptIn button', async () => {
+ const onQueryMatchChange = jest.fn();
+ const telemetryService = new TelemetryService({
+ config: {
+ enabled: true,
+ url: '',
+ banner: true,
+ allowChangingOptInStatus: true,
+ optIn: false,
+ optInStatusUrl: '',
+ sendUsageFrom: 'browser',
+ },
+ reportOptInStatusChange: false,
+ notifications: coreStart.notifications,
+ http: coreSetup.http,
+ });
+
+ const component = mountWithIntl(
+
+ );
+ try {
+ const toggleOptInComponent = component.find('Field');
+ await expect(
+ toggleOptInComponent.prop('handleChange')()
+ ).resolves.toBe(true);
+ expect((component.state() as any).enabled).toBe(true);
+ await expect(
+ toggleOptInComponent.prop('handleChange')()
+ ).resolves.toBe(true);
+ expect((component.state() as any).enabled).toBe(false);
+ telemetryService.setOptIn = jest.fn().mockRejectedValue(Error('test-error'));
+ await expect(
+ toggleOptInComponent.prop('handleChange')()
+ ).rejects.toStrictEqual(Error('test-error'));
+ } finally {
+ component.unmount();
+ }
+ });
+
+ it('test the wrapper (for coverage purposes)', () => {
+ const onQueryMatchChange = jest.fn();
+ const telemetryService = new TelemetryService({
+ config: {
+ enabled: true,
+ url: '',
+ banner: true,
+ allowChangingOptInStatus: false,
+ optIn: false,
+ optInStatusUrl: '',
+ sendUsageFrom: 'browser',
+ },
+ reportOptInStatusChange: false,
+ notifications: coreStart.notifications,
+ http: coreSetup.http,
+ });
+ const Wrapper = telemetryManagementSectionWrapper(telemetryService);
+ expect(
+ shallowWithIntl(
+
+ ).html()
+ ).toMatchSnapshot();
+ });
+});
diff --git a/src/plugins/telemetry_management_section/public/components/telemetry_management_section.tsx b/src/plugins/telemetry_management_section/public/components/telemetry_management_section.tsx
index 26e075b666593..361c5ff719c54 100644
--- a/src/plugins/telemetry_management_section/public/components/telemetry_management_section.tsx
+++ b/src/plugins/telemetry_management_section/public/components/telemetry_management_section.tsx
@@ -69,7 +69,9 @@ export class TelemetryManagementSection extends Component {
const { query } = nextProps;
const searchTerm = (query.text || '').toLowerCase();
- const searchTermMatches = SEARCH_TERMS.some(term => term.indexOf(searchTerm) >= 0);
+ const searchTermMatches =
+ this.props.telemetryService.getCanChangeOptInStatus() &&
+ SEARCH_TERMS.some(term => term.indexOf(searchTerm) >= 0);
if (searchTermMatches !== this.state.queryMatches) {
this.setState(