debian-mirror-gitlab/spec/frontend/vue_shared/components/markdown/suggestion_diff_header_spec.js

244 lines
7.1 KiB
JavaScript
Raw Normal View History

2019-07-31 22:56:46 +05:30
import { GlLoadingIcon } from '@gitlab/ui';
2020-03-13 15:44:24 +05:30
import { shallowMount } from '@vue/test-utils';
2021-04-29 21:17:54 +05:30
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
2021-04-17 20:07:23 +05:30
import ApplySuggestion from '~/vue_shared/components/markdown/apply_suggestion.vue';
2019-07-31 22:56:46 +05:30
import SuggestionDiffHeader from '~/vue_shared/components/markdown/suggestion_diff_header.vue';
const DEFAULT_PROPS = {
2020-06-23 00:09:42 +05:30
batchSuggestionsCount: 2,
2019-07-31 22:56:46 +05:30
canApply: true,
isApplied: false,
2020-06-23 00:09:42 +05:30
isBatched: false,
isApplyingBatch: false,
2019-07-31 22:56:46 +05:30
helpPagePath: 'path_to_docs',
2021-03-08 18:12:59 +05:30
defaultCommitMessage: 'Apply suggestion',
2019-07-31 22:56:46 +05:30
};
describe('Suggestion Diff component', () => {
let wrapper;
2021-04-29 21:17:54 +05:30
const createComponent = (props) => {
2020-03-13 15:44:24 +05:30
wrapper = shallowMount(SuggestionDiffHeader, {
2019-07-31 22:56:46 +05:30
propsData: {
...DEFAULT_PROPS,
...props,
},
2021-04-29 21:17:54 +05:30
directives: {
GlTooltip: createMockDirective(),
2020-06-23 00:09:42 +05:30
},
2019-07-31 22:56:46 +05:30
});
};
2021-01-29 00:20:46 +05:30
beforeEach(() => {
window.gon.current_user_id = 1;
});
2019-07-31 22:56:46 +05:30
afterEach(() => {
wrapper.destroy();
});
2021-04-17 20:07:23 +05:30
const findApplyButton = () => wrapper.find(ApplySuggestion);
2020-06-23 00:09:42 +05:30
const findApplyBatchButton = () => wrapper.find('.js-apply-batch-btn');
const findAddToBatchButton = () => wrapper.find('.js-add-to-batch-btn');
const findRemoveFromBatchButton = () => wrapper.find('.js-remove-from-batch-btn');
2019-10-12 21:52:04 +05:30
const findHeader = () => wrapper.find('.js-suggestion-diff-header');
2019-07-31 22:56:46 +05:30
const findHelpButton = () => wrapper.find('.js-help-btn');
const findLoading = () => wrapper.find(GlLoadingIcon);
it('renders a suggestion header', () => {
createComponent();
const header = findHeader();
expect(header.exists()).toBe(true);
expect(header.html().includes('Suggested change')).toBe(true);
});
it('renders a help button', () => {
createComponent();
expect(findHelpButton().exists()).toBe(true);
});
2021-11-18 22:05:49 +05:30
it('renders add to batch button when more than 1 suggestion', () => {
2021-01-03 14:25:43 +05:30
createComponent({
suggestionsCount: 2,
});
2019-07-31 22:56:46 +05:30
const applyBtn = findApplyButton();
2020-06-23 00:09:42 +05:30
const addToBatchBtn = findAddToBatchButton();
2019-07-31 22:56:46 +05:30
2021-11-18 22:05:49 +05:30
expect(applyBtn.exists()).toBe(false);
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
expect(addToBatchBtn.exists()).toBe(true);
expect(addToBatchBtn.html().includes('Add suggestion to batch')).toBe(true);
});
2019-07-31 22:56:46 +05:30
2021-01-29 00:20:46 +05:30
it('does not render apply suggestion button with anonymous user', () => {
window.gon.current_user_id = null;
createComponent();
expect(findApplyButton().exists()).toBe(false);
});
2019-07-31 22:56:46 +05:30
describe('when apply suggestion is clicked', () => {
2020-01-01 13:55:28 +05:30
beforeEach(() => {
2021-11-18 22:05:49 +05:30
createComponent({ batchSuggestionsCount: 0 });
2019-07-31 22:56:46 +05:30
2021-04-17 20:07:23 +05:30
findApplyButton().vm.$emit('apply');
2019-07-31 22:56:46 +05:30
});
it('emits apply', () => {
2021-03-08 18:12:59 +05:30
expect(wrapper.emitted().apply).toEqual([[expect.any(Function), undefined]]);
2019-07-31 22:56:46 +05:30
});
2020-06-23 00:09:42 +05:30
it('does not render apply suggestion and add to batch buttons', () => {
2019-07-31 22:56:46 +05:30
expect(findApplyButton().exists()).toBe(false);
2020-06-23 00:09:42 +05:30
expect(findAddToBatchButton().exists()).toBe(false);
2019-07-31 22:56:46 +05:30
});
it('shows loading', () => {
expect(findLoading().exists()).toBe(true);
2020-06-23 00:09:42 +05:30
expect(wrapper.text()).toContain('Applying suggestion...');
2019-07-31 22:56:46 +05:30
});
2020-01-01 13:55:28 +05:30
it('when callback of apply is called, hides loading', () => {
2019-07-31 22:56:46 +05:30
const [callback] = wrapper.emitted().apply[0];
callback();
2020-01-01 13:55:28 +05:30
return wrapper.vm.$nextTick().then(() => {
expect(findApplyButton().exists()).toBe(true);
expect(findLoading().exists()).toBe(false);
});
2019-07-31 22:56:46 +05:30
});
});
2020-06-23 00:09:42 +05:30
describe('when add to batch is clicked', () => {
it('emits addToBatch', () => {
2021-01-03 14:25:43 +05:30
createComponent({
suggestionsCount: 2,
});
2020-06-23 00:09:42 +05:30
findAddToBatchButton().vm.$emit('click');
2020-11-24 15:15:51 +05:30
expect(wrapper.emitted().addToBatch).toEqual([[]]);
2020-06-23 00:09:42 +05:30
});
});
describe('when remove from batch is clicked', () => {
it('emits removeFromBatch', () => {
createComponent({ isBatched: true });
findRemoveFromBatchButton().vm.$emit('click');
2020-11-24 15:15:51 +05:30
expect(wrapper.emitted().removeFromBatch).toEqual([[]]);
2020-06-23 00:09:42 +05:30
});
});
describe('apply suggestions is clicked', () => {
it('emits applyBatch', () => {
2021-11-18 22:05:49 +05:30
createComponent({ isBatched: true, batchSuggestionsCount: 2 });
2020-06-23 00:09:42 +05:30
2021-11-18 22:05:49 +05:30
findApplyButton().vm.$emit('apply');
2020-06-23 00:09:42 +05:30
2021-11-18 22:05:49 +05:30
expect(wrapper.emitted().applyBatch).toEqual([[undefined]]);
2020-06-23 00:09:42 +05:30
});
});
describe('when isBatched is true', () => {
it('shows remove from batch and apply batch buttons and displays the batch count', () => {
createComponent({
batchSuggestionsCount: 9,
isBatched: true,
});
2021-11-18 22:05:49 +05:30
const applyBatchBtn = findApplyButton();
2020-06-23 00:09:42 +05:30
const removeFromBatchBtn = findRemoveFromBatchButton();
expect(removeFromBatchBtn.exists()).toBe(true);
expect(removeFromBatchBtn.html().includes('Remove from batch')).toBe(true);
expect(applyBatchBtn.exists()).toBe(true);
2021-11-18 22:05:49 +05:30
expect(applyBatchBtn.html().includes('Apply suggestion')).toBe(true);
2020-06-23 00:09:42 +05:30
expect(applyBatchBtn.html().includes(String('9'))).toBe(true);
});
it('hides add to batch and apply buttons', () => {
createComponent({
isBatched: true,
2021-11-18 22:05:49 +05:30
batchSuggestionsCount: 9,
2020-06-23 00:09:42 +05:30
});
2021-11-18 22:05:49 +05:30
expect(findApplyButton().exists()).toBe(true);
2020-06-23 00:09:42 +05:30
expect(findAddToBatchButton().exists()).toBe(false);
});
describe('when isBatched and isApplyingBatch are true', () => {
it('shows loading', () => {
createComponent({
isBatched: true,
isApplyingBatch: true,
});
expect(findLoading().exists()).toBe(true);
expect(wrapper.text()).toContain('Applying suggestions...');
});
it('adjusts message for batch with single suggestion', () => {
createComponent({
batchSuggestionsCount: 1,
isBatched: true,
isApplyingBatch: true,
});
expect(findLoading().exists()).toBe(true);
expect(wrapper.text()).toContain('Applying suggestion...');
});
it('hides remove from batch and apply suggestions buttons', () => {
createComponent({
isBatched: true,
isApplyingBatch: true,
});
expect(findRemoveFromBatchButton().exists()).toBe(false);
expect(findApplyBatchButton().exists()).toBe(false);
});
});
});
describe('canApply is set to false', () => {
beforeEach(() => {
createComponent({ canApply: false });
});
2020-07-28 23:09:34 +05:30
it('disables apply suggestion and hides add to batch button', () => {
2021-11-18 22:05:49 +05:30
expect(findApplyButton().exists()).toBe(false);
2020-07-28 23:09:34 +05:30
expect(findAddToBatchButton().exists()).toBe(false);
});
});
describe('tooltip message for apply button', () => {
2021-04-29 21:17:54 +05:30
const findTooltip = () => getBinding(findApplyButton().element, 'gl-tooltip');
2020-07-28 23:09:34 +05:30
it('renders correct tooltip message when button is applicable', () => {
2021-11-18 22:05:49 +05:30
createComponent({ batchSuggestionsCount: 0 });
2021-04-29 21:17:54 +05:30
const tooltip = findTooltip();
expect(tooltip.modifiers.viewport).toBe(true);
expect(tooltip.value).toBe('This also resolves this thread');
2020-06-23 00:09:42 +05:30
});
2020-07-28 23:09:34 +05:30
it('renders the inapplicable reason in the tooltip when button is not applicable', () => {
const inapplicableReason = 'lorem';
2021-11-18 22:05:49 +05:30
createComponent({ canApply: false, inapplicableReason, batchSuggestionsCount: 0 });
2021-04-29 21:17:54 +05:30
const tooltip = findTooltip();
expect(tooltip.modifiers.viewport).toBe(true);
expect(tooltip.value).toBe(inapplicableReason);
2020-06-23 00:09:42 +05:30
});
});
2019-07-31 22:56:46 +05:30
});