debian-mirror-gitlab/spec/javascripts/lib/utils/text_utility_spec.js

139 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import * as textUtils from '~/lib/utils/text_utility';
2017-08-17 22:00:37 +05:30
describe('text_utility', () => {
2018-03-17 18:26:18 +05:30
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-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
it('should not add a delimiter if string contains no numbers', () => {
expect(textUtils.addDelimiter('aaaa')).toEqual('aaaa');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
describe('highCountTrim', () => {
it('returns 99+ for count >= 100', () => {
expect(textUtils.highCountTrim(105)).toBe('99+');
expect(textUtils.highCountTrim(100)).toBe('99+');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
it('returns exact number for count < 100', () => {
expect(textUtils.highCountTrim(45)).toBe(45);
2017-08-17 22:00:37 +05:30
});
});
2018-03-17 18:26:18 +05:30
describe('capitalizeFirstCharacter', () => {
it('returns string with first letter capitalized', () => {
expect(textUtils.capitalizeFirstCharacter('gitlab')).toEqual('Gitlab');
expect(textUtils.highCountTrim(105)).toBe('99+');
expect(textUtils.highCountTrim(100)).toBe('99+');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
describe('humanize', () => {
it('should remove underscores and uppercase the first letter', () => {
expect(textUtils.humanize('foo_bar')).toEqual('Foo bar');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
describe('pluralize', () => {
it('should pluralize given string', () => {
expect(textUtils.pluralize('test', 2)).toBe('tests');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
it('should pluralize when count is 0', () => {
expect(textUtils.pluralize('test', 0)).toBe('tests');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
it('should not pluralize when count is 1', () => {
expect(textUtils.pluralize('test', 1)).toBe('test');
2017-08-17 22:00:37 +05:30
});
});
2018-03-17 18:26:18 +05:30
describe('dasherize', () => {
it('should replace underscores with dashes', () => {
expect(textUtils.dasherize('foo_bar_foo')).toEqual('foo-bar-foo');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
describe('slugify', () => {
it('should remove accents and convert to lower case', () => {
expect(textUtils.slugify('João')).toEqual('joão');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
2018-11-20 20:47:30 +05:30
describe('slugifyWithHyphens', () => {
it('should replaces whitespaces with hyphens and convert to lower case', () => {
expect(textUtils.slugifyWithHyphens('My Input String')).toEqual('my-input-string');
});
});
2018-03-17 18:26:18 +05:30
describe('stripHtml', () => {
it('replaces html tag with the default replacement', () => {
2018-05-09 12:01:36 +05:30
expect(textUtils.stripHtml('This is a text with <p>html</p>.')).toEqual(
'This is a text with html.',
);
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
it('replaces html tags with the provided replacement', () => {
2018-05-09 12:01:36 +05:30
expect(textUtils.stripHtml('This is a text with <p>html</p>.', ' ')).toEqual(
'This is a text with html .',
);
2018-03-17 18:26:18 +05:30
});
2018-10-15 14:42:47 +05:30
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-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
describe('convertToCamelCase', () => {
it('converts snake_case string to camelCase string', () => {
expect(textUtils.convertToCamelCase('snake_case')).toBe('snakeCase');
2017-08-17 22:00:37 +05:30
});
});
2018-05-09 12:01:36 +05:30
describe('convertToSentenceCase', () => {
it('converts Sentence Case to Sentence case', () => {
expect(textUtils.convertToSentenceCase('Hello World')).toBe('Hello world');
});
});
2018-11-08 19:23:39 +05:30
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-11-18 11:00:15 +05:30
describe('getFirstCharacterCapitalized', () => {
2018-12-13 13:39:08 +05:30
it('returns the first character capitalized, if first character is alphabetic', () => {
2018-11-18 11:00:15 +05:30
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('');
});
});
2017-08-17 22:00:37 +05:30
});