Skip to content
代码片段 群组 项目
未验证 提交 b00939db 编辑于 作者: Alexander Turinske's avatar Alexander Turinske 提交者: GitLab
浏览文件

Update tests for vue 3

- remove spy on vm methods
- remove check of vm properties
上级 ce95836b
No related branches found
No related tags found
无相关合并请求
...@@ -91,6 +91,7 @@ export default { ...@@ -91,6 +91,7 @@ export default {
v-bind="treeRootOptions" v-bind="treeRootOptions"
class="list-unstyled related-items-list tree-root gl-pb-0 gl-pt-0" class="list-unstyled related-items-list tree-root gl-pb-0 gl-pt-0"
:move="onMove" :move="onMove"
data-testid="tree-root"
@start="handleDragOnStart" @start="handleDragOnStart"
@end="handleDragOnEnd" @end="handleDragOnEnd"
> >
......
import { GlButton } from '@gitlab/ui'; import { GlButton } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue'; import Vue, { nextTick } from 'vue';
import Draggable from 'vuedraggable'; import Draggable from 'vuedraggable';
// eslint-disable-next-line no-restricted-imports // eslint-disable-next-line no-restricted-imports
import Vuex from 'vuex'; import Vuex from 'vuex';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { ESC_KEY_CODE } from '~/lib/utils/keycodes'; import { ESC_KEY_CODE } from '~/lib/utils/keycodes';
import { DRAG_DELAY } from '~/sortable/constants'; import { DRAG_DELAY } from '~/sortable/constants';
import TreeRoot from 'ee/related_items_tree/components/tree_root.vue'; import TreeRoot from 'ee/related_items_tree/components/tree_root.vue';
import { treeItemChevronBtnClassName } from 'ee/related_items_tree/constants'; import { treeItemChevronBtnClassName } from 'ee/related_items_tree/constants';
import createDefaultStore from 'ee/related_items_tree/store'; import createDefaultStore from 'ee/related_items_tree/store';
import * as epicUtils from 'ee/related_items_tree/utils/epic_utils'; import * as epicUtils from 'ee/related_items_tree/utils/epic_utils';
import { gqClient } from 'ee/related_items_tree/utils/epic_utils';
import { import {
mockQueryResponse, mockQueryResponse,
mockInitialConfig, mockInitialConfig,
...@@ -21,51 +22,54 @@ import { ...@@ -21,51 +22,54 @@ import {
const { epic } = mockQueryResponse.data.group; const { epic } = mockQueryResponse.data.group;
Vue.use(Vuex); Vue.use(Vuex);
let store;
const createComponent = ({
parentItem = mockParentItem,
epicPageInfo = epic.children.pageInfo,
issuesPageInfo = epic.issues.pageInfo,
} = {}) => {
store = createDefaultStore();
const children = epicUtils.processQueryResponse(mockQueryResponse.data.group);
store.dispatch('setInitialParentItem', mockParentItem);
store.dispatch('setInitialConfig', mockInitialConfig);
store.dispatch('setItemChildrenFlags', {
isSubItem: false,
children,
});
store.dispatch('setEpicPageInfo', { describe('RelatedItemsTree', () => {
parentItem, let wrapper;
pageInfo: epicPageInfo, let store;
});
const createComponent = ({
parentItem = mockParentItem,
epicPageInfo = epic.children.pageInfo,
issuesPageInfo = epic.issues.pageInfo,
} = {}) => {
store = createDefaultStore();
const children = epicUtils.processQueryResponse(mockQueryResponse.data.group);
store.dispatch('setInitialParentItem', mockParentItem);
store.dispatch('setInitialConfig', mockInitialConfig);
store.dispatch('setItemChildrenFlags', {
isSubItem: false,
children,
});
store.dispatch('setIssuePageInfo', { store.dispatch('setEpicPageInfo', {
parentItem, parentItem,
pageInfo: issuesPageInfo, pageInfo: epicPageInfo,
}); });
return shallowMount(TreeRoot, { store.dispatch('setIssuePageInfo', {
store,
stubs: {
'tree-item': true,
},
propsData: {
parentItem, parentItem,
children, pageInfo: issuesPageInfo,
}, });
});
};
describe('RelatedItemsTree', () => { wrapper = shallowMountExtended(TreeRoot, {
describe('TreeRoot', () => { store,
let wrapper; stubs: {
'tree-item': true,
},
propsData: {
parentItem,
children,
},
});
};
const findTreeRoot = () => wrapper.findByTestId('tree-root');
const findMoreChildren = () => findTreeRoot().find('li');
describe('TreeRoot', () => {
beforeEach(() => { beforeEach(() => {
wrapper = createComponent(); createComponent();
}); });
describe('mixins', () => { describe('mixins', () => {
...@@ -87,46 +91,48 @@ describe('RelatedItemsTree', () => { ...@@ -87,46 +91,48 @@ describe('RelatedItemsTree', () => {
describe('computed', () => { describe('computed', () => {
describe('treeRootWrapper', () => { describe('treeRootWrapper', () => {
it('should return Draggable reference when userSignedIn prop is true', () => { it('should return Draggable reference when userSignedIn prop is true', () => {
expect(wrapper.vm.treeRootWrapper).toBe(Draggable); expect(findTreeRoot().element.tagName).toBe('DRAGGABLE-STUB');
}); });
it('should return string "ul" when userSignedIn prop is false', () => { it('should return string "ul" when userSignedIn prop is false', async () => {
store.dispatch('setInitialConfig', { await store.dispatch('setInitialConfig', {
...mockInitialConfig, ...mockInitialConfig,
userSignedIn: false, userSignedIn: false,
}); });
expect(wrapper.vm.treeRootWrapper).toBe('ul'); expect(findTreeRoot().element.tagName).toBe('UL');
}); });
}); });
describe('treeRootOptions', () => { describe('treeRootOptions', () => {
it('should return object containing Vue.Draggable config extended from `defaultSortableOptions` when userSignedIn prop is true', () => { it('should return object containing Vue.Draggable config extended from `defaultSortableOptions` when userSignedIn prop is true', () => {
expect(wrapper.vm.treeRootOptions).toEqual( expect(findTreeRoot().attributes()).toEqual(
expect.objectContaining({ expect.objectContaining({
animation: 200, animation: '200',
forceFallback: true, delay: '100',
fallbackClass: 'is-dragging', forcefallback: 'true',
fallbackOnBody: false, fallbackclass: 'is-dragging',
ghostClass: 'is-ghost', ghostclass: 'is-ghost',
group: 'gl-new-card-body', group: 'gl-new-card-body',
tag: 'ul', tag: 'ul',
'ghost-class': 'tree-item-drag-active',
'data-parent-reference': mockParentItem.reference, 'data-parent-reference': mockParentItem.reference,
'data-parent-id': mockParentItem.id, 'data-parent-id': mockParentItem.id,
value: wrapper.vm.children,
filter: `.${treeItemChevronBtnClassName}`, filter: `.${treeItemChevronBtnClassName}`,
}), }),
); );
}); });
it('should return an empty object when userSignedIn prop is false', () => { it('should return an empty object when userSignedIn prop is false', async () => {
store.dispatch('setInitialConfig', { await store.dispatch('setInitialConfig', {
...mockInitialConfig, ...mockInitialConfig,
userSignedIn: false, userSignedIn: false,
}); });
expect(wrapper.vm.treeRootOptions).toEqual(expect.objectContaining({})); expect(Object.keys(findTreeRoot().attributes())).toEqual([
'move',
'data-testid',
'class',
]);
}); });
}); });
}); });
...@@ -374,49 +380,7 @@ describe('RelatedItemsTree', () => { ...@@ -374,49 +380,7 @@ describe('RelatedItemsTree', () => {
}); });
}); });
describe('computed', () => {
describe('hasMoreChildren', () => {
it('returns `true` when either `hasMoreEpics` or `hasMoreIssues` is true', () => {
expect(wrapper.vm.hasMoreChildren).toBe(true);
});
it('returns `false` when both `hasMoreEpics` and `hasMoreIssues` is false', () => {
const wrapperNoMoreChild = createComponent({
epicPageInfo: {
hasNextPage: false,
endCursor: 'abc',
},
issuesPageInfo: {
hasNextPage: false,
endCursor: 'def',
},
});
expect(wrapperNoMoreChild.vm.hasMoreChildren).toBe(false);
wrapperNoMoreChild.destroy();
});
});
});
describe('methods', () => { describe('methods', () => {
describe('handleShowMoreClick', () => {
it('sets `fetchInProgress` to true and calls `fetchNextPageItems` action with parentItem as param', () => {
jest
.spyOn(wrapper.vm, 'fetchNextPageItems')
.mockImplementation(() => new Promise(() => {}));
wrapper.vm.handleShowMoreClick();
expect(wrapper.vm.fetchInProgress).toBe(true);
expect(wrapper.vm.fetchNextPageItems).toHaveBeenCalledWith(
expect.objectContaining({
parentItem: mockParentItem,
}),
);
});
});
describe('onMove', () => { describe('onMove', () => {
let mockEvt; let mockEvt;
let mockOriginalEvt; let mockOriginalEvt;
...@@ -482,6 +446,27 @@ describe('RelatedItemsTree', () => { ...@@ -482,6 +446,27 @@ describe('RelatedItemsTree', () => {
}); });
describe('template', () => { describe('template', () => {
describe('Children', () => {
it('displays children when either when either `hasMoreEpics` or `hasMoreIssues` are true', () => {
expect(findMoreChildren().exists()).toBe(true);
});
it('does not display children when both `hasMoreEpics` and `hasMoreIssues` are false', () => {
createComponent({
epicPageInfo: {
hasNextPage: false,
endCursor: 'abc',
},
issuesPageInfo: {
hasNextPage: false,
endCursor: 'def',
},
});
expect(findMoreChildren().exists()).toBe(false);
});
});
it('renders tree item component', () => { it('renders tree item component', () => {
expect(wrapper.html()).toContain('tree-item-stub'); expect(wrapper.html()).toContain('tree-item-stub');
}); });
...@@ -490,12 +475,22 @@ describe('RelatedItemsTree', () => { ...@@ -490,12 +475,22 @@ describe('RelatedItemsTree', () => {
expect(wrapper.findComponent(GlButton).text()).toBe('Show more'); expect(wrapper.findComponent(GlButton).text()).toBe('Show more');
}); });
it('calls `handleShowMoreClick` when `Show more` link is clicked', () => { it('fetches more when `Show more` link is clicked and disables the button', async () => {
jest.spyOn(wrapper.vm, 'handleShowMoreClick').mockImplementation(() => {}); jest.spyOn(gqClient, 'query').mockResolvedValue({ data: {} });
await wrapper.findComponent(GlButton).vm.$emit('click');
wrapper.findComponent(GlButton).vm.$emit('click'); expect(gqClient.query).toHaveBeenCalled();
expect(wrapper.findComponent(GlButton).exists()).toBe(false);
expect(wrapper.vm.handleShowMoreClick).toHaveBeenCalled(); const { fullPath, iid } = mockParentItem;
expect(gqClient.query).toHaveBeenCalledWith(
expect.objectContaining({
variables: {
epicEndCursor: 'abc',
fullPath,
iid,
issueEndCursor: 'def',
},
}),
);
}); });
}); });
}); });
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册