2020-03-09 13:42:32 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2020-07-28 23:09:34 +05:30
|
|
|
import { GlDropdownItem, GlIcon } from '@gitlab/ui';
|
2020-03-09 13:42:32 +05:30
|
|
|
|
|
|
|
import DashboardsDropdown from '~/monitoring/components/dashboards_dropdown.vue';
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
import { dashboardGitResponse, selfMonitoringDashboardGitResponse } from '../mock_data';
|
2020-03-09 13:42:32 +05:30
|
|
|
|
|
|
|
const defaultBranch = 'master';
|
2020-07-28 23:09:34 +05:30
|
|
|
const modalId = 'duplicateDashboardModalId';
|
2020-05-24 23:13:21 +05:30
|
|
|
const starredDashboards = dashboardGitResponse.filter(({ starred }) => starred);
|
|
|
|
const notStarredDashboards = dashboardGitResponse.filter(({ starred }) => !starred);
|
2020-03-09 13:42:32 +05:30
|
|
|
|
|
|
|
describe('DashboardsDropdown', () => {
|
|
|
|
let wrapper;
|
2020-05-24 23:13:21 +05:30
|
|
|
let mockDashboards;
|
|
|
|
let mockSelectedDashboard;
|
|
|
|
|
|
|
|
function createComponent(props, opts = {}) {
|
|
|
|
const storeOpts = {
|
|
|
|
methods: {
|
|
|
|
duplicateSystemDashboard: jest.fn(),
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
allDashboards: () => mockDashboards,
|
|
|
|
selectedDashboard: () => mockSelectedDashboard,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return shallowMount(DashboardsDropdown, {
|
|
|
|
propsData: {
|
|
|
|
...props,
|
|
|
|
defaultBranch,
|
2020-07-28 23:09:34 +05:30
|
|
|
modalId,
|
2020-05-24 23:13:21 +05:30
|
|
|
},
|
|
|
|
sync: false,
|
|
|
|
...storeOpts,
|
|
|
|
...opts,
|
|
|
|
});
|
|
|
|
}
|
2020-03-09 13:42:32 +05:30
|
|
|
|
|
|
|
const findItems = () => wrapper.findAll(GlDropdownItem);
|
|
|
|
const findItemAt = i => wrapper.findAll(GlDropdownItem).at(i);
|
|
|
|
const findSearchInput = () => wrapper.find({ ref: 'monitorDashboardsDropdownSearch' });
|
|
|
|
const findNoItemsMsg = () => wrapper.find({ ref: 'monitorDashboardsDropdownMsg' });
|
2020-05-24 23:13:21 +05:30
|
|
|
const findStarredListDivider = () => wrapper.find({ ref: 'starredListDivider' });
|
2020-03-09 13:42:32 +05:30
|
|
|
const setSearchTerm = searchTerm => wrapper.setData({ searchTerm });
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
mockDashboards = dashboardGitResponse;
|
|
|
|
mockSelectedDashboard = null;
|
|
|
|
});
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
describe('when it receives dashboards data', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays an item for each dashboard', () => {
|
|
|
|
expect(findItems().length).toEqual(dashboardGitResponse.length);
|
|
|
|
});
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it('displays items with the dashboard display name, with starred dashboards first', () => {
|
|
|
|
expect(findItemAt(0).text()).toBe(starredDashboards[0].display_name);
|
|
|
|
expect(findItemAt(1).text()).toBe(notStarredDashboards[0].display_name);
|
|
|
|
expect(findItemAt(2).text()).toBe(notStarredDashboards[1].display_name);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays separator between starred and not starred dashboards', () => {
|
|
|
|
expect(findStarredListDivider().exists()).toBe(true);
|
2020-03-09 13:42:32 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('displays a search input', () => {
|
|
|
|
expect(findSearchInput().isVisible()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('hides no message text by default', () => {
|
|
|
|
expect(findNoItemsMsg().isVisible()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('filters dropdown items when searched for item exists in the list', () => {
|
|
|
|
const searchTerm = 'Default';
|
|
|
|
setSearchTerm(searchTerm);
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
return wrapper.vm.$nextTick().then(() => {
|
2020-03-09 13:42:32 +05:30
|
|
|
expect(findItems()).toHaveLength(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows no items found message when searched for item does not exists in the list', () => {
|
|
|
|
const searchTerm = 'does-not-exist';
|
|
|
|
setSearchTerm(searchTerm);
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
return wrapper.vm.$nextTick().then(() => {
|
2020-03-09 13:42:32 +05:30
|
|
|
expect(findNoItemsMsg().isVisible()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
describe('when the dashboard is missing a display name', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mockDashboards = dashboardGitResponse.map(d => ({ ...d, display_name: undefined }));
|
|
|
|
wrapper = createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays items with the dashboard path, with starred dashboards first', () => {
|
|
|
|
expect(findItemAt(0).text()).toBe(starredDashboards[0].path);
|
|
|
|
expect(findItemAt(1).text()).toBe(notStarredDashboards[0].path);
|
|
|
|
expect(findItemAt(2).text()).toBe(notStarredDashboards[1].path);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when it receives starred dashboards', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mockDashboards = starredDashboards;
|
|
|
|
wrapper = createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays an item for each dashboard', () => {
|
|
|
|
expect(findItems().length).toEqual(starredDashboards.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays a star icon', () => {
|
|
|
|
const star = findItemAt(0).find(GlIcon);
|
|
|
|
expect(star.exists()).toBe(true);
|
|
|
|
expect(star.attributes('name')).toBe('star');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays no separator between starred and not starred dashboards', () => {
|
|
|
|
expect(findStarredListDivider().exists()).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when it receives only not-starred dashboards', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mockDashboards = notStarredDashboards;
|
|
|
|
wrapper = createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays an item for each dashboard', () => {
|
|
|
|
expect(findItems().length).toEqual(notStarredDashboards.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays no star icon', () => {
|
|
|
|
const star = findItemAt(0).find(GlIcon);
|
|
|
|
expect(star.exists()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays no separator between starred and not starred dashboards', () => {
|
|
|
|
expect(findStarredListDivider().exists()).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
const duplicableCases = [
|
|
|
|
dashboardGitResponse[0],
|
|
|
|
dashboardGitResponse[2],
|
|
|
|
selfMonitoringDashboardGitResponse[0],
|
|
|
|
];
|
|
|
|
|
|
|
|
describe.each(duplicableCases)('when the selected dashboard can be duplicated', dashboard => {
|
2020-03-09 13:42:32 +05:30
|
|
|
let duplicateDashboardAction;
|
|
|
|
let modalDirective;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-07-28 23:09:34 +05:30
|
|
|
mockSelectedDashboard = dashboard;
|
2020-03-09 13:42:32 +05:30
|
|
|
modalDirective = jest.fn();
|
|
|
|
duplicateDashboardAction = jest.fn().mockResolvedValue();
|
|
|
|
|
|
|
|
wrapper = createComponent(
|
2020-05-24 23:13:21 +05:30
|
|
|
{},
|
2020-03-09 13:42:32 +05:30
|
|
|
{
|
|
|
|
directives: {
|
|
|
|
GlModal: modalDirective,
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
// Mock vuex actions
|
|
|
|
duplicateSystemDashboard: duplicateDashboardAction,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('displays a dropdown item for each dashboard', () => {
|
2020-03-09 13:42:32 +05:30
|
|
|
expect(findItems().length).toEqual(dashboardGitResponse.length + 1);
|
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('displays one "duplicate dashboard" dropdown item with a directive attached', () => {
|
|
|
|
const item = wrapper.findAll('[data-testid="duplicateDashboardItem"]');
|
2020-03-09 13:42:32 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(item.length).toBe(1);
|
|
|
|
});
|
2020-03-09 13:42:32 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('"duplicate dashboard" dropdown item directive works', () => {
|
|
|
|
const item = wrapper.find('[data-testid="duplicateDashboardItem"]');
|
2020-03-09 13:42:32 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
item.trigger('click');
|
2020-03-09 13:42:32 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
return wrapper.vm.$nextTick().then(() => {
|
|
|
|
expect(modalDirective).toHaveBeenCalled();
|
2020-03-09 13:42:32 +05:30
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
2020-03-09 13:42:32 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('id is correct, as the value of modal directive binding matches modal id', () => {
|
|
|
|
expect(modalDirective).toHaveBeenCalledTimes(1);
|
2020-03-09 13:42:32 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
// Binding's second argument contains the modal id
|
|
|
|
expect(modalDirective.mock.calls[0][1]).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
value: modalId,
|
|
|
|
}),
|
|
|
|
);
|
2020-03-09 13:42:32 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
const nonDuplicableCases = [dashboardGitResponse[1], selfMonitoringDashboardGitResponse[1]];
|
2020-03-09 13:42:32 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
describe.each(nonDuplicableCases)(
|
|
|
|
'when the selected dashboard can not be duplicated',
|
|
|
|
dashboard => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mockSelectedDashboard = dashboard;
|
|
|
|
|
|
|
|
wrapper = createComponent();
|
2020-03-09 13:42:32 +05:30
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('displays a dropdown list item for each dashboard, but no list item for "duplicate dashboard"', () => {
|
|
|
|
const item = wrapper.findAll('[data-testid="duplicateDashboardItem"]');
|
2020-03-09 13:42:32 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(findItems()).toHaveLength(dashboardGitResponse.length);
|
|
|
|
expect(item.length).toBe(0);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2020-03-09 13:42:32 +05:30
|
|
|
|
|
|
|
describe('when a dashboard gets selected by the user', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
findItemAt(1).vm.$emit('click');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits a "selectDashboard" event', () => {
|
|
|
|
expect(wrapper.emitted().selectDashboard).toBeTruthy();
|
|
|
|
});
|
|
|
|
it('emits a "selectDashboard" event with dashboard information', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(wrapper.emitted().selectDashboard[0]).toEqual([dashboardGitResponse[0]]);
|
2020-03-09 13:42:32 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|