kibana/x-pack/plugins/ml/common/util/string_utils.test.ts
Walter Rafelsberger a489e5f0b1
[ML] Data Grid Histograms (#68359)
Adds support for histogram charts to data grid columns.
- Adds a toggle button to the data grid's header to enabled/disable column charts.
- When enabled, the charts get rendered as part of the data grid header.
- Histogram charts will get rendered for fields based on date, number, string and boolean.
2020-06-19 08:39:50 +02:00

58 lines
1.9 KiB
TypeScript

/*
* 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 { renderTemplate, getMedianStringLength, stringHash } from './string_utils';
const strings: string[] = [
'foo',
'foofoofoofoofoo',
'foofoofoo',
'f',
'f',
'foofoofoofoofoofoofoo',
];
const noStrings: string[] = [];
describe('ML - string utils', () => {
describe('renderTemplate', () => {
test('returns plain string', () => {
const templateString = 'plain string';
const result = renderTemplate(templateString);
expect(result).toBe(result);
});
test('returns rendered template with one replacement', () => {
const templateString = 'string with {{one}} replacement';
const result = renderTemplate(templateString, { one: '1' });
expect(result).toBe('string with 1 replacement');
});
test('returns rendered template with two replacements', () => {
const templateString = 'string with {{one}} replacement, and a {{two}} one.';
const result = renderTemplate(templateString, { one: '1', two: '2nd' });
expect(result).toBe('string with 1 replacement, and a 2nd one.');
});
});
describe('getMedianStringLength', () => {
test('test median for string array', () => {
const result = getMedianStringLength(strings);
expect(result).toBe(9);
});
test('test median for no strings', () => {
const result = getMedianStringLength(noStrings);
expect(result).toBe(0);
});
});
describe('stringHash', () => {
test('should return a unique number based off a string', () => {
const hash1 = stringHash('the-string-1');
const hash2 = stringHash('the-string-2');
expect(hash1).not.toBe(hash2);
});
});
});