debian-mirror-gitlab/spec/frontend/filtered_search/dropdown_utils_spec.js

375 lines
11 KiB
JavaScript
Raw Normal View History

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