debian-mirror-gitlab/spec/frontend/notes/mixins/discussion_navigation_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
3.9 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
2021-03-11 19:13:27 +05:30
import Vuex from 'vuex';
2020-10-24 23:57:45 +05:30
import { setHTMLFixture } from 'helpers/fixtures';
2021-03-11 19:13:27 +05:30
import createEventHub from '~/helpers/event_hub_factory';
2020-04-08 14:13:33 +05:30
import * as utils from '~/lib/utils/common_utils';
2021-03-11 19:13:27 +05:30
import discussionNavigation from '~/notes/mixins/discussion_navigation';
2020-04-08 14:13:33 +05:30
import notesModule from '~/notes/stores/modules';
2021-12-11 22:18:48 +05:30
let scrollToFile;
2020-04-08 14:13:33 +05:30
const discussion = (id, index) => ({
id,
resolvable: index % 2 === 0,
active: true,
notes: [{}],
diff_discussion: true,
2021-12-11 22:18:48 +05:30
position: { new_line: 1, old_line: 1 },
diff_file: { file_path: 'test.js' },
2020-04-08 14:13:33 +05:30
});
const createDiscussions = () => [...'abcde'].map(discussion);
const createComponent = () => ({
mixins: [discussionNavigation],
render() {
return this.$slots.default;
},
});
describe('Discussion navigation mixin', () => {
2022-04-04 11:22:00 +05:30
Vue.use(Vuex);
2020-04-08 14:13:33 +05:30
let wrapper;
let store;
let expandDiscussion;
beforeEach(() => {
setHTMLFixture(
2023-01-13 00:05:48 +05:30
`<div class="tab-pane notes">
2022-11-25 23:54:43 +05:30
${[...'abcde']
2020-04-08 14:13:33 +05:30
.map(
2021-03-08 18:12:59 +05:30
(id) =>
2020-04-08 14:13:33 +05:30
`<ul class="notes" data-discussion-id="${id}"></ul>
<div class="discussion" data-discussion-id="${id}"></div>`,
)
2022-11-25 23:54:43 +05:30
.join('')}
</div>`,
2020-04-08 14:13:33 +05:30
);
2020-06-23 00:09:42 +05:30
jest.spyOn(utils, 'scrollToElementWithContext');
2021-02-22 17:27:13 +05:30
jest.spyOn(utils, 'scrollToElement');
2020-04-08 14:13:33 +05:30
expandDiscussion = jest.fn();
2021-12-11 22:18:48 +05:30
scrollToFile = jest.fn();
2020-04-08 14:13:33 +05:30
const { actions, ...notesRest } = notesModule();
store = new Vuex.Store({
modules: {
notes: {
...notesRest,
actions: { ...actions, expandDiscussion },
},
2021-12-11 22:18:48 +05:30
diffs: {
namespaced: true,
2022-11-25 23:54:43 +05:30
actions: { scrollToFile, disableVirtualScroller: () => {} },
2022-07-16 23:28:13 +05:30
state: { diffFiles: [] },
2021-12-11 22:18:48 +05:30
},
2020-04-08 14:13:33 +05:30
},
});
store.state.notes.discussions = createDiscussions();
2022-04-04 11:22:00 +05:30
wrapper = shallowMount(createComponent(), { store });
2020-04-08 14:13:33 +05:30
});
afterEach(() => {
wrapper.vm.$destroy();
jest.clearAllMocks();
});
2020-10-24 23:57:45 +05:30
describe('jumpToFirstUnresolvedDiscussion method', () => {
let vm;
beforeEach(() => {
createComponent();
({ vm } = wrapper);
jest.spyOn(store, 'dispatch');
jest.spyOn(vm, 'jumpToNextDiscussion');
});
it('triggers the setCurrentDiscussionId action with null as the value', () => {
vm.jumpToFirstUnresolvedDiscussion();
expect(store.dispatch).toHaveBeenCalledWith('setCurrentDiscussionId', null);
});
2022-04-04 11:22:00 +05:30
it('triggers the jumpToNextDiscussion action when the previous store action succeeds', async () => {
2020-10-24 23:57:45 +05:30
store.dispatch.mockResolvedValue();
vm.jumpToFirstUnresolvedDiscussion();
2022-04-04 11:22:00 +05:30
await nextTick();
expect(vm.jumpToNextDiscussion).toHaveBeenCalled();
2020-10-24 23:57:45 +05:30
});
});
2020-04-08 14:13:33 +05:30
describe('cycle through discussions', () => {
beforeEach(() => {
2020-05-24 23:13:21 +05:30
window.mrTabs = { eventHub: createEventHub(), tabShown: jest.fn() };
2020-04-08 14:13:33 +05:30
});
describe.each`
2022-11-25 23:54:43 +05:30
fn | args | currentId
${'jumpToNextDiscussion'} | ${[]} | ${null}
${'jumpToNextDiscussion'} | ${[]} | ${'a'}
${'jumpToNextDiscussion'} | ${[]} | ${'e'}
${'jumpToPreviousDiscussion'} | ${[]} | ${null}
${'jumpToPreviousDiscussion'} | ${[]} | ${'e'}
${'jumpToPreviousDiscussion'} | ${[]} | ${'c'}
`('$fn (args = $args, currentId = $currentId)', ({ fn, args, currentId }) => {
2020-04-08 14:13:33 +05:30
beforeEach(() => {
store.state.notes.currentDiscussionId = currentId;
});
describe('on `show` active tab', () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2020-04-08 14:13:33 +05:30
window.mrTabs.currentAction = 'show';
wrapper.vm[fn](...args);
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2020-04-08 14:13:33 +05:30
});
2022-11-25 23:54:43 +05:30
it('expands discussion', async () => {
2022-04-04 11:22:00 +05:30
await nextTick();
2020-04-08 14:13:33 +05:30
expect(expandDiscussion).toHaveBeenCalled();
});
2022-11-25 23:54:43 +05:30
it('scrolls to element', async () => {
2022-04-04 11:22:00 +05:30
await nextTick();
2020-04-08 14:13:33 +05:30
2022-11-25 23:54:43 +05:30
expect(utils.scrollToElement).toHaveBeenCalled();
2020-04-08 14:13:33 +05:30
});
});
});
});
});