gitlab-org--gitlab-foss/spec/frontend/filtered_search/dropdown_utils_spec.js

375 lines
11 KiB
JavaScript
Raw Normal View History

2018-02-21 20:22:56 +00:00
import DropdownUtils from '~/filtered_search/dropdown_utils';
import FilteredSearchDropdownManager from '~/filtered_search/filtered_search_dropdown_manager';
import IssuableFilteredSearchTokenKeys from '~/filtered_search/issuable_filtered_search_token_keys';
import FilteredSearchSpecHelper from '../helpers/filtered_search_spec_helper';
2016-12-14 05:30:28 +00:00
describe('Dropdown Utils', () => {
const issueListFixture = 'issues/issue_list.html';
preloadFixtures(issueListFixture);
describe('getEscapedText', () => {
it('should return same word when it has no space', () => {
2018-02-21 20:22:56 +00:00
const escaped = DropdownUtils.getEscapedText('textWithoutSpace');
2018-10-09 18:03:09 +00:00
expect(escaped).toBe('textWithoutSpace');
});
2016-12-14 05:30:28 +00:00
it('should escape with double quotes', () => {
2018-02-21 20:22:56 +00:00
let escaped = DropdownUtils.getEscapedText('text with space');
2018-10-09 18:03:09 +00:00
expect(escaped).toBe('"text with space"');
2016-12-14 05:30:28 +00:00
2018-10-17 07:13:26 +00:00
escaped = DropdownUtils.getEscapedText("won't fix");
2018-10-09 18:03:09 +00:00
expect(escaped).toBe('"won\'t fix"');
});
2016-12-14 05:30:28 +00:00
it('should escape with single quotes', () => {
2018-02-21 20:22:56 +00:00
const escaped = DropdownUtils.getEscapedText('won"t fix');
2018-10-09 18:03:09 +00:00
2018-10-17 07:13:26 +00:00
expect(escaped).toBe("'won\"t fix'");
});
2016-12-14 05:30:28 +00:00
it('should escape with single quotes by default', () => {
2018-02-21 20:22:56 +00:00
const escaped = DropdownUtils.getEscapedText('won"t\' fix');
2018-10-09 18:03:09 +00:00
2018-10-17 07:13:26 +00:00
expect(escaped).toBe("'won\"t' fix'");
2016-12-14 05:30:28 +00:00
});
});
2016-12-14 05:30:28 +00:00
describe('filterWithSymbol', () => {
let input;
const item = {
title: '@root',
};
2016-12-14 05:30:28 +00:00
beforeEach(() => {
setFixtures(`
<input type="text" id="test" />
`);
2017-01-19 11:33:19 +00:00
input = document.getElementById('test');
});
2017-01-19 11:33:19 +00:00
it('should filter without symbol', () => {
input.value = 'roo';
2017-01-19 11:33:19 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('@', input, item);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
2016-12-14 05:30:28 +00:00
it('should filter with symbol', () => {
input.value = '@roo';
2017-01-19 11:33:19 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('@', input, item);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
2016-12-14 05:30:28 +00:00
describe('filters multiple word title', () => {
const multipleWordItem = {
title: 'Community Contributions',
};
2017-01-26 21:50:06 +00:00
it('should filter with double quote', () => {
input.value = '"';
2017-01-26 21:50:06 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('~', input, multipleWordItem);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
2017-01-26 21:50:06 +00:00
it('should filter with double quote and symbol', () => {
input.value = '~"';
2017-01-26 21:50:06 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('~', input, multipleWordItem);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
2017-01-26 21:50:06 +00:00
it('should filter with double quote and multiple words', () => {
input.value = '"community con';
2017-01-26 21:50:06 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('~', input, multipleWordItem);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
2017-01-26 21:50:06 +00:00
it('should filter with double quote, symbol and multiple words', () => {
input.value = '~"community con';
2017-01-26 21:50:06 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('~', input, multipleWordItem);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
2017-01-26 21:50:06 +00:00
it('should filter with single quote', () => {
2018-10-17 07:13:26 +00:00
input.value = "'";
2017-01-26 21:50:06 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('~', input, multipleWordItem);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
2017-01-26 21:50:06 +00:00
it('should filter with single quote and symbol', () => {
2018-10-17 07:13:26 +00:00
input.value = "~'";
2017-01-26 21:50:06 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('~', input, multipleWordItem);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
2017-01-26 21:50:06 +00:00
it('should filter with single quote and multiple words', () => {
2018-10-17 07:13:26 +00:00
input.value = "'community con";
2017-01-26 21:50:06 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('~', input, multipleWordItem);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
2017-01-26 21:50:06 +00:00
it('should filter with single quote, symbol and multiple words', () => {
2018-10-17 07:13:26 +00:00
input.value = "~'community con";
2017-01-26 21:50:06 +00:00
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterWithSymbol('~', input, multipleWordItem);
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
2017-01-26 21:50:06 +00:00
});
2016-12-14 05:30:28 +00:00
});
});
2016-12-14 05:30:28 +00:00
describe('filterHint', () => {
let input;
2017-05-23 16:35:54 +00:00
let allowedKeys;
2017-01-20 18:04:15 +00:00
beforeEach(() => {
setFixtures(`
<ul class="tokens-container">
<li class="input-token">
<input class="filtered-search" type="text" id="test" />
</li>
</ul>
`);
2016-12-14 05:30:28 +00:00
input = document.getElementById('test');
allowedKeys = IssuableFilteredSearchTokenKeys.getKeys();
});
2016-12-14 05:30:28 +00:00
2017-05-23 16:35:54 +00:00
function config() {
return {
input,
allowedKeys,
};
}
it('should filter', () => {
input.value = 'l';
2018-02-21 20:22:56 +00:00
let updatedItem = DropdownUtils.filterHint(config(), {
hint: 'label',
2016-12-14 05:30:28 +00:00
});
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
input.value = 'o';
2018-02-21 20:22:56 +00:00
updatedItem = DropdownUtils.filterHint(config(), {
hint: 'label',
});
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(true);
});
it('should return droplab_hidden false when item has no hint', () => {
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterHint(config(), {}, '');
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
});
it('should allow multiple if item.type is array', () => {
input.value = 'label:~first la';
2018-02-21 20:22:56 +00:00
const updatedItem = DropdownUtils.filterHint(config(), {
hint: 'label',
type: 'array',
});
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(false);
2016-12-14 05:30:28 +00:00
});
it('should prevent multiple if item.type is not array', () => {
input.value = 'milestone:~first mile';
2018-02-21 20:22:56 +00:00
let updatedItem = DropdownUtils.filterHint(config(), {
hint: 'milestone',
2016-12-14 05:30:28 +00:00
});
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(true);
2016-12-14 05:30:28 +00:00
2018-02-21 20:22:56 +00:00
updatedItem = DropdownUtils.filterHint(config(), {
hint: 'milestone',
type: 'string',
2016-12-14 05:30:28 +00:00
});
2018-10-09 18:03:09 +00:00
expect(updatedItem.droplab_hidden).toBe(true);
});
});
2016-12-14 05:30:28 +00:00
describe('setDataValueIfSelected', () => {
beforeEach(() => {
jest.spyOn(FilteredSearchDropdownManager, 'addWordToInput').mockImplementation(() => {});
});
2016-12-14 05:30:28 +00:00
it('calls addWordToInput when dataValue exists', () => {
const selected = {
getAttribute: () => 'value',
hasAttribute: () => false,
};
2016-12-14 05:30:28 +00:00
DropdownUtils.setDataValueIfSelected(null, '=', selected);
2018-10-09 18:03:09 +00:00
expect(FilteredSearchDropdownManager.addWordToInput.mock.calls.length).toEqual(1);
});
2016-12-14 05:30:28 +00:00
it('returns true when dataValue exists', () => {
const selected = {
getAttribute: () => 'value',
hasAttribute: () => false,
};
const result = DropdownUtils.setDataValueIfSelected(null, '=', selected);
const result2 = DropdownUtils.setDataValueIfSelected(null, '!=', selected);
2018-10-09 18:03:09 +00:00
expect(result).toBe(true);
expect(result2).toBe(true);
2016-12-14 05:30:28 +00:00
});
2017-01-26 21:50:06 +00:00
it('returns false when dataValue does not exist', () => {
const selected = {
getAttribute: () => null,
};
const result = DropdownUtils.setDataValueIfSelected(null, '=', selected);
const result2 = DropdownUtils.setDataValueIfSelected(null, '!=', selected);
2018-10-09 18:03:09 +00:00
expect(result).toBe(false);
expect(result2).toBe(false);
});
});
2017-01-26 21:50:06 +00:00
describe('getInputSelectionPosition', () => {
describe('word with trailing spaces', () => {
const value = 'label:none ';
2017-01-26 21:50:06 +00:00
it('should return selectionStart when cursor is at the trailing space', () => {
2018-02-21 20:22:56 +00:00
const { left, right } = DropdownUtils.getInputSelectionPosition({
selectionStart: 11,
value,
2017-01-26 21:50:06 +00:00
});
expect(left).toBe(11);
expect(right).toBe(11);
});
2017-01-26 21:50:06 +00:00
it('should return input when cursor is at the start of input', () => {
2018-02-21 20:22:56 +00:00
const { left, right } = DropdownUtils.getInputSelectionPosition({
selectionStart: 0,
value,
2017-01-26 21:50:06 +00:00
});
expect(left).toBe(0);
expect(right).toBe(10);
});
2017-01-26 21:50:06 +00:00
it('should return input when cursor is at the middle of input', () => {
2018-02-21 20:22:56 +00:00
const { left, right } = DropdownUtils.getInputSelectionPosition({
selectionStart: 7,
value,
2017-01-26 21:50:06 +00:00
});
expect(left).toBe(0);
expect(right).toBe(10);
});
2017-01-26 21:50:06 +00:00
it('should return input when cursor is at the end of input', () => {
2018-02-21 20:22:56 +00:00
const { left, right } = DropdownUtils.getInputSelectionPosition({
selectionStart: 10,
value,
2017-01-26 21:50:06 +00:00
});
expect(left).toBe(0);
expect(right).toBe(10);
});
});
2017-01-26 21:50:06 +00:00
describe('multiple words', () => {
const value = 'label:~"Community Contribution"';
2017-01-26 21:50:06 +00:00
it('should return input when cursor is after the first word', () => {
2018-02-21 20:22:56 +00:00
const { left, right } = DropdownUtils.getInputSelectionPosition({
selectionStart: 17,
value,
2017-01-26 21:50:06 +00:00
});
expect(left).toBe(0);
expect(right).toBe(31);
});
2017-01-26 21:50:06 +00:00
it('should return input when cursor is before the second word', () => {
2018-02-21 20:22:56 +00:00
const { left, right } = DropdownUtils.getInputSelectionPosition({
selectionStart: 18,
value,
2017-01-26 21:50:06 +00:00
});
expect(left).toBe(0);
expect(right).toBe(31);
});
});
2017-01-26 21:50:06 +00:00
describe('incomplete multiple words', () => {
const value = 'label:~"Community Contribution';
2017-01-26 21:50:06 +00:00
it('should return entire input when cursor is at the start of input', () => {
2018-02-21 20:22:56 +00:00
const { left, right } = DropdownUtils.getInputSelectionPosition({
selectionStart: 0,
value,
2017-01-26 21:50:06 +00:00
});
expect(left).toBe(0);
expect(right).toBe(30);
});
2017-01-26 21:50:06 +00:00
it('should return entire input when cursor is at the end of input', () => {
2018-02-21 20:22:56 +00:00
const { left, right } = DropdownUtils.getInputSelectionPosition({
selectionStart: 30,
value,
2017-01-26 21:50:06 +00:00
});
expect(left).toBe(0);
expect(right).toBe(30);
2017-01-26 21:50:06 +00:00
});
});
2016-12-14 05:30:28 +00:00
});
describe('getSearchQuery', () => {
let authorToken;
beforeEach(() => {
loadFixtures(issueListFixture);
authorToken = FilteredSearchSpecHelper.createFilterVisualToken('author', '=', '@user');
const searchTermToken = FilteredSearchSpecHelper.createSearchVisualToken('search term');
const tokensContainer = document.querySelector('.tokens-container');
tokensContainer.appendChild(searchTermToken);
tokensContainer.appendChild(authorToken);
});
it('uses original value if present', () => {
const originalValue = 'original dance';
const valueContainer = authorToken.querySelector('.value-container');
valueContainer.dataset.originalValue = originalValue;
2018-02-21 20:22:56 +00:00
const searchQuery = DropdownUtils.getSearchQuery();
expect(searchQuery).toBe(' search term author:=original dance');
});
});
});