From 9894355490e761feb25b3c80895af3d0007cad8e Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 8 May 2018 13:53:22 +0200 Subject: [PATCH] [ML] Move the hide() function outside the show() function's scope. (#18900) The mlChartTooltipService's hide() method was inside the show() method scope. If hide() was called before show() was called for the first time this would result in an error. This could have happened in angular directive's which called hide() on some cleanup action without a previous call to show(). This fix moves the hide() method outside the show() method's scope and adds a unit test to check for the correct structure of mlChartTooltipService. --- .../chart_tooltip/__tests__/chart_tooltip.js | 26 +++++++++++++++++++ .../components/chart_tooltip/chart_tooltip.js | 26 +++++++++---------- 2 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 x-pack/plugins/ml/public/components/chart_tooltip/__tests__/chart_tooltip.js diff --git a/x-pack/plugins/ml/public/components/chart_tooltip/__tests__/chart_tooltip.js b/x-pack/plugins/ml/public/components/chart_tooltip/__tests__/chart_tooltip.js new file mode 100644 index 000000000000..5f5c1c465947 --- /dev/null +++ b/x-pack/plugins/ml/public/components/chart_tooltip/__tests__/chart_tooltip.js @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import ngMock from 'ng_mock'; +import expect from 'expect.js'; + +describe('ML - mlChartTooltipService', () => { + let mlChartTooltipService; + + beforeEach(ngMock.module('kibana')); + beforeEach(() => { + ngMock.inject(function ($injector) { + mlChartTooltipService = $injector.get('mlChartTooltipService'); + }); + }); + + it('service API duck typing', () => { + expect(mlChartTooltipService).to.be.an('object'); + expect(mlChartTooltipService.show).to.be.a('function'); + expect(mlChartTooltipService.hide).to.be.a('function'); + }); + +}); diff --git a/x-pack/plugins/ml/public/components/chart_tooltip/chart_tooltip.js b/x-pack/plugins/ml/public/components/chart_tooltip/chart_tooltip.js index 1a0a66fb4efb..8f2861a0aad3 100644 --- a/x-pack/plugins/ml/public/components/chart_tooltip/chart_tooltip.js +++ b/x-pack/plugins/ml/public/components/chart_tooltip/chart_tooltip.js @@ -68,20 +68,20 @@ module.directive('mlChartTooltip', function (mlChartTooltipService) { 'display': 'block' }); } + }; - this.hide = function () { - if (this.element !== null) { - this.element.css({ - 'opacity': '0', - }); + this.hide = function () { + if (this.element !== null) { + this.element.css({ + 'opacity': '0', + }); - // after the fade out transition has finished, set the display to - // none so it doesn't block any mouse events underneath it. - this.fadeTimeout = $timeout(() => { - this.element.css('display', 'none'); - this.fadeTimeout = null; - }, FADE_TIMEOUT_MS); - } - }; + // after the fade out transition has finished, set the display to + // none so it doesn't block any mouse events underneath it. + this.fadeTimeout = $timeout(() => { + this.element.css('display', 'none'); + this.fadeTimeout = null; + }, FADE_TIMEOUT_MS); + } }; });