2020-03-10 05:08:10 -04:00
|
|
|
import { nextTick } from 'vue';
|
2020-03-03 04:07:54 -05:00
|
|
|
import { mount } from '@vue/test-utils';
|
2020-12-01 16:09:29 -05:00
|
|
|
import { GlDropdown, GlLoadingIcon, GlDropdownSectionHeader } from '@gitlab/ui';
|
2020-03-03 04:07:54 -05:00
|
|
|
import { TEST_HOST } from 'spec/test_constants';
|
|
|
|
import BoardsSelector from '~/boards/components/boards_selector.vue';
|
|
|
|
import boardsStore from '~/boards/stores/boards_store';
|
|
|
|
|
|
|
|
const throttleDuration = 1;
|
|
|
|
|
|
|
|
function boardGenerator(n) {
|
2020-03-10 05:08:10 -04:00
|
|
|
return new Array(n).fill().map((board, index) => {
|
|
|
|
const id = `${index}`;
|
2020-03-03 04:07:54 -05:00
|
|
|
const name = `board${id}`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('BoardsSelector', () => {
|
|
|
|
let wrapper;
|
|
|
|
let allBoardsResponse;
|
|
|
|
let recentBoardsResponse;
|
|
|
|
const boards = boardGenerator(20);
|
|
|
|
const recentBoards = boardGenerator(5);
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const fillSearchBox = (filterTerm) => {
|
2020-03-03 04:07:54 -05:00
|
|
|
const searchBox = wrapper.find({ ref: 'searchBox' });
|
|
|
|
const searchBoxInput = searchBox.find('input');
|
|
|
|
searchBoxInput.setValue(filterTerm);
|
|
|
|
searchBoxInput.trigger('input');
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDropdownItems = () => wrapper.findAll('.js-dropdown-item');
|
2020-12-01 16:09:29 -05:00
|
|
|
const getDropdownHeaders = () => wrapper.findAll(GlDropdownSectionHeader);
|
2020-03-10 05:08:10 -04:00
|
|
|
const getLoadingIcon = () => wrapper.find(GlLoadingIcon);
|
2020-12-01 16:09:29 -05:00
|
|
|
const findDropdown = () => wrapper.find(GlDropdown);
|
2020-03-03 04:07:54 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-03-10 05:08:10 -04:00
|
|
|
const $apollo = {
|
|
|
|
queries: {
|
|
|
|
boards: {
|
|
|
|
loading: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-03-03 04:07:54 -05:00
|
|
|
boardsStore.setEndpoints({
|
|
|
|
boardsEndpoint: '',
|
|
|
|
recentBoardsEndpoint: '',
|
|
|
|
listsEndpoint: '',
|
|
|
|
bulkUpdatePath: '',
|
|
|
|
boardId: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
allBoardsResponse = Promise.resolve({
|
2020-03-10 05:08:10 -04:00
|
|
|
data: {
|
|
|
|
group: {
|
|
|
|
boards: {
|
2020-12-23 16:10:24 -05:00
|
|
|
edges: boards.map((board) => ({ node: board })),
|
2020-03-10 05:08:10 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
recentBoardsResponse = Promise.resolve({
|
|
|
|
data: recentBoards,
|
|
|
|
});
|
|
|
|
|
|
|
|
boardsStore.allBoards = jest.fn(() => allBoardsResponse);
|
|
|
|
boardsStore.recentBoards = jest.fn(() => recentBoardsResponse);
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
wrapper = mount(BoardsSelector, {
|
2020-03-03 04:07:54 -05:00
|
|
|
propsData: {
|
|
|
|
throttleDuration,
|
|
|
|
currentBoard: {
|
|
|
|
id: 1,
|
|
|
|
name: 'Development',
|
|
|
|
milestone_id: null,
|
|
|
|
weight: null,
|
|
|
|
assignee_id: null,
|
|
|
|
labels: [],
|
|
|
|
},
|
|
|
|
boardBaseUrl: `${TEST_HOST}/board/base/url`,
|
|
|
|
hasMissingBoards: false,
|
|
|
|
canAdminBoard: true,
|
|
|
|
multipleIssueBoardsAvailable: true,
|
|
|
|
labelsPath: `${TEST_HOST}/labels/path`,
|
2020-09-03 14:08:29 -04:00
|
|
|
labelsWebUrl: `${TEST_HOST}/labels`,
|
2020-03-03 04:07:54 -05:00
|
|
|
projectId: 42,
|
|
|
|
groupId: 19,
|
|
|
|
scopedIssueBoardFeatureEnabled: true,
|
|
|
|
weights: [],
|
|
|
|
},
|
2020-03-10 05:08:10 -04:00
|
|
|
mocks: { $apollo },
|
2020-12-25 13:10:00 -05:00
|
|
|
attachTo: document.body,
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
wrapper.vm.$apollo.addSmartQuery = jest.fn((_, options) => {
|
|
|
|
wrapper.setData({
|
|
|
|
[options.loadingKey]: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-03 04:07:54 -05:00
|
|
|
// Emits gl-dropdown show event to simulate the dropdown is opened at initialization time
|
2020-12-01 16:09:29 -05:00
|
|
|
findDropdown().vm.$emit('show');
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
|
|
|
});
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
describe('loading', () => {
|
|
|
|
// we are testing loading state, so don't resolve responses until after the tests
|
|
|
|
afterEach(() => {
|
|
|
|
return Promise.all([allBoardsResponse, recentBoardsResponse]).then(() => nextTick());
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
it('shows loading spinner', () => {
|
|
|
|
expect(getDropdownHeaders()).toHaveLength(0);
|
|
|
|
expect(getDropdownItems()).toHaveLength(0);
|
|
|
|
expect(getLoadingIcon().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2020-03-03 04:07:54 -05:00
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
describe('loaded', () => {
|
2020-12-16 04:10:26 -05:00
|
|
|
beforeEach(async () => {
|
|
|
|
await wrapper.setData({
|
|
|
|
loadingBoards: false,
|
|
|
|
});
|
2020-03-10 05:08:10 -04:00
|
|
|
return Promise.all([allBoardsResponse, recentBoardsResponse]).then(() => nextTick());
|
|
|
|
});
|
2020-03-03 04:07:54 -05:00
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
it('hides loading spinner', () => {
|
|
|
|
expect(getLoadingIcon().exists()).toBe(false);
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
describe('filtering', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper.setData({
|
|
|
|
boards,
|
|
|
|
});
|
2020-03-03 04:07:54 -05:00
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
return nextTick();
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
it('shows all boards without filtering', () => {
|
|
|
|
expect(getDropdownItems()).toHaveLength(boards.length + recentBoards.length);
|
|
|
|
});
|
2020-03-03 04:07:54 -05:00
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
it('shows only matching boards when filtering', () => {
|
|
|
|
const filterTerm = 'board1';
|
2020-12-23 16:10:24 -05:00
|
|
|
const expectedCount = boards.filter((board) => board.name.includes(filterTerm)).length;
|
2020-03-03 04:07:54 -05:00
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
fillSearchBox(filterTerm);
|
|
|
|
|
|
|
|
return nextTick().then(() => {
|
|
|
|
expect(getDropdownItems()).toHaveLength(expectedCount);
|
|
|
|
});
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
it('shows message if there are no matching boards', () => {
|
|
|
|
fillSearchBox('does not exist');
|
|
|
|
|
|
|
|
return nextTick().then(() => {
|
|
|
|
expect(getDropdownItems()).toHaveLength(0);
|
|
|
|
expect(wrapper.text().includes('No matching boards found')).toBe(true);
|
|
|
|
});
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
describe('recent boards section', () => {
|
|
|
|
it('shows only when boards are greater than 10', () => {
|
|
|
|
wrapper.setData({
|
|
|
|
boards,
|
|
|
|
});
|
|
|
|
|
|
|
|
return nextTick().then(() => {
|
|
|
|
expect(getDropdownHeaders()).toHaveLength(2);
|
|
|
|
});
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
it('does not show when boards are less than 10', () => {
|
|
|
|
wrapper.setData({
|
|
|
|
boards: boards.slice(0, 5),
|
|
|
|
});
|
|
|
|
|
|
|
|
return nextTick().then(() => {
|
|
|
|
expect(getDropdownHeaders()).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not show when recentBoards api returns empty array', () => {
|
|
|
|
wrapper.setData({
|
|
|
|
recentBoards: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
return nextTick().then(() => {
|
|
|
|
expect(getDropdownHeaders()).toHaveLength(0);
|
|
|
|
});
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
it('does not show when search is active', () => {
|
|
|
|
fillSearchBox('Random string');
|
2020-03-03 04:07:54 -05:00
|
|
|
|
2020-03-10 05:08:10 -04:00
|
|
|
return nextTick().then(() => {
|
|
|
|
expect(getDropdownHeaders()).toHaveLength(0);
|
|
|
|
});
|
2020-03-03 04:07:54 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|