Commit 2adc83c1 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'upgrade-bootstrap-vue/specs-update-filter' into 'master'

Migrate filter specs to VTU

See merge request gitlab-org/gitlab!22804
parents 3f830e81 5b117d80
import Vue from 'vue'; import Vuex from 'vuex';
import component from 'ee/security_dashboard/components/filter.vue'; import Filter from 'ee/security_dashboard/components/filter.vue';
import createStore from 'ee/security_dashboard/store'; import createStore from 'ee/security_dashboard/store';
import { mountComponentWithStore } from 'helpers/vue_mount_component_helper'; import { mount, createLocalVue } from '@vue/test-utils';
import stubChildren from 'helpers/stub_children';
const localVue = createLocalVue();
localVue.use(Vuex);
describe('Filter component', () => { describe('Filter component', () => {
let vm; let wrapper;
let props;
let store; let store;
let Component;
const createWrapper = propsData => {
wrapper = mount(Filter, {
stubs: {
...stubChildren(Filter),
GlDropdown: false,
GlSearchBoxByType: false,
},
attachToDocument: true,
propsData,
store,
});
};
const findSearchInput = () =>
wrapper.find({ ref: 'searchBox' }).exists() && wrapper.find({ ref: 'searchBox' }).find('input');
const findDropdownToggle = () => wrapper.find('.dropdown-toggle');
const dropdownItemsCount = () => wrapper.findAll('.dropdown-item').length;
function isDropdownOpen() { function isDropdownOpen() {
const toggleButton = vm.$el.querySelector('.dropdown-toggle'); const toggleButton = findDropdownToggle();
return toggleButton.getAttribute('aria-expanded') === 'true'; return toggleButton.attributes('aria-expanded') === 'true';
} }
function setProjectsCount(count) { function setProjectsCount(count) {
...@@ -26,37 +46,33 @@ describe('Filter component', () => { ...@@ -26,37 +46,33 @@ describe('Filter component', () => {
}); });
} }
const findSearchInput = () => vm.$refs.searchBox && vm.$refs.searchBox.$el.querySelector('input');
beforeEach(() => { beforeEach(() => {
store = createStore(); store = createStore();
Component = Vue.extend(component);
}); });
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
}); });
describe('severity', () => { describe('severity', () => {
beforeEach(() => { beforeEach(() => {
props = { filterId: 'severity' }; createWrapper({ filterId: 'severity' });
vm = mountComponentWithStore(Component, { store, props });
}); });
it('should display all 8 severity options', () => { it('should display all 8 severity options', () => {
expect(vm.$el.querySelectorAll('.dropdown-item').length).toEqual(8); expect(dropdownItemsCount()).toEqual(8);
}); });
it('should display a check next to only the selected item', () => { it('should display a check next to only the selected item', () => {
expect(vm.$el.querySelectorAll('.dropdown-item .js-check').length).toEqual(1); expect(wrapper.findAll('.dropdown-item .js-check').length).toEqual(1);
}); });
it('should display "Severity" as the option name', () => { it('should display "Severity" as the option name', () => {
expect(vm.$el.querySelector('.js-name').textContent).toContain('Severity'); expect(wrapper.find('.js-name').text()).toContain('Severity');
}); });
it('should not have a search box', () => { it('should not have a search box', () => {
expect(findSearchInput()).not.toEqual(jasmine.any(HTMLElement)); expect(findSearchInput()).toBe(false);
}); });
it('should not be open', () => { it('should not be open', () => {
...@@ -65,27 +81,25 @@ describe('Filter component', () => { ...@@ -65,27 +81,25 @@ describe('Filter component', () => {
describe('when the dropdown is open', () => { describe('when the dropdown is open', () => {
beforeEach(done => { beforeEach(done => {
vm.$el.querySelector('.dropdown-toggle').click(); findDropdownToggle().trigger('click');
vm.$on('bv::dropdown::shown', () => { wrapper.vm.$root.$on('bv::dropdown::shown', () => {
done(); done();
}); });
}); });
it('should keep the menu open after clicking on an item', done => { it('should keep the menu open after clicking on an item', () => {
expect(isDropdownOpen()).toBe(true); expect(isDropdownOpen()).toBe(true);
vm.$el.querySelector('.dropdown-item').click(); wrapper.find('.dropdown-item').trigger('click');
vm.$nextTick(() => { return wrapper.vm.$nextTick().then(() => {
expect(isDropdownOpen()).toBe(true); expect(isDropdownOpen()).toBe(true);
done();
}); });
}); });
it('should close the menu when the close button is clicked', done => { it('should close the menu when the close button is clicked', () => {
expect(isDropdownOpen()).toBe(true); expect(isDropdownOpen()).toBe(true);
vm.$refs.close.click(); wrapper.find({ ref: 'close' }).trigger('click');
vm.$nextTick(() => { return wrapper.vm.$nextTick().then(() => {
expect(isDropdownOpen()).toBe(false); expect(isDropdownOpen()).toBe(false);
done();
}); });
}); });
}); });
...@@ -94,28 +108,26 @@ describe('Filter component', () => { ...@@ -94,28 +108,26 @@ describe('Filter component', () => {
describe('Project', () => { describe('Project', () => {
describe('when there are lots of projects', () => { describe('when there are lots of projects', () => {
const lots = 30; const lots = 30;
beforeEach(done => { beforeEach(() => {
props = { filterId: 'project_id', dashboardDocumentation: '' }; createWrapper({ filterId: 'project_id', dashboardDocumentation: '' });
vm = mountComponentWithStore(Component, { store, props });
setProjectsCount(lots); setProjectsCount(lots);
vm.$nextTick(done); return wrapper.vm.$nextTick();
}); });
it('should display a search box', () => { it('should display a search box', () => {
expect(findSearchInput()).toEqual(jasmine.any(HTMLElement)); expect(findSearchInput().exists()).toBe(true);
}); });
it(`should show all projects`, () => { it(`should show all projects`, () => {
expect(vm.$el.querySelectorAll('.dropdown-item').length).toBe(lots); expect(dropdownItemsCount()).toBe(lots);
}); });
it('should show only matching projects when a search term is entered', done => { it('should show only matching projects when a search term is entered', () => {
const input = findSearchInput(); const input = findSearchInput();
input.value = '0'; input.vm.$el.value = '0';
input.dispatchEvent(new Event('input')); input.vm.$el.dispatchEvent(new Event('input'));
vm.$nextTick(() => { return wrapper.vm.$nextTick().then(() => {
expect(vm.$el.querySelectorAll('.dropdown-item').length).toBe(3); expect(dropdownItemsCount()).toBe(3);
done();
}); });
}); });
}); });
......
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