2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-01-18 03:02:21 -05:00
|
|
|
import CreateItemDropdown from '~/create_item_dropdown';
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
const DROPDOWN_ITEM_DATA = [
|
|
|
|
{
|
|
|
|
title: 'one',
|
|
|
|
id: 'one',
|
|
|
|
text: 'one',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'two',
|
|
|
|
id: 'two',
|
|
|
|
text: 'two',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'three',
|
|
|
|
id: 'three',
|
|
|
|
text: 'three',
|
|
|
|
},
|
2022-02-03 19:13:53 -05:00
|
|
|
{
|
|
|
|
title: '<b>four</b>title',
|
|
|
|
id: '<b>four</b>id',
|
|
|
|
text: '<b>four</b>text',
|
|
|
|
},
|
2018-10-17 03:13:26 -04:00
|
|
|
];
|
2018-01-18 03:02:21 -05:00
|
|
|
|
|
|
|
describe('CreateItemDropdown', () => {
|
|
|
|
let $wrapperEl;
|
2018-01-25 01:55:49 -05:00
|
|
|
let createItemDropdown;
|
|
|
|
|
|
|
|
function createItemAndClearInput(text) {
|
|
|
|
// Filter for the new item
|
2020-12-23 07:10:26 -05:00
|
|
|
$wrapperEl.find('.dropdown-input-field').val(text).trigger('input');
|
2018-01-25 01:55:49 -05:00
|
|
|
|
|
|
|
// Create the new item
|
|
|
|
const $createButton = $wrapperEl.find('.js-dropdown-create-new-item');
|
|
|
|
$createButton.click();
|
|
|
|
|
|
|
|
// Clear out the filter
|
2020-12-23 07:10:26 -05:00
|
|
|
$wrapperEl.find('.dropdown-input-field').val('').trigger('input');
|
2018-01-25 01:55:49 -05:00
|
|
|
}
|
2018-01-18 03:02:21 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-03-26 12:03:28 -04:00
|
|
|
loadFixtures('static/create_item_dropdown.html');
|
2018-01-18 03:02:21 -05:00
|
|
|
$wrapperEl = $('.js-create-item-dropdown-fixture-root');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
$wrapperEl.remove();
|
|
|
|
});
|
|
|
|
|
2018-01-25 01:55:49 -05:00
|
|
|
describe('items', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createItemDropdown = new CreateItemDropdown({
|
|
|
|
$dropdown: $wrapperEl.find('.js-dropdown-menu-toggle'),
|
|
|
|
defaultToggleLabel: 'All variables',
|
|
|
|
fieldName: 'variable[environment]',
|
|
|
|
getData: (term, callback) => {
|
|
|
|
callback(DROPDOWN_ITEM_DATA);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have a dropdown item for each piece of data', () => {
|
|
|
|
// Get the data in the dropdown
|
|
|
|
$('.js-dropdown-menu-toggle').click();
|
2018-01-18 03:02:21 -05:00
|
|
|
|
2018-01-25 01:55:49 -05:00
|
|
|
const $itemEls = $wrapperEl.find('.js-dropdown-content a');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-01-25 01:55:49 -05:00
|
|
|
expect($itemEls.length).toEqual(DROPDOWN_ITEM_DATA.length);
|
2022-02-03 19:13:53 -05:00
|
|
|
|
|
|
|
DROPDOWN_ITEM_DATA.forEach((dataItem, i) => {
|
|
|
|
expect($($itemEls[i]).text()).toEqual(dataItem.text);
|
|
|
|
});
|
2018-01-25 01:55:49 -05:00
|
|
|
});
|
2018-01-18 03:02:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('created items', () => {
|
|
|
|
const NEW_ITEM_TEXT = 'foobarbaz';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-01-25 01:55:49 -05:00
|
|
|
createItemDropdown = new CreateItemDropdown({
|
|
|
|
$dropdown: $wrapperEl.find('.js-dropdown-menu-toggle'),
|
|
|
|
defaultToggleLabel: 'All variables',
|
|
|
|
fieldName: 'variable[environment]',
|
|
|
|
getData: (term, callback) => {
|
|
|
|
callback(DROPDOWN_ITEM_DATA);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-01-18 03:02:21 -05:00
|
|
|
// Open the dropdown
|
|
|
|
$('.js-dropdown-menu-toggle').click();
|
|
|
|
|
|
|
|
// Filter for the new item
|
2020-12-23 07:10:26 -05:00
|
|
|
$wrapperEl.find('.dropdown-input-field').val(NEW_ITEM_TEXT).trigger('input');
|
2018-01-18 03:02:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('create new item button should include the filter text', () => {
|
|
|
|
expect($wrapperEl.find('.js-dropdown-create-new-item code').text()).toEqual(NEW_ITEM_TEXT);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update the dropdown with the newly created item', () => {
|
|
|
|
// Create the new item
|
|
|
|
const $createButton = $wrapperEl.find('.js-dropdown-create-new-item');
|
|
|
|
$createButton.click();
|
|
|
|
|
|
|
|
expect($wrapperEl.find('.dropdown-toggle-text').text()).toEqual(NEW_ITEM_TEXT);
|
|
|
|
expect($wrapperEl.find('input[name="variable[environment]"]').val()).toEqual(NEW_ITEM_TEXT);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should include newly created item in dropdown list', () => {
|
|
|
|
createItemAndClearInput(NEW_ITEM_TEXT);
|
|
|
|
|
|
|
|
const $itemEls = $wrapperEl.find('.js-dropdown-content a');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-01-18 03:02:21 -05:00
|
|
|
expect($itemEls.length).toEqual(1 + DROPDOWN_ITEM_DATA.length);
|
|
|
|
expect($($itemEls.get(DROPDOWN_ITEM_DATA.length)).text()).toEqual(NEW_ITEM_TEXT);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not duplicate an item when trying to create an existing item', () => {
|
|
|
|
createItemAndClearInput(DROPDOWN_ITEM_DATA[0].text);
|
|
|
|
|
|
|
|
const $itemEls = $wrapperEl.find('.js-dropdown-content a');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-01-18 03:02:21 -05:00
|
|
|
expect($itemEls.length).toEqual(DROPDOWN_ITEM_DATA.length);
|
|
|
|
});
|
|
|
|
});
|
2018-01-25 01:55:49 -05:00
|
|
|
|
|
|
|
describe('clearDropdown()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createItemDropdown = new CreateItemDropdown({
|
|
|
|
$dropdown: $wrapperEl.find('.js-dropdown-menu-toggle'),
|
|
|
|
defaultToggleLabel: 'All variables',
|
|
|
|
fieldName: 'variable[environment]',
|
|
|
|
getData: (term, callback) => {
|
|
|
|
callback(DROPDOWN_ITEM_DATA);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should clear all data and filter input', () => {
|
|
|
|
const filterInput = $wrapperEl.find('.dropdown-input-field');
|
|
|
|
|
|
|
|
// Get the data in the dropdown
|
|
|
|
$('.js-dropdown-menu-toggle').click();
|
|
|
|
|
|
|
|
// Filter for an item
|
2018-10-17 03:13:26 -04:00
|
|
|
filterInput.val('one').trigger('input');
|
2018-01-25 01:55:49 -05:00
|
|
|
|
|
|
|
const $itemElsAfterFilter = $wrapperEl.find('.js-dropdown-content a');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-01-25 01:55:49 -05:00
|
|
|
expect($itemElsAfterFilter.length).toEqual(1);
|
|
|
|
|
|
|
|
createItemDropdown.clearDropdown();
|
|
|
|
|
|
|
|
const $itemElsAfterClear = $wrapperEl.find('.js-dropdown-content a');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-01-25 01:55:49 -05:00
|
|
|
expect($itemElsAfterClear.length).toEqual(0);
|
|
|
|
expect(filterInput.val()).toEqual('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createNewItemFromValue option', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createItemDropdown = new CreateItemDropdown({
|
|
|
|
$dropdown: $wrapperEl.find('.js-dropdown-menu-toggle'),
|
|
|
|
defaultToggleLabel: 'All variables',
|
|
|
|
fieldName: 'variable[environment]',
|
|
|
|
getData: (term, callback) => {
|
|
|
|
callback(DROPDOWN_ITEM_DATA);
|
|
|
|
},
|
2020-12-23 16:10:24 -05:00
|
|
|
createNewItemFromValue: (newValue) => ({
|
2018-01-25 01:55:49 -05:00
|
|
|
title: `${newValue}-title`,
|
|
|
|
id: `${newValue}-id`,
|
|
|
|
text: `${newValue}-text`,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('all items go through createNewItemFromValue', () => {
|
|
|
|
// Get the data in the dropdown
|
|
|
|
$('.js-dropdown-menu-toggle').click();
|
|
|
|
|
|
|
|
createItemAndClearInput('new-item');
|
|
|
|
|
|
|
|
const $itemEls = $wrapperEl.find('.js-dropdown-content a');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-01-25 01:55:49 -05:00
|
|
|
expect($itemEls.length).toEqual(1 + DROPDOWN_ITEM_DATA.length);
|
2022-02-03 19:13:53 -05:00
|
|
|
expect($($itemEls[DROPDOWN_ITEM_DATA.length]).text()).toEqual('new-item-text');
|
2018-01-25 01:55:49 -05:00
|
|
|
expect($wrapperEl.find('.dropdown-toggle-text').text()).toEqual('new-item-title');
|
|
|
|
});
|
|
|
|
});
|
2018-01-18 03:02:21 -05:00
|
|
|
});
|