gitlab-org--gitlab-foss/spec/javascripts/filtered_search/dropdown_utils_spec.js.es6

135 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-01-06 19:56:34 +00:00
//= require extensions/array
2016-12-14 05:30:28 +00:00
//= require filtered_search/dropdown_utils
//= require filtered_search/filtered_search_tokenizer
//= require filtered_search/filtered_search_dropdown_manager
(() => {
describe('Dropdown Utils', () => {
describe('getEscapedText', () => {
it('should return same word when it has no space', () => {
const escaped = gl.DropdownUtils.getEscapedText('textWithoutSpace');
expect(escaped).toBe('textWithoutSpace');
});
it('should escape with double quotes', () => {
let escaped = gl.DropdownUtils.getEscapedText('text with space');
expect(escaped).toBe('"text with space"');
escaped = gl.DropdownUtils.getEscapedText('won\'t fix');
expect(escaped).toBe('"won\'t fix"');
});
it('should escape with single quotes', () => {
const escaped = gl.DropdownUtils.getEscapedText('won"t fix');
expect(escaped).toBe('\'won"t fix\'');
});
it('should escape with single quotes by default', () => {
const escaped = gl.DropdownUtils.getEscapedText('won"t\' fix');
expect(escaped).toBe('\'won"t\' fix\'');
});
});
describe('filterWithSymbol', () => {
2017-01-19 11:33:19 +00:00
let input;
2016-12-14 05:30:28 +00:00
const item = {
title: '@root',
};
2017-01-19 11:33:19 +00:00
beforeEach(() => {
setFixtures(`
<input type="text" id="test" />
`);
input = document.getElementById('test');
});
2016-12-14 05:30:28 +00:00
it('should filter without symbol', () => {
2017-01-19 11:33:19 +00:00
input.value = ':roo';
const updatedItem = gl.DropdownUtils.filterWithSymbol('@', input, item);
2016-12-14 05:30:28 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
it('should filter with symbol', () => {
2017-01-19 11:33:19 +00:00
input.value = '@roo';
const updatedItem = gl.DropdownUtils.filterWithSymbol('@', input, item);
2016-12-14 05:30:28 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
it('should filter with colon', () => {
2017-01-19 11:33:19 +00:00
input.value = 'roo';
const updatedItem = gl.DropdownUtils.filterWithSymbol('@', input, item);
2016-12-14 05:30:28 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
});
describe('filterHint', () => {
2017-01-20 18:04:15 +00:00
let input;
beforeEach(() => {
setFixtures(`
<input type="text" id="test" />
`);
input = document.getElementById('test');
});
it('should filter', () => {
2017-01-20 18:04:15 +00:00
input.value = 'l';
let updatedItem = gl.DropdownUtils.filterHint(input, {
2016-12-14 05:30:28 +00:00
hint: 'label',
2017-01-20 18:04:15 +00:00
});
2016-12-14 05:30:28 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
2017-01-20 18:04:15 +00:00
input.value = 'o';
updatedItem = gl.DropdownUtils.filterHint(input, {
2016-12-14 05:30:28 +00:00
hint: 'label',
}, 'o');
expect(updatedItem.droplab_hidden).toBe(true);
});
it('should return droplab_hidden false when item has no hint', () => {
2017-01-20 18:04:15 +00:00
const updatedItem = gl.DropdownUtils.filterHint(input, {}, '');
2016-12-14 05:30:28 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
});
describe('setDataValueIfSelected', () => {
beforeEach(() => {
spyOn(gl.FilteredSearchDropdownManager, 'addWordToInput')
.and.callFake(() => {});
});
it('calls addWordToInput when dataValue exists', () => {
const selected = {
getAttribute: () => 'value',
};
2017-01-06 19:56:34 +00:00
gl.DropdownUtils.setDataValueIfSelected(null, selected);
2016-12-14 05:30:28 +00:00
expect(gl.FilteredSearchDropdownManager.addWordToInput.calls.count()).toEqual(1);
});
it('returns true when dataValue exists', () => {
const selected = {
getAttribute: () => 'value',
};
2017-01-06 19:56:34 +00:00
const result = gl.DropdownUtils.setDataValueIfSelected(null, selected);
2016-12-14 05:30:28 +00:00
expect(result).toBe(true);
});
it('returns false when dataValue does not exist', () => {
const selected = {
getAttribute: () => null,
};
2017-01-06 19:56:34 +00:00
const result = gl.DropdownUtils.setDataValueIfSelected(null, selected);
2016-12-14 05:30:28 +00:00
expect(result).toBe(false);
});
});
});
})();