remove Private from timefilter lib (#19651)
This commit is contained in:
parent
88bb0121d4
commit
5b05c4cbc5
|
@ -1,100 +0,0 @@
|
|||
/*
|
||||
* 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 sinon from 'sinon';
|
||||
import expect from 'expect.js';
|
||||
import ngMock from 'ng_mock';
|
||||
import { TimefilterLibDiffIntervalProvider } from '../lib/diff_interval';
|
||||
|
||||
describe('Timefilter service', function () {
|
||||
describe('Refresh interval diff watcher', function () {
|
||||
|
||||
let fn;
|
||||
let update;
|
||||
let fetch;
|
||||
let timefilter;
|
||||
|
||||
beforeEach(ngMock.module('kibana'));
|
||||
|
||||
beforeEach(ngMock.inject(function (Private) {
|
||||
update = sinon.spy();
|
||||
fetch = sinon.spy();
|
||||
timefilter = {
|
||||
refreshInterval: {
|
||||
pause: false,
|
||||
value: 0
|
||||
},
|
||||
emit: function (eventType) {
|
||||
if (eventType === 'update') update();
|
||||
if (eventType === 'fetch') fetch();
|
||||
}
|
||||
};
|
||||
|
||||
fn = Private(TimefilterLibDiffIntervalProvider)(timefilter);
|
||||
}));
|
||||
|
||||
it('not emit anything if nothing has changed', function () {
|
||||
timefilter.refreshInterval = { pause: false, value: 0 };
|
||||
fn();
|
||||
expect(update.called).to.be(false);
|
||||
expect(fetch.called).to.be(false);
|
||||
});
|
||||
|
||||
it('emit only an update when paused', function () {
|
||||
timefilter.refreshInterval = { pause: true, value: 5000 };
|
||||
fn();
|
||||
expect(update.called).to.be(true);
|
||||
expect(fetch.called).to.be(false);
|
||||
});
|
||||
|
||||
it('emit update, not fetch, when switching to value: 0', function () {
|
||||
timefilter.refreshInterval = { pause: false, value: 5000 };
|
||||
fn();
|
||||
expect(update.calledOnce).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(true);
|
||||
timefilter.refreshInterval = { pause: false, value: 0 };
|
||||
fn();
|
||||
expect(update.calledTwice).to.be(true);
|
||||
expect(fetch.calledTwice).to.be(false);
|
||||
});
|
||||
|
||||
it('should emit update, not fetch, when moving from unpaused to paused', function () {
|
||||
timefilter.refreshInterval = { pause: false, value: 5000 };
|
||||
fn();
|
||||
expect(update.calledOnce).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(true);
|
||||
timefilter.refreshInterval = { pause: true, value: 5000 };
|
||||
fn();
|
||||
expect(update.calledTwice).to.be(true);
|
||||
expect(fetch.calledTwice).to.be(false);
|
||||
});
|
||||
|
||||
it('should emit update and fetch when unpaused', function () {
|
||||
timefilter.refreshInterval = { pause: true, value: 5000 };
|
||||
fn();
|
||||
expect(update.calledOnce).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(false);
|
||||
timefilter.refreshInterval = { pause: false, value: 5000 };
|
||||
fn();
|
||||
expect(update.calledTwice).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(true);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
|
@ -1,66 +0,0 @@
|
|||
/*
|
||||
* 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 sinon from 'sinon';
|
||||
import expect from 'expect.js';
|
||||
import ngMock from 'ng_mock';
|
||||
import { TimefilterLibDiffTimeProvider } from '../lib/diff_time';
|
||||
|
||||
describe('Timefilter service', function () {
|
||||
describe('time diff watcher', function () {
|
||||
let fn;
|
||||
let update;
|
||||
let fetch;
|
||||
let timefilter;
|
||||
|
||||
beforeEach(ngMock.module('kibana'));
|
||||
|
||||
beforeEach(ngMock.inject(function (Private) {
|
||||
update = sinon.spy();
|
||||
fetch = sinon.spy();
|
||||
timefilter = {
|
||||
time: {
|
||||
from: 0,
|
||||
to: 1
|
||||
},
|
||||
emit: function (eventType) {
|
||||
if (eventType === 'update') update();
|
||||
if (eventType === 'fetch') fetch();
|
||||
}
|
||||
};
|
||||
|
||||
fn = Private(TimefilterLibDiffTimeProvider)(timefilter);
|
||||
}));
|
||||
|
||||
it('not emit anything if the time has not changed', function () {
|
||||
timefilter.time = { from: 0, to: 1 };
|
||||
fn();
|
||||
expect(update.called).to.be(false);
|
||||
expect(fetch.called).to.be(false);
|
||||
});
|
||||
|
||||
it('emit update and fetch if the time has changed', function () {
|
||||
timefilter.time = { from: 5, to: 10 };
|
||||
fn();
|
||||
expect(update.called).to.be(true);
|
||||
expect(fetch.called).to.be(true);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
|
@ -18,23 +18,19 @@
|
|||
*/
|
||||
|
||||
import _ from 'lodash';
|
||||
import { UtilsDiffTimePickerValsProvider } from '../../utils/diff_time_picker_vals';
|
||||
import { areTimePickerValsDifferent } from './diff_time_picker_vals';
|
||||
|
||||
export function TimefilterLibDiffIntervalProvider(Private) {
|
||||
const diff = Private(UtilsDiffTimePickerValsProvider);
|
||||
export function diffIntervalFactory(self) {
|
||||
let oldRefreshInterval = _.clone(self.refreshInterval);
|
||||
|
||||
return function (self) {
|
||||
let oldRefreshInterval = _.clone(self.refreshInterval);
|
||||
|
||||
return function () {
|
||||
if (diff(self.refreshInterval, oldRefreshInterval)) {
|
||||
self.emit('update');
|
||||
if (!self.refreshInterval.pause && self.refreshInterval.value !== 0) {
|
||||
self.emit('fetch');
|
||||
}
|
||||
return function () {
|
||||
if (areTimePickerValsDifferent(self.refreshInterval, oldRefreshInterval)) {
|
||||
self.emit('update');
|
||||
if (!self.refreshInterval.pause && self.refreshInterval.value !== 0) {
|
||||
self.emit('fetch');
|
||||
}
|
||||
}
|
||||
|
||||
oldRefreshInterval = _.clone(self.refreshInterval);
|
||||
};
|
||||
oldRefreshInterval = _.clone(self.refreshInterval);
|
||||
};
|
||||
}
|
||||
|
|
95
src/ui/public/timefilter/lib/diff_interval.test.js
Normal file
95
src/ui/public/timefilter/lib/diff_interval.test.js
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* 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 sinon from 'sinon';
|
||||
import expect from 'expect.js';
|
||||
import { diffIntervalFactory } from '../lib/diff_interval';
|
||||
|
||||
describe('Refresh interval diff watcher', () => {
|
||||
|
||||
let diffInterval;
|
||||
let update;
|
||||
let fetch;
|
||||
let timefilter;
|
||||
|
||||
beforeEach(() => {
|
||||
update = sinon.spy();
|
||||
fetch = sinon.spy();
|
||||
timefilter = {
|
||||
refreshInterval: {
|
||||
pause: false,
|
||||
value: 0
|
||||
},
|
||||
emit: (eventType) => {
|
||||
if (eventType === 'update') update();
|
||||
if (eventType === 'fetch') fetch();
|
||||
}
|
||||
};
|
||||
|
||||
diffInterval = diffIntervalFactory(timefilter);
|
||||
});
|
||||
|
||||
test('not emit anything if nothing has changed', () => {
|
||||
timefilter.refreshInterval = { pause: false, value: 0 };
|
||||
diffInterval();
|
||||
expect(update.called).to.be(false);
|
||||
expect(fetch.called).to.be(false);
|
||||
});
|
||||
|
||||
test('emit only an update when paused', () => {
|
||||
timefilter.refreshInterval = { pause: true, value: 5000 };
|
||||
diffInterval();
|
||||
expect(update.called).to.be(true);
|
||||
expect(fetch.called).to.be(false);
|
||||
});
|
||||
|
||||
test('emit update, not fetch, when switching to value: 0', () => {
|
||||
timefilter.refreshInterval = { pause: false, value: 5000 };
|
||||
diffInterval();
|
||||
expect(update.calledOnce).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(true);
|
||||
timefilter.refreshInterval = { pause: false, value: 0 };
|
||||
diffInterval();
|
||||
expect(update.calledTwice).to.be(true);
|
||||
expect(fetch.calledTwice).to.be(false);
|
||||
});
|
||||
|
||||
test('should emit update, not fetch, when moving from unpaused to paused', () => {
|
||||
timefilter.refreshInterval = { pause: false, value: 5000 };
|
||||
diffInterval();
|
||||
expect(update.calledOnce).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(true);
|
||||
timefilter.refreshInterval = { pause: true, value: 5000 };
|
||||
diffInterval();
|
||||
expect(update.calledTwice).to.be(true);
|
||||
expect(fetch.calledTwice).to.be(false);
|
||||
});
|
||||
|
||||
test('should emit update and fetch when unpaused', () => {
|
||||
timefilter.refreshInterval = { pause: true, value: 5000 };
|
||||
diffInterval();
|
||||
expect(update.calledOnce).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(false);
|
||||
timefilter.refreshInterval = { pause: false, value: 5000 };
|
||||
diffInterval();
|
||||
expect(update.calledTwice).to.be(true);
|
||||
expect(fetch.calledOnce).to.be(true);
|
||||
});
|
||||
|
||||
});
|
|
@ -18,21 +18,17 @@
|
|||
*/
|
||||
|
||||
import _ from 'lodash';
|
||||
import { UtilsDiffTimePickerValsProvider } from '../../utils/diff_time_picker_vals';
|
||||
import { areTimePickerValsDifferent } from './diff_time_picker_vals';
|
||||
import { timeHistory } from '../time_history';
|
||||
|
||||
export function TimefilterLibDiffTimeProvider(Private) {
|
||||
const diff = Private(UtilsDiffTimePickerValsProvider);
|
||||
|
||||
return function (self) {
|
||||
let oldTime = _.clone(self.time);
|
||||
return function () {
|
||||
if (diff(self.time, oldTime)) {
|
||||
timeHistory.add(self.time);
|
||||
self.emit('update');
|
||||
self.emit('fetch');
|
||||
}
|
||||
oldTime = _.clone(self.time);
|
||||
};
|
||||
export function diffTimeFactory(self) {
|
||||
let oldTime = _.clone(self.time);
|
||||
return function () {
|
||||
if (areTimePickerValsDifferent(self.time, oldTime)) {
|
||||
timeHistory.add(self.time);
|
||||
self.emit('update');
|
||||
self.emit('fetch');
|
||||
}
|
||||
oldTime = _.clone(self.time);
|
||||
};
|
||||
}
|
||||
|
|
61
src/ui/public/timefilter/lib/diff_time.test.js
Normal file
61
src/ui/public/timefilter/lib/diff_time.test.js
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* 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 sinon from 'sinon';
|
||||
import expect from 'expect.js';
|
||||
import { diffTimeFactory } from '../lib/diff_time';
|
||||
|
||||
describe('time diff watcher', () => {
|
||||
let diffTime;
|
||||
let update;
|
||||
let fetch;
|
||||
let timefilter;
|
||||
|
||||
beforeEach(() => {
|
||||
update = sinon.spy();
|
||||
fetch = sinon.spy();
|
||||
timefilter = {
|
||||
time: {
|
||||
from: 0,
|
||||
to: 1
|
||||
},
|
||||
emit: function (eventType) {
|
||||
if (eventType === 'update') update();
|
||||
if (eventType === 'fetch') fetch();
|
||||
}
|
||||
};
|
||||
|
||||
diffTime = diffTimeFactory(timefilter);
|
||||
});
|
||||
|
||||
test('not emit anything if the time has not changed', () => {
|
||||
timefilter.time = { from: 0, to: 1 };
|
||||
diffTime();
|
||||
expect(update.called).to.be(false);
|
||||
expect(fetch.called).to.be(false);
|
||||
});
|
||||
|
||||
test('emit update and fetch if the time has changed', () => {
|
||||
timefilter.time = { from: 5, to: 10 };
|
||||
diffTime();
|
||||
expect(update.called).to.be(true);
|
||||
expect(fetch.called).to.be(true);
|
||||
});
|
||||
|
||||
});
|
|
@ -18,28 +18,24 @@
|
|||
*/
|
||||
|
||||
import _ from 'lodash';
|
||||
import angular from 'angular';
|
||||
|
||||
export function UtilsDiffTimePickerValsProvider() {
|
||||
const valueOf = function (o) {
|
||||
if (o) return o.valueOf();
|
||||
};
|
||||
|
||||
const valueOf = function (o) {
|
||||
if (o) return o.valueOf();
|
||||
};
|
||||
|
||||
return function (rangeA, rangeB) {
|
||||
if (_.isObject(rangeA) && _.isObject(rangeB)) {
|
||||
if (
|
||||
valueOf(rangeA.to) !== valueOf(rangeB.to)
|
||||
|| valueOf(rangeA.from) !== valueOf(rangeB.from)
|
||||
|| valueOf(rangeA.value) !== valueOf(rangeB.value)
|
||||
|| valueOf(rangeA.pause) !== valueOf(rangeB.pause)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return !angular.equals(rangeA, rangeB);
|
||||
export function areTimePickerValsDifferent(rangeA, rangeB) {
|
||||
if (_.isObject(rangeA) && _.isObject(rangeB)) {
|
||||
if (
|
||||
valueOf(rangeA.to) !== valueOf(rangeB.to)
|
||||
|| valueOf(rangeA.from) !== valueOf(rangeB.from)
|
||||
|| valueOf(rangeA.value) !== valueOf(rangeB.value)
|
||||
|| valueOf(rangeA.pause) !== valueOf(rangeB.pause)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return !_.isEqual(rangeA, rangeB);
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
return false;
|
||||
}
|
|
@ -19,26 +19,19 @@
|
|||
|
||||
|
||||
import moment from 'moment';
|
||||
import ngMock from 'ng_mock';
|
||||
import expect from 'expect.js';
|
||||
import { UtilsDiffTimePickerValsProvider } from '../diff_time_picker_vals';
|
||||
import { areTimePickerValsDifferent } from './diff_time_picker_vals';
|
||||
|
||||
describe('Diff Time Picker Values', function () {
|
||||
let diffTimePickerValues;
|
||||
describe('Diff Time Picker Values', () => {
|
||||
|
||||
beforeEach(ngMock.module('kibana'));
|
||||
beforeEach(ngMock.inject(function (Private) {
|
||||
diffTimePickerValues = Private(UtilsDiffTimePickerValsProvider);
|
||||
}));
|
||||
|
||||
it('accepts two undefined values', function () {
|
||||
const diff = diffTimePickerValues(undefined, undefined);
|
||||
test('accepts two undefined values', () => {
|
||||
const diff = areTimePickerValsDifferent(undefined, undefined);
|
||||
expect(diff).to.be(false);
|
||||
});
|
||||
|
||||
describe('dateMath ranges', function () {
|
||||
it('knows a match', function () {
|
||||
const diff = diffTimePickerValues(
|
||||
describe('dateMath ranges', () => {
|
||||
test('knows a match', () => {
|
||||
const diff = areTimePickerValsDifferent(
|
||||
{
|
||||
to: 'now',
|
||||
from: 'now-7d'
|
||||
|
@ -51,8 +44,8 @@ describe('Diff Time Picker Values', function () {
|
|||
|
||||
expect(diff).to.be(false);
|
||||
});
|
||||
it('knows a difference', function () {
|
||||
const diff = diffTimePickerValues(
|
||||
test('knows a difference', () => {
|
||||
const diff = areTimePickerValsDifferent(
|
||||
{
|
||||
to: 'now',
|
||||
from: 'now-7d'
|
||||
|
@ -67,9 +60,9 @@ describe('Diff Time Picker Values', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('a dateMath range, and a moment range', function () {
|
||||
it('is always different', function () {
|
||||
const diff = diffTimePickerValues(
|
||||
describe('a dateMath range, and a moment range', () => {
|
||||
test('is always different', () => {
|
||||
const diff = areTimePickerValsDifferent(
|
||||
{
|
||||
to: moment(),
|
||||
from: moment()
|
||||
|
@ -84,12 +77,12 @@ describe('Diff Time Picker Values', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('moment ranges', function () {
|
||||
it('uses the time value of moments for comparison', function () {
|
||||
describe('moment ranges', () => {
|
||||
test('uses the time value of moments for comparison', () => {
|
||||
const to = moment();
|
||||
const from = moment().add(1, 'day');
|
||||
|
||||
const diff = diffTimePickerValues(
|
||||
const diff = areTimePickerValsDifferent(
|
||||
{
|
||||
to: to.clone(),
|
||||
from: from.clone()
|
||||
|
@ -103,12 +96,12 @@ describe('Diff Time Picker Values', function () {
|
|||
expect(diff).to.be(false);
|
||||
});
|
||||
|
||||
it('fails if any to or from is different', function () {
|
||||
test('fails if any to or from is different', () => {
|
||||
const to = moment();
|
||||
const from = moment().add(1, 'day');
|
||||
const from2 = moment().add(2, 'day');
|
||||
|
||||
const diff = diffTimePickerValues(
|
||||
const diff = areTimePickerValsDifferent(
|
||||
{
|
||||
to: to.clone(),
|
||||
from: from.clone()
|
||||
|
@ -123,8 +116,8 @@ describe('Diff Time Picker Values', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('does not fall apart with unusual values', function () {
|
||||
const diff = diffTimePickerValues({}, {});
|
||||
test('does not fall apart with unusual values', () => {
|
||||
const diff = areTimePickerValsDifferent({}, {});
|
||||
expect(diff).to.be(false);
|
||||
});
|
||||
});
|
|
@ -23,8 +23,8 @@ import { calculateBounds, getTime } from './get_time';
|
|||
import '../state_management/global_state';
|
||||
import '../config';
|
||||
import { EventsProvider } from '../events';
|
||||
import { TimefilterLibDiffTimeProvider } from './lib/diff_time';
|
||||
import { TimefilterLibDiffIntervalProvider } from './lib/diff_interval';
|
||||
import { diffTimeFactory } from './lib/diff_time';
|
||||
import { diffIntervalFactory } from './lib/diff_interval';
|
||||
import uiRoutes from '../routes';
|
||||
import { uiModules } from '../modules';
|
||||
import { createLegacyClass } from '../utils/legacy_class';
|
||||
|
@ -49,8 +49,8 @@ uiModules
|
|||
Timefilter.Super.call(this);
|
||||
|
||||
const self = this;
|
||||
const diffTime = Private(TimefilterLibDiffTimeProvider)(self);
|
||||
const diffInterval = Private(TimefilterLibDiffIntervalProvider)(self);
|
||||
const diffTime = diffTimeFactory(self);
|
||||
const diffInterval = diffIntervalFactory(self);
|
||||
|
||||
self.isTimeRangeSelectorEnabled = false;
|
||||
self.isAutoRefreshSelectorEnabled = false;
|
||||
|
|
Loading…
Reference in a new issue