text_utility_spec.js 2.4 KB
Newer Older
1
import * as textUtils from '~/lib/utils/text_utility';
Clement Ho's avatar
Clement Ho committed
2

3
describe('text_utility', () => {
4 5 6 7
  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');
8
    });
Clement Ho's avatar
Clement Ho committed
9

10 11
    it('should not add a delimiter if string contains no numbers', () => {
      expect(textUtils.addDelimiter('aaaa')).toEqual('aaaa');
12
    });
13
  });
14

15
  describe('highCountTrim', () => {
16
    it('returns 99+ for count >= 100', () => {
17 18
      expect(textUtils.highCountTrim(105)).toBe('99+');
      expect(textUtils.highCountTrim(100)).toBe('99+');
19
    });
20

21
    it('returns exact number for count < 100', () => {
22
      expect(textUtils.highCountTrim(45)).toBe(45);
Clement Ho's avatar
Clement Ho committed
23 24 25 26 27
    });
  });

  describe('capitalizeFirstCharacter', () => {
    it('returns string with first letter capitalized', () => {
28 29 30
      expect(textUtils.capitalizeFirstCharacter('gitlab')).toEqual('Gitlab');
      expect(textUtils.highCountTrim(105)).toBe('99+');
      expect(textUtils.highCountTrim(100)).toBe('99+');
31
    });
32
  });
33

34 35 36
  describe('humanize', () => {
    it('should remove underscores and uppercase the first letter', () => {
      expect(textUtils.humanize('foo_bar')).toEqual('Foo bar');
37
    });
38
  });
39

40 41 42
  describe('pluralize', () => {
    it('should pluralize given string', () => {
      expect(textUtils.pluralize('test', 2)).toBe('tests');
43
    });
44

45 46 47
    it('should pluralize when count is 0', () => {
      expect(textUtils.pluralize('test', 0)).toBe('tests');
    });
48

49 50 51 52
    it('should not pluralize when count is 1', () => {
      expect(textUtils.pluralize('test', 1)).toBe('test');
    });
  });
53

54 55 56 57 58
  describe('dasherize', () => {
    it('should replace underscores with dashes', () => {
      expect(textUtils.dasherize('foo_bar_foo')).toEqual('foo-bar-foo');
    });
  });
59

60 61 62
  describe('slugify', () => {
    it('should remove accents and convert to lower case', () => {
      expect(textUtils.slugify('João')).toEqual('joão');
63
    });
Clement Ho's avatar
Clement Ho committed
64
  });
65

Tim Zallmann's avatar
Tim Zallmann committed
66
  describe('stripHtml', () => {
67
    it('replaces html tag with the default replacement', () => {
Tim Zallmann's avatar
Tim Zallmann committed
68
      expect(textUtils.stripHtml('This is a text with <p>html</p>.')).toEqual('This is a text with html.');
69 70 71
    });

    it('replaces html tags with the provided replacement', () => {
Tim Zallmann's avatar
Tim Zallmann committed
72
      expect(textUtils.stripHtml('This is a text with <p>html</p>.', ' ')).toEqual('This is a text with  html .');
73 74
    });
  });
75
});