Skip to content

Commit

Permalink
Convert tests to jest in vis_type_timeseries/public & common folders (#…
Browse files Browse the repository at this point in the history
…55023)

* Convert tests to jest in vis_type_timeseries/public & common folders

* Remove unused translation
  • Loading branch information
sulemanof authored Jan 17, 2020
1 parent d740ec3 commit b9161e7
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 209 deletions.

This file was deleted.

22 changes: 0 additions & 22 deletions src/legacy/core_plugins/vis_type_timeseries/common/agg_lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,3 @@ const byType = {
export function isBasicAgg(item) {
return _.includes(Object.keys(byType.basic), item.type);
}

export function createOptions(type = '_all', siblings = []) {
let aggs = byType[type];
if (!aggs) aggs = byType._all;
let enablePipelines = siblings.some(isBasicAgg);
if (siblings.length <= 1) enablePipelines = false;
return _(aggs)
.map((label, value) => {
const disabled = _.includes(pipeline, value) ? !enablePipelines : false;
return {
label: disabled
? i18n.translate('visTypeTimeseries.aggLookup.addPipelineAggDescription', {
defaultMessage: '{label} (use the "+" button to add this pipeline agg)',
values: { label },
})
: label,
value,
disabled,
};
})
.value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { isBasicAgg } from './agg_lookup';

describe('aggLookup', () => {
describe('isBasicAgg(metric)', () => {
test('returns true for a basic metric (count)', () => {
expect(isBasicAgg({ type: 'count' })).toEqual(true);
});
test('returns false for a pipeline metric (derivative)', () => {
expect(isBasicAgg({ type: 'derivative' })).toEqual(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,67 @@
* under the License.
*/

import { expect } from 'chai';
import { calculateLabel } from '../calculate_label';
import { calculateLabel } from './calculate_label';

describe('calculateLabel(metric, metrics)', () => {
it('returns "Unknown" for empty metric', () => {
expect(calculateLabel()).to.equal('Unknown');
test('returns "Unknown" for empty metric', () => {
expect(calculateLabel()).toEqual('Unknown');
});

it('returns the metric.alias if set', () => {
expect(calculateLabel({ alias: 'Example' })).to.equal('Example');
test('returns the metric.alias if set', () => {
expect(calculateLabel({ alias: 'Example' })).toEqual('Example');
});

it('returns "Count" for a count metric', () => {
expect(calculateLabel({ type: 'count' })).to.equal('Count');
test('returns "Count" for a count metric', () => {
expect(calculateLabel({ type: 'count' })).toEqual('Count');
});

it('returns "Calculation" for a bucket script metric', () => {
expect(calculateLabel({ type: 'calculation' })).to.equal('Bucket Script');
test('returns "Calculation" for a bucket script metric', () => {
expect(calculateLabel({ type: 'calculation' })).toEqual('Bucket Script');
});

it('returns formated label for series_agg', () => {
test('returns formated label for series_agg', () => {
const label = calculateLabel({ type: 'series_agg', function: 'max' });
expect(label).to.equal('Series Agg (max)');
expect(label).toEqual('Series Agg (max)');
});

it('returns formated label for basic aggs', () => {
test('returns formated label for basic aggs', () => {
const label = calculateLabel({ type: 'avg', field: 'memory' });
expect(label).to.equal('Average of memory');
expect(label).toEqual('Average of memory');
});

it('returns formated label for pipeline aggs', () => {
test('returns formated label for pipeline aggs', () => {
const metric = { id: 2, type: 'derivative', field: 1 };
const metrics = [{ id: 1, type: 'max', field: 'network.out.bytes' }, metric];
const label = calculateLabel(metric, metrics);
expect(label).to.equal('Derivative of Max of network.out.bytes');
expect(label).toEqual('Derivative of Max of network.out.bytes');
});

it('returns formated label for derivative of percentile', () => {
test('returns formated label for derivative of percentile', () => {
const metric = { id: 2, type: 'derivative', field: '1[50.0]' };
const metrics = [{ id: 1, type: 'percentile', field: 'network.out.bytes' }, metric];
const label = calculateLabel(metric, metrics);
expect(label).to.equal('Derivative of Percentile of network.out.bytes (50.0)');
expect(label).toEqual('Derivative of Percentile of network.out.bytes (50.0)');
});

it('returns formated label for pipeline aggs (deep)', () => {
test('returns formated label for pipeline aggs (deep)', () => {
const metric = { id: 3, type: 'derivative', field: 2 };
const metrics = [
{ id: 1, type: 'max', field: 'network.out.bytes' },
{ id: 2, type: 'moving_average', field: 1 },
metric,
];
const label = calculateLabel(metric, metrics);
expect(label).to.equal('Derivative of Moving Average of Max of network.out.bytes');
expect(label).toEqual('Derivative of Moving Average of Max of network.out.bytes');
});

it('returns formated label for pipeline aggs uses alias for field metric', () => {
test('returns formated label for pipeline aggs uses alias for field metric', () => {
const metric = { id: 2, type: 'derivative', field: 1 };
const metrics = [
{ id: 1, type: 'max', field: 'network.out.bytes', alias: 'Outbound Traffic' },
metric,
];
const label = calculateLabel(metric, metrics);
expect(label).to.equal('Derivative of Outbound Traffic');
expect(label).toEqual('Derivative of Outbound Traffic');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
* under the License.
*/

import { calculateSiblings } from '../calculate_siblings';
import { expect } from 'chai';
import { calculateSiblings } from './calculate_siblings';

describe('calculateSiblings(metrics, metric)', () => {
it('should return all siblings', () => {
test('should return all siblings', () => {
const metrics = [
{ id: 1, type: 'max', field: 'network.bytes' },
{ id: 2, type: 'derivative', field: 1 },
Expand All @@ -30,7 +29,7 @@ describe('calculateSiblings(metrics, metric)', () => {
{ id: 5, type: 'count' },
];
const siblings = calculateSiblings(metrics, { id: 2 });
expect(siblings).to.eql([
expect(siblings).toEqual([
{ id: 1, type: 'max', field: 'network.bytes' },
{ id: 5, type: 'count' },
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,52 @@
* under the License.
*/

import sinon from 'sinon';
import { expect } from 'chai';
import { handleChange, handleAdd, handleDelete } from '../collection_actions';
import { handleChange, handleAdd, handleDelete } from './collection_actions';

describe('collection actions', () => {
it('handleChange() calls props.onChange() with updated collection', () => {
const fn = sinon.spy();
test('handleChange() calls props.onChange() with updated collection', () => {
const fn = jest.fn();
const props = {
model: { test: [{ id: 1, title: 'foo' }] },
name: 'test',
onChange: fn,
};
handleChange.call(null, props, { id: 1, title: 'bar' });
expect(fn.calledOnce).to.equal(true);
expect(fn.firstCall.args[0]).to.eql({
expect(fn.mock.calls.length).toEqual(1);
expect(fn.mock.calls[0][0]).toEqual({
test: [{ id: 1, title: 'bar' }],
});
});

it('handleAdd() calls props.onChange() with update collection', () => {
const newItemFn = sinon.stub().returns({ id: 2, title: 'example' });
const fn = sinon.spy();
test('handleAdd() calls props.onChange() with update collection', () => {
const newItemFn = jest.fn(() => ({ id: 2, title: 'example' }));
const fn = jest.fn();
const props = {
model: { test: [{ id: 1, title: 'foo' }] },
name: 'test',
onChange: fn,
};
handleAdd.call(null, props, newItemFn);
expect(fn.calledOnce).to.equal(true);
expect(newItemFn.calledOnce).to.equal(true);
expect(fn.firstCall.args[0]).to.eql({
expect(fn.mock.calls.length).toEqual(1);
expect(newItemFn.mock.calls.length).toEqual(1);
expect(fn.mock.calls[0][0]).toEqual({
test: [
{ id: 1, title: 'foo' },
{ id: 2, title: 'example' },
],
});
});

it('handleDelete() calls props.onChange() with update collection', () => {
const fn = sinon.spy();
test('handleDelete() calls props.onChange() with update collection', () => {
const fn = jest.fn();
const props = {
model: { test: [{ id: 1, title: 'foo' }] },
name: 'test',
onChange: fn,
};
handleDelete.call(null, props, { id: 1 });
expect(fn.calledOnce).to.equal(true);
expect(fn.firstCall.args[0]).to.eql({
expect(fn.mock.calls.length).toEqual(1);
expect(fn.mock.calls[0][0]).toEqual({
test: [],
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
*/

describe('convertSeriesToVars(series, model)', () => {
it('returns and object', () => {});
test('returns and object', () => {});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,25 @@
* under the License.
*/

import sinon from 'sinon';
import { expect } from 'chai';
import { createNumberHandler } from '../create_number_handler';
import { createNumberHandler } from './create_number_handler';

describe('createNumberHandler()', () => {
let handleChange;
let changeHandler;
let event;

beforeEach(() => {
handleChange = sinon.spy();
handleChange = jest.fn();
changeHandler = createNumberHandler(handleChange);
event = { preventDefault: sinon.spy(), target: { value: '1' } };
event = { preventDefault: jest.fn(), target: { value: '1' } };
const fn = changeHandler('test');
fn(event);
});

it('calls handleChange() function with partial', () => {
expect(event.preventDefault.calledOnce).to.equal(true);
expect(handleChange.calledOnce).to.equal(true);
expect(handleChange.firstCall.args[0]).to.eql({
test('calls handleChange() function with partial', () => {
expect(event.preventDefault.mock.calls.length).toEqual(1);
expect(handleChange.mock.calls.length).toEqual(1);
expect(handleChange.mock.calls[0][0]).toEqual({
test: 1,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@
* under the License.
*/

import sinon from 'sinon';
import { expect } from 'chai';
import { createSelectHandler } from '../create_select_handler';
import { createSelectHandler } from './create_select_handler';

describe('createSelectHandler()', () => {
let handleChange;
let changeHandler;

beforeEach(() => {
handleChange = sinon.spy();
handleChange = jest.fn();
changeHandler = createSelectHandler(handleChange);
const fn = changeHandler('test');
fn([{ value: 'foo' }]);
});

it('calls handleChange() function with partial', () => {
expect(handleChange.calledOnce).to.equal(true);
expect(handleChange.firstCall.args[0]).to.eql({
test('calls handleChange() function with partial', () => {
expect(handleChange.mock.calls.length).toEqual(1);
expect(handleChange.mock.calls[0][0]).toEqual({
test: 'foo',
});
});
Expand Down
Loading

0 comments on commit b9161e7

Please sign in to comment.