BigW Consortium Gitlab

text_utility_spec.js 3.28 KB
Newer Older
Mike Greiling committed
1
require('~/lib/utils/text_utility');
Clement Ho committed
2

3 4 5 6 7
describe('text_utility', () => {
  describe('gl.text.getTextWidth', () => {
    it('returns zero width when no text is passed', () => {
      expect(gl.text.getTextWidth('')).toBe(0);
    });
Clement Ho committed
8

9 10 11
    it('returns zero width when no text is passed and font is passed', () => {
      expect(gl.text.getTextWidth('', '100px sans-serif')).toBe(0);
    });
Clement Ho committed
12

13 14 15
    it('returns width when text is passed', () => {
      expect(gl.text.getTextWidth('foo') > 0).toBe(true);
    });
Clement Ho committed
16

17 18 19 20
    it('returns bigger width when font is larger', () => {
      const largeFont = gl.text.getTextWidth('foo', '100px sans-serif');
      const regular = gl.text.getTextWidth('foo', '10px sans-serif');
      expect(largeFont > regular).toBe(true);
Clement Ho committed
21
    });
22
  });
23

24 25 26 27
  describe('gl.text.pluralize', () => {
    it('returns pluralized', () => {
      expect(gl.text.pluralize('test', 2)).toBe('tests');
    });
28

29 30 31
    it('returns pluralized when count is 0', () => {
      expect(gl.text.pluralize('test', 0)).toBe('tests');
    });
32

33 34
    it('does not return pluralized', () => {
      expect(gl.text.pluralize('test', 1)).toBe('test');
35
    });
36
  });
37

38 39 40 41 42
  describe('gl.text.highCountTrim', () => {
    it('returns 99+ for count >= 100', () => {
      expect(gl.text.highCountTrim(105)).toBe('99+');
      expect(gl.text.highCountTrim(100)).toBe('99+');
    });
43

44 45
    it('returns exact number for count < 100', () => {
      expect(gl.text.highCountTrim(45)).toBe(45);
46
    });
47
  });
48

49 50
  describe('gl.text.insertText', () => {
    let textArea;
51

52 53 54 55
    beforeAll(() => {
      textArea = document.createElement('textarea');
      document.querySelector('body').appendChild(textArea);
    });
56

57 58 59
    afterAll(() => {
      textArea.parentNode.removeChild(textArea);
    });
60

61 62 63
    describe('without selection', () => {
      it('inserts the tag on an empty line', () => {
        const initialValue = '';
64

65 66 67
        textArea.value = initialValue;
        textArea.selectionStart = 0;
        textArea.selectionEnd = 0;
68

69
        gl.text.insertText(textArea, textArea.value, '*', null, '', false);
70

71 72
        expect(textArea.value).toEqual(`${initialValue}* `);
      });
73

74 75
      it('inserts the tag on a new line if the current one is not empty', () => {
        const initialValue = 'some text';
76

77 78
        textArea.value = initialValue;
        textArea.setSelectionRange(initialValue.length, initialValue.length);
79

80
        gl.text.insertText(textArea, textArea.value, '*', null, '', false);
81

82 83
        expect(textArea.value).toEqual(`${initialValue}\n* `);
      });
84

85 86
      it('inserts the tag on the same line if the current line only contains spaces', () => {
        const initialValue = '  ';
87

88 89
        textArea.value = initialValue;
        textArea.setSelectionRange(initialValue.length, initialValue.length);
90

91
        gl.text.insertText(textArea, textArea.value, '*', null, '', false);
92

93 94
        expect(textArea.value).toEqual(`${initialValue}* `);
      });
95

96 97
      it('inserts the tag on the same line if the current line only contains tabs', () => {
        const initialValue = '\t\t\t';
98

99 100
        textArea.value = initialValue;
        textArea.setSelectionRange(initialValue.length, initialValue.length);
101

102
        gl.text.insertText(textArea, textArea.value, '*', null, '', false);
103

104
        expect(textArea.value).toEqual(`${initialValue}* `);
105 106
      });
    });
Clement Ho committed
107
  });
108
});