2017-11-13 04:11:54 -05:00
|
|
|
import * as textUtils from '~/lib/utils/text_utility';
|
2016-12-14 15:37:17 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
describe('text_utility', () => {
|
2017-11-13 04:11:54 -05:00
|
|
|
describe('addDelimiter', () => {
|
|
|
|
it('should add a delimiter to the given string', () => {
|
|
|
|
expect(textUtils.addDelimiter('1234')).toEqual('1,234');
|
|
|
|
expect(textUtils.addDelimiter('222222')).toEqual('222,222');
|
2017-04-10 21:02:55 -04:00
|
|
|
});
|
2016-12-14 15:37:17 -05:00
|
|
|
|
2017-11-13 04:11:54 -05:00
|
|
|
it('should not add a delimiter if string contains no numbers', () => {
|
|
|
|
expect(textUtils.addDelimiter('aaaa')).toEqual('aaaa');
|
2017-02-01 10:23:01 -05:00
|
|
|
});
|
2017-04-10 21:02:55 -04:00
|
|
|
});
|
2017-02-21 14:21:49 -05:00
|
|
|
|
2017-10-10 03:47:42 -04:00
|
|
|
describe('highCountTrim', () => {
|
2017-04-10 21:02:55 -04:00
|
|
|
it('returns 99+ for count >= 100', () => {
|
2017-11-13 04:11:54 -05:00
|
|
|
expect(textUtils.highCountTrim(105)).toBe('99+');
|
|
|
|
expect(textUtils.highCountTrim(100)).toBe('99+');
|
2017-04-10 21:02:55 -04:00
|
|
|
});
|
2017-02-21 14:21:49 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
it('returns exact number for count < 100', () => {
|
2017-11-13 04:11:54 -05:00
|
|
|
expect(textUtils.highCountTrim(45)).toBe(45);
|
2017-11-10 18:41:04 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-13 04:11:54 -05:00
|
|
|
describe('humanize', () => {
|
|
|
|
it('should remove underscores and uppercase the first letter', () => {
|
|
|
|
expect(textUtils.humanize('foo_bar')).toEqual('Foo bar');
|
2017-04-10 21:02:55 -04:00
|
|
|
});
|
2020-01-17 16:08:29 -05:00
|
|
|
it('should remove underscores and dashes and uppercase the first letter', () => {
|
|
|
|
expect(textUtils.humanize('foo_bar-foo', '[_-]')).toEqual('Foo bar foo');
|
|
|
|
});
|
2017-11-13 04:11:54 -05:00
|
|
|
});
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-11-13 04:11:54 -05:00
|
|
|
describe('dasherize', () => {
|
|
|
|
it('should replace underscores with dashes', () => {
|
|
|
|
expect(textUtils.dasherize('foo_bar_foo')).toEqual('foo-bar-foo');
|
|
|
|
});
|
|
|
|
});
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2019-04-11 23:20:45 -04:00
|
|
|
describe('capitalizeFirstCharacter', () => {
|
|
|
|
it('returns string with first letter capitalized', () => {
|
|
|
|
expect(textUtils.capitalizeFirstCharacter('gitlab')).toEqual('Gitlab');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-03 04:20:57 -04:00
|
|
|
describe('slugify', () => {
|
|
|
|
it('should remove accents and convert to lower case', () => {
|
|
|
|
expect(textUtils.slugify('João')).toEqual('jo-o');
|
|
|
|
});
|
2018-09-08 02:03:00 -04:00
|
|
|
it('should replaces whitespaces with hyphens and convert to lower case', () => {
|
2019-07-03 04:20:57 -04:00
|
|
|
expect(textUtils.slugify('My Input String')).toEqual('my-input-string');
|
|
|
|
});
|
|
|
|
it('should remove trailing whitespace and replace whitespaces within string with a hyphen', () => {
|
|
|
|
expect(textUtils.slugify(' a new project ')).toEqual('a-new-project');
|
|
|
|
});
|
|
|
|
it('should only remove non-allowed special characters', () => {
|
2020-01-17 16:08:29 -05:00
|
|
|
expect(textUtils.slugify('test!_pro-ject~')).toEqual('test-_pro-ject');
|
2019-07-03 04:20:57 -04:00
|
|
|
});
|
|
|
|
it('should squash multiple hypens', () => {
|
2020-01-17 16:08:29 -05:00
|
|
|
expect(textUtils.slugify('test!!!!_pro-ject~')).toEqual('test-_pro-ject');
|
2019-07-03 04:20:57 -04:00
|
|
|
});
|
|
|
|
it('should return empty string if only non-allowed characters', () => {
|
|
|
|
expect(textUtils.slugify('здрасти')).toEqual('');
|
2018-09-08 02:03:00 -04:00
|
|
|
});
|
2020-01-17 16:08:29 -05:00
|
|
|
it('should squash multiple separators', () => {
|
|
|
|
expect(textUtils.slugify('Test:-)')).toEqual('test');
|
|
|
|
});
|
|
|
|
it('should trim any separators from the beginning and end of the slug', () => {
|
|
|
|
expect(textUtils.slugify('-Test:-)-')).toEqual('test');
|
|
|
|
});
|
2018-09-08 02:03:00 -04:00
|
|
|
});
|
|
|
|
|
2018-01-19 04:38:34 -05:00
|
|
|
describe('stripHtml', () => {
|
2018-01-03 16:03:39 -05:00
|
|
|
it('replaces html tag with the default replacement', () => {
|
2018-04-02 17:50:39 -04:00
|
|
|
expect(textUtils.stripHtml('This is a text with <p>html</p>.')).toEqual(
|
|
|
|
'This is a text with html.',
|
|
|
|
);
|
2018-01-03 16:03:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('replaces html tags with the provided replacement', () => {
|
2018-04-02 17:50:39 -04:00
|
|
|
expect(textUtils.stripHtml('This is a text with <p>html</p>.', ' ')).toEqual(
|
|
|
|
'This is a text with html .',
|
|
|
|
);
|
2018-01-03 16:03:39 -05:00
|
|
|
});
|
2018-05-03 04:25:22 -04:00
|
|
|
|
|
|
|
it('passes through with null string input', () => {
|
|
|
|
expect(textUtils.stripHtml(null, ' ')).toEqual(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes through with undefined string input', () => {
|
|
|
|
expect(textUtils.stripHtml(undefined, ' ')).toEqual(undefined);
|
|
|
|
});
|
2018-01-03 16:03:39 -05:00
|
|
|
});
|
2018-02-07 07:57:44 -05:00
|
|
|
|
|
|
|
describe('convertToCamelCase', () => {
|
|
|
|
it('converts snake_case string to camelCase string', () => {
|
|
|
|
expect(textUtils.convertToCamelCase('snake_case')).toBe('snakeCase');
|
|
|
|
});
|
|
|
|
});
|
2018-04-02 17:50:39 -04:00
|
|
|
|
2019-11-05 19:06:13 -05:00
|
|
|
describe('convertToSnakeCase', () => {
|
|
|
|
it.each`
|
|
|
|
txt | result
|
|
|
|
${'snakeCase'} | ${'snake_case'}
|
|
|
|
${'snake Case'} | ${'snake_case'}
|
|
|
|
${'snake case'} | ${'snake_case'}
|
|
|
|
${'snake_case'} | ${'snake_case'}
|
|
|
|
${'snakeCasesnake Case'} | ${'snake_casesnake_case'}
|
|
|
|
`('converts string $txt to $result string', ({ txt, result }) => {
|
|
|
|
expect(textUtils.convertToSnakeCase(txt)).toEqual(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-02 17:50:39 -04:00
|
|
|
describe('convertToSentenceCase', () => {
|
|
|
|
it('converts Sentence Case to Sentence case', () => {
|
|
|
|
expect(textUtils.convertToSentenceCase('Hello World')).toBe('Hello world');
|
|
|
|
});
|
|
|
|
});
|
2018-06-21 08:22:40 -04:00
|
|
|
|
2020-01-17 16:08:29 -05:00
|
|
|
describe('convertToTitleCase', () => {
|
|
|
|
it('converts sentence case to Sentence Case', () => {
|
|
|
|
expect(textUtils.convertToTitleCase('hello world')).toBe('Hello World');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
describe('truncateSha', () => {
|
|
|
|
it('shortens SHAs to 8 characters', () => {
|
|
|
|
expect(textUtils.truncateSha('verylongsha')).toBe('verylong');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('leaves short SHAs as is', () => {
|
|
|
|
expect(textUtils.truncateSha('shortsha')).toBe('shortsha');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('splitCamelCase', () => {
|
|
|
|
it('separates a PascalCase word to two', () => {
|
|
|
|
expect(textUtils.splitCamelCase('HelloWorld')).toBe('Hello World');
|
|
|
|
});
|
|
|
|
});
|
2018-08-01 15:43:50 -04:00
|
|
|
|
|
|
|
describe('getFirstCharacterCapitalized', () => {
|
2018-10-30 06:53:01 -04:00
|
|
|
it('returns the first character capitalized, if first character is alphabetic', () => {
|
2018-08-01 15:43:50 -04:00
|
|
|
expect(textUtils.getFirstCharacterCapitalized('loremIpsumDolar')).toEqual('L');
|
|
|
|
expect(textUtils.getFirstCharacterCapitalized('Sit amit !')).toEqual('S');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the first character, if first character is non-alphabetic', () => {
|
|
|
|
expect(textUtils.getFirstCharacterCapitalized(' lorem')).toEqual(' ');
|
|
|
|
expect(textUtils.getFirstCharacterCapitalized('%#!')).toEqual('%');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns an empty string, if string is falsey', () => {
|
|
|
|
expect(textUtils.getFirstCharacterCapitalized('')).toEqual('');
|
|
|
|
expect(textUtils.getFirstCharacterCapitalized(null)).toEqual('');
|
|
|
|
});
|
|
|
|
});
|
2019-01-08 04:31:23 -05:00
|
|
|
|
|
|
|
describe('truncatePathMiddleToLength', () => {
|
|
|
|
it('does not truncate text', () => {
|
|
|
|
expect(textUtils.truncatePathMiddleToLength('app/test', 50)).toEqual('app/test');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('truncates middle of the path', () => {
|
|
|
|
expect(textUtils.truncatePathMiddleToLength('app/test/diff', 13)).toEqual('app/…/diff');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('truncates multiple times in the middle of the path', () => {
|
|
|
|
expect(textUtils.truncatePathMiddleToLength('app/test/merge_request/diff', 13)).toEqual(
|
|
|
|
'app/…/…/diff',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2019-03-12 12:35:55 -04:00
|
|
|
|
2019-04-12 11:32:24 -04:00
|
|
|
describe('slugifyWithUnderscore', () => {
|
|
|
|
it('should replaces whitespaces with underscore and convert to lower case', () => {
|
|
|
|
expect(textUtils.slugifyWithUnderscore('My Input String')).toEqual('my_input_string');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-03-12 12:35:55 -04:00
|
|
|
describe('truncateNamespace', () => {
|
|
|
|
it(`should return the root namespace if the namespace only includes one level`, () => {
|
|
|
|
expect(textUtils.truncateNamespace('a / b')).toBe('a');
|
|
|
|
});
|
|
|
|
|
2019-05-20 10:11:44 -04:00
|
|
|
it(`should return the first 2 namespaces if the namespace includes exactly 2 levels`, () => {
|
2019-03-12 12:35:55 -04:00
|
|
|
expect(textUtils.truncateNamespace('a / b / c')).toBe('a / b');
|
|
|
|
});
|
|
|
|
|
2019-05-20 10:11:44 -04:00
|
|
|
it(`should return the first and last namespaces, separated by "...", if the namespace includes more than 2 levels`, () => {
|
2019-03-12 12:35:55 -04:00
|
|
|
expect(textUtils.truncateNamespace('a / b / c / d')).toBe('a / ... / c');
|
|
|
|
expect(textUtils.truncateNamespace('a / b / c / d / e / f / g / h / i')).toBe('a / ... / h');
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should return an empty string for invalid inputs`, () => {
|
|
|
|
[undefined, null, 4, {}, true, new Date()].forEach(input => {
|
|
|
|
expect(textUtils.truncateNamespace(input)).toBe('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should not alter strings that aren't formatted as namespaces`, () => {
|
|
|
|
['', ' ', '\t', 'a', 'a \\ b'].forEach(input => {
|
|
|
|
expect(textUtils.truncateNamespace(input)).toBe(input);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-04-10 21:02:55 -04:00
|
|
|
});
|