2017-05-16 17:01:51 -04:00
|
|
|
import '~/lib/utils/text_utility';
|
2016-12-14 15:37:17 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
describe('text_utility', () => {
|
|
|
|
describe('gl.text.getTextWidth', () => {
|
|
|
|
it('returns zero width when no text is passed', () => {
|
|
|
|
expect(gl.text.getTextWidth('')).toBe(0);
|
|
|
|
});
|
2016-12-14 15:37:17 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
it('returns zero width when no text is passed and font is passed', () => {
|
|
|
|
expect(gl.text.getTextWidth('', '100px sans-serif')).toBe(0);
|
|
|
|
});
|
2016-12-14 15:37:17 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
it('returns width when text is passed', () => {
|
|
|
|
expect(gl.text.getTextWidth('foo') > 0).toBe(true);
|
|
|
|
});
|
2016-12-14 15:37:17 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
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);
|
2016-12-14 15:37:17 -05:00
|
|
|
});
|
2017-04-10 21:02:55 -04:00
|
|
|
});
|
2017-02-01 10:23:01 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
describe('gl.text.pluralize', () => {
|
|
|
|
it('returns pluralized', () => {
|
|
|
|
expect(gl.text.pluralize('test', 2)).toBe('tests');
|
|
|
|
});
|
2017-02-01 10:23:01 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
it('returns pluralized when count is 0', () => {
|
|
|
|
expect(gl.text.pluralize('test', 0)).toBe('tests');
|
|
|
|
});
|
2017-02-01 10:23:01 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
it('does not return pluralized', () => {
|
|
|
|
expect(gl.text.pluralize('test', 1)).toBe('test');
|
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-04-10 21:02:55 -04:00
|
|
|
describe('gl.text.highCountTrim', () => {
|
|
|
|
it('returns 99+ for count >= 100', () => {
|
|
|
|
expect(gl.text.highCountTrim(105)).toBe('99+');
|
|
|
|
expect(gl.text.highCountTrim(100)).toBe('99+');
|
|
|
|
});
|
2017-02-21 14:21:49 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
it('returns exact number for count < 100', () => {
|
|
|
|
expect(gl.text.highCountTrim(45)).toBe(45);
|
2017-02-21 14:21:49 -05:00
|
|
|
});
|
2017-04-10 21:02:55 -04:00
|
|
|
});
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
describe('gl.text.insertText', () => {
|
|
|
|
let textArea;
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
beforeAll(() => {
|
|
|
|
textArea = document.createElement('textarea');
|
|
|
|
document.querySelector('body').appendChild(textArea);
|
2017-08-18 03:44:08 -04:00
|
|
|
textArea.focus();
|
2017-04-10 21:02:55 -04:00
|
|
|
});
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
afterAll(() => {
|
|
|
|
textArea.parentNode.removeChild(textArea);
|
|
|
|
});
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
describe('without selection', () => {
|
|
|
|
it('inserts the tag on an empty line', () => {
|
|
|
|
const initialValue = '';
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
textArea.value = initialValue;
|
|
|
|
textArea.selectionStart = 0;
|
|
|
|
textArea.selectionEnd = 0;
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
gl.text.insertText(textArea, textArea.value, '*', null, '', false);
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
expect(textArea.value).toEqual(`${initialValue}* `);
|
|
|
|
});
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
it('inserts the tag on a new line if the current one is not empty', () => {
|
|
|
|
const initialValue = 'some text';
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
textArea.value = initialValue;
|
|
|
|
textArea.setSelectionRange(initialValue.length, initialValue.length);
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
gl.text.insertText(textArea, textArea.value, '*', null, '', false);
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
expect(textArea.value).toEqual(`${initialValue}\n* `);
|
|
|
|
});
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
it('inserts the tag on the same line if the current line only contains spaces', () => {
|
|
|
|
const initialValue = ' ';
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
textArea.value = initialValue;
|
|
|
|
textArea.setSelectionRange(initialValue.length, initialValue.length);
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
gl.text.insertText(textArea, textArea.value, '*', null, '', false);
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
expect(textArea.value).toEqual(`${initialValue}* `);
|
|
|
|
});
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
it('inserts the tag on the same line if the current line only contains tabs', () => {
|
|
|
|
const initialValue = '\t\t\t';
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
textArea.value = initialValue;
|
|
|
|
textArea.setSelectionRange(initialValue.length, initialValue.length);
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
gl.text.insertText(textArea, textArea.value, '*', null, '', false);
|
2017-02-22 14:58:24 -05:00
|
|
|
|
2017-04-10 21:02:55 -04:00
|
|
|
expect(textArea.value).toEqual(`${initialValue}* `);
|
2017-02-22 14:58:24 -05:00
|
|
|
});
|
|
|
|
});
|
2016-12-14 15:37:17 -05:00
|
|
|
});
|
2017-04-10 21:02:55 -04:00
|
|
|
});
|