Avoid mutating global moment object (#17341)

* Clone the moment object before using it to generate reporting data
so that calling .utc() doesn't mutate global state.

* fix tests

* do less work on each digest cycle
This commit is contained in:
Matt Bargar 2018-03-23 11:25:44 -04:00 committed by GitHub
parent e5e025d732
commit abd9ee9848
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 10 deletions

View file

@ -2,16 +2,23 @@ import moment from 'moment';
import expect from 'expect.js';
import ngMock from 'ng_mock';
import $ from 'jquery';
import sinon from 'sinon';
describe('kbnGlobalTimepicker', function () {
const sandbox = sinon.sandbox.create();
let compile;
let scope;
beforeEach(() => {
ngMock.module('kibana');
ngMock.inject(($compile, $rootScope) => {
ngMock.inject(($compile, $rootScope, timefilter) => {
scope = $rootScope.$new();
compile = () => {
compile = (timefilterStubProperties = {}) => {
Object.keys(timefilterStubProperties).forEach((key) => {
sandbox.stub(timefilter, key, timefilterStubProperties[key]);
});
const $el = $('<kbn-global-timepicker></kbn-global-timepicker>');
$el.data('$kbnTopNavController', {}); // Mock the kbnTopNav
$compile($el)(scope);
@ -20,6 +27,11 @@ describe('kbnGlobalTimepicker', function () {
};
});
});
afterEach(() => {
sandbox.restore();
});
it('injects the timepicker into the DOM', () => {
const $el = compile();
expect($el.attr('data-test-subj')).to.be('globalTimepicker');
@ -32,13 +44,13 @@ describe('kbnGlobalTimepicker', function () {
min: moment(minString),
max: moment(maxString),
};
scope.timefilter = {
const timefilter = {
isAutoRefreshSelectorEnabled: true,
isTimeRangeSelectorEnabled: false,
getBounds: () => bounds
};
const $el = compile();
const $el = compile(timefilter);
expect($el.attr('data-shared-timefilter-from')).to.eql(minString);
expect($el.attr('data-shared-timefilter-to')).to.eql(maxString);
@ -51,13 +63,13 @@ describe('kbnGlobalTimepicker', function () {
min: moment(minString),
max: moment(maxString),
};
scope.timefilter = {
const timefilter = {
isAutoRefreshSelectorEnabled: false,
isTimeRangeSelectorEnabled: true,
getBounds: () => bounds
};
const $el = compile();
const $el = compile(timefilter);
expect($el.attr('data-shared-timefilter-from')).to.eql(minString);
expect($el.attr('data-shared-timefilter-to')).to.eql(maxString);
@ -70,13 +82,13 @@ describe('kbnGlobalTimepicker', function () {
min: moment(minString),
max: moment(maxString),
};
scope.timefilter = {
const timefilter = {
isAutoRefreshSelectorEnabled: false,
isTimeRangeSelectorEnabled: false,
getBounds: () => bounds
};
const $el = compile();
const $el = compile(timefilter);
expect($el.attr('data-shared-timefilter-from')).to.eql('');
expect($el.attr('data-shared-timefilter-to')).to.eql('');

View file

@ -1,7 +1,7 @@
<div
ng-show="timefilter.isAutoRefreshSelectorEnabled || timefilter.isTimeRangeSelectorEnabled"
data-shared-timefilter-from="{{timefilter.isAutoRefreshSelectorEnabled || timefilter.isTimeRangeSelectorEnabled ? timefilter.getBounds().min.utc().format() : null }}"
data-shared-timefilter-to="{{timefilter.isAutoRefreshSelectorEnabled || timefilter.isTimeRangeSelectorEnabled ? timefilter.getBounds().max.utc().format() : null }}"
data-shared-timefilter-from="{{ getSharedTimeFilterFromDate() }}"
data-shared-timefilter-to="{{ getSharedTimeFilterToDate() }}"
class="kuiLocalMenu"
data-test-subj="globalTimepicker"
>

View file

@ -45,6 +45,18 @@ uiModules
timefilter.refreshInterval = interval;
kbnTopNav.close('interval');
};
$scope.getSharedTimeFilterFromDate = function () {
return (timefilter.isAutoRefreshSelectorEnabled || timefilter.isTimeRangeSelectorEnabled)
? timefilter.getBounds().min.clone().utc().format()
: null;
};
$scope.getSharedTimeFilterToDate = function () {
return (timefilter.isAutoRefreshSelectorEnabled || timefilter.isTimeRangeSelectorEnabled)
? timefilter.getBounds().max.clone().utc().format()
: null;
};
},
};
});