Commit 2baf662c authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Merge branch 'tz-load-additional-css-for-licence-page' into 'master'

Additional CSS Loading for 3 more Select2 Usages

See merge request gitlab-org/gitlab!48053
parents 4090e26e 06ab152f
<script> <script>
import $ from 'jquery'; import $ from 'jquery';
import 'select2'; import 'select2';
import { loadCSSFile } from '~/lib/utils/css_utils';
export default { export default {
// False positive i18n lint: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/26 // False positive i18n lint: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/26
...@@ -20,10 +21,14 @@ export default { ...@@ -20,10 +21,14 @@ export default {
}, },
mounted() { mounted() {
$(this.$refs.dropdownInput) loadCSSFile(gon.select2_css_path)
.val(this.value) .then(() => {
.select2(this.options) $(this.$refs.dropdownInput)
.on('change', event => this.$emit('input', event.target.value)); .val(this.value)
.select2(this.options)
.on('change', event => this.$emit('input', event.target.value));
})
.catch(() => {});
}, },
beforeDestroy() { beforeDestroy() {
......
...@@ -3,6 +3,7 @@ import $ from 'jquery'; ...@@ -3,6 +3,7 @@ import $ from 'jquery';
import 'select2/select2'; import 'select2/select2';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import Api from 'ee/api'; import Api from 'ee/api';
import { loadCSSFile } from '~/lib/utils/css_utils';
import { __ } from '~/locale'; import { __ } from '~/locale';
const anyBranch = { const anyBranch = {
...@@ -54,27 +55,36 @@ export default { ...@@ -54,27 +55,36 @@ export default {
const $modal = $('#project-settings-approvals-create-modal .modal-content'); const $modal = $('#project-settings-approvals-create-modal .modal-content');
this.$input = $(this.$refs.input); this.$input = $(this.$refs.input);
this.$input loadCSSFile(gon.select2_css_path)
.select2({ .then(() => {
minimumInputLength: 0, this.$input
multiple: false, .select2({
closeOnSelect: false, minimumInputLength: 0,
formatResult, multiple: false,
formatSelection, closeOnSelect: false,
initSelection: (element, callback) => this.initialOption(element, callback), formatResult,
query: debounce(({ term, callback }) => this.fetchBranches(term).then(callback), 250), formatSelection,
id: ({ type, id }) => `${type}${id}`, initSelection: (element, callback) => this.initialOption(element, callback),
query: debounce(({ term, callback }) => {
// eslint-disable-next-line promise/no-nesting
this.fetchBranches(term)
.then(callback)
.catch(() => {});
}, 250),
id: ({ type, id }) => `${type}${id}`,
})
.on('change', e => this.onChange(e))
.on('select2-open', () => {
// https://stackoverflow.com/questions/18487056/select2-doesnt-work-when-embedded-in-a-bootstrap-modal
// Ensure search feature works in modal
// (known issue with our current select2 version, solved in version 4 with "dropdownParent")
$modal.removeAttr('tabindex', '-1');
})
.on('select2-close', () => {
$modal.attr('tabindex', '-1');
});
}) })
.on('change', e => this.onChange(e)) .catch(() => {});
.on('select2-open', () => {
// https://stackoverflow.com/questions/18487056/select2-doesnt-work-when-embedded-in-a-bootstrap-modal
// Ensure search feature works in modal
// (known issue with our current select2 version, solved in version 4 with "dropdownParent")
$modal.removeAttr('tabindex', '-1');
})
.on('select2-close', () => {
$modal.attr('tabindex', '-1');
});
}, },
beforeDestroy() { beforeDestroy() {
this.$input.select2('destroy'); this.$input.select2('destroy');
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
import $ from 'jquery'; import $ from 'jquery';
import select2 from 'select2/select2'; import select2 from 'select2/select2';
import { loadCSSFile } from '~/lib/utils/css_utils';
export default { export default {
name: 'AddLicenseFormDropdown', name: 'AddLicenseFormDropdown',
...@@ -22,21 +23,25 @@ export default { ...@@ -22,21 +23,25 @@ export default {
}, },
}, },
mounted() { mounted() {
$(this.$refs.dropdownInput) loadCSSFile(gon.select2_css_path)
.val(this.value) .then(() => {
.select2({ $(this.$refs.dropdownInput)
allowClear: true, .val(this.value)
placeholder: this.placeholder, .select2({
createSearchChoice: term => ({ id: term, text: term }), allowClear: true,
createSearchChoicePosition: 'bottom', placeholder: this.placeholder,
data: this.knownLicenses.map(license => ({ createSearchChoice: term => ({ id: term, text: term }),
id: license, createSearchChoicePosition: 'bottom',
text: license, data: this.knownLicenses.map(license => ({
})), id: license,
text: license,
})),
})
.on('change', e => {
this.$emit('input', e.target.value);
});
}) })
.on('change', e => { .catch(() => {});
this.$emit('input', e.target.value);
});
}, },
beforeDestroy() { beforeDestroy() {
$(this.$refs.dropdownInput).select2('destroy'); $(this.$refs.dropdownInput).select2('destroy');
......
import { shallowMount, createLocalVue } from '@vue/test-utils'; import { shallowMount, createLocalVue } from '@vue/test-utils';
import $ from 'jquery'; import $ from 'jquery';
import Vuex from 'vuex'; import Vuex from 'vuex';
import waitForPromises from 'helpers/wait_for_promises';
import Api from 'ee/api'; import Api from 'ee/api';
import BranchesSelect from 'ee/approvals/components/branches_select.vue'; import BranchesSelect from 'ee/approvals/components/branches_select.vue';
...@@ -22,7 +23,7 @@ describe('Branches Select', () => { ...@@ -22,7 +23,7 @@ describe('Branches Select', () => {
let store; let store;
let $input; let $input;
const createComponent = (props = {}) => { const createComponent = async (props = {}) => {
wrapper = shallowMount(localVue.extend(BranchesSelect), { wrapper = shallowMount(localVue.extend(BranchesSelect), {
propsData: { propsData: {
projectId: '1', projectId: '1',
...@@ -33,6 +34,8 @@ describe('Branches Select', () => { ...@@ -33,6 +34,8 @@ describe('Branches Select', () => {
attachToDocument: true, attachToDocument: true,
}); });
await waitForPromises();
$input = $(wrapper.vm.$refs.input); $input = $(wrapper.vm.$refs.input);
}; };
...@@ -50,16 +53,16 @@ describe('Branches Select', () => { ...@@ -50,16 +53,16 @@ describe('Branches Select', () => {
wrapper.destroy(); wrapper.destroy();
}); });
it('renders select2 input', () => { it('renders select2 input', async () => {
expect(select2Container()).toBe(null); expect(select2Container()).toBe(null);
createComponent(); await createComponent();
expect(select2Container()).not.toBe(null); expect(select2Container()).not.toBe(null);
}); });
it('displays all the protected branches and any branch', done => { it('displays all the protected branches and any branch', async done => {
createComponent(); await createComponent();
waitForEvent($input, 'select2-loaded') waitForEvent($input, 'select2-loaded')
.then(() => { .then(() => {
const nodeList = select2DropdownOptions(); const nodeList = select2DropdownOptions();
...@@ -74,7 +77,7 @@ describe('Branches Select', () => { ...@@ -74,7 +77,7 @@ describe('Branches Select', () => {
describe('with search term', () => { describe('with search term', () => {
beforeEach(() => { beforeEach(() => {
createComponent(); return createComponent();
}); });
it('fetches protected branches with search term', done => { it('fetches protected branches with search term', done => {
...@@ -116,8 +119,8 @@ describe('Branches Select', () => { ...@@ -116,8 +119,8 @@ describe('Branches Select', () => {
}); });
}); });
it('emits input when data changes', done => { it('emits input when data changes', async done => {
createComponent(); await createComponent();
const selectedIndex = 1; const selectedIndex = 1;
const selectedId = TEST_BRANCHES_SELECTIONS[selectedIndex].id; const selectedId = TEST_BRANCHES_SELECTIONS[selectedIndex].id;
......
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import $ from 'jquery'; import $ from 'jquery';
import waitForPromises from 'helpers/wait_for_promises';
import Dropdown from 'ee/vue_shared/license_compliance/components/add_license_form_dropdown.vue'; import Dropdown from 'ee/vue_shared/license_compliance/components/add_license_form_dropdown.vue';
let vm; let vm;
...@@ -7,8 +8,9 @@ let wrapper; ...@@ -7,8 +8,9 @@ let wrapper;
const KNOWN_LICENSES = ['AGPL-1.0', 'AGPL-3.0', 'Apache 2.0', 'BSD']; const KNOWN_LICENSES = ['AGPL-1.0', 'AGPL-3.0', 'Apache 2.0', 'BSD'];
const createComponent = (props = {}) => { const createComponent = async (props = {}) => {
wrapper = shallowMount(Dropdown, { propsData: { knownLicenses: KNOWN_LICENSES, ...props } }); wrapper = shallowMount(Dropdown, { propsData: { knownLicenses: KNOWN_LICENSES, ...props } });
await waitForPromises();
vm = wrapper.vm; vm = wrapper.vm;
}; };
...@@ -18,8 +20,8 @@ describe('AddLicenseFormDropdown', () => { ...@@ -18,8 +20,8 @@ describe('AddLicenseFormDropdown', () => {
wrapper.destroy(); wrapper.destroy();
}); });
it('emits `input` invent on change', () => { it('emits `input` invent on change', async () => {
createComponent(); await createComponent();
jest.spyOn(vm, '$emit').mockImplementation(() => {}); jest.spyOn(vm, '$emit').mockImplementation(() => {});
...@@ -30,24 +32,24 @@ describe('AddLicenseFormDropdown', () => { ...@@ -30,24 +32,24 @@ describe('AddLicenseFormDropdown', () => {
expect(vm.$emit).toHaveBeenCalledWith('input', 'LGPL'); expect(vm.$emit).toHaveBeenCalledWith('input', 'LGPL');
}); });
it('sets the placeholder appropriately', () => { it('sets the placeholder appropriately', async () => {
const placeholder = 'Select a license'; const placeholder = 'Select a license';
createComponent({ placeholder }); await createComponent({ placeholder });
const dropdownContainer = $(vm.$el).select2('container')[0]; const dropdownContainer = $(vm.$el).select2('container')[0];
expect(dropdownContainer.textContent).toContain(placeholder); expect(dropdownContainer.textContent).toContain(placeholder);
}); });
it('sets the initial value correctly', () => { it('sets the initial value correctly', async () => {
const value = 'AWESOME_LICENSE'; const value = 'AWESOME_LICENSE';
createComponent({ value }); await createComponent({ value });
expect(vm.$el.value).toContain(value); expect(vm.$el.value).toContain(value);
}); });
it('shows all defined licenses', done => { it('shows all defined licenses', async done => {
createComponent(); await createComponent();
const element = $(vm.$el); const element = $(vm.$el);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment