debian-mirror-gitlab/spec/frontend/snippets/components/snippet_header_spec.js

207 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-01-01 13:55:28 +05:30
import SnippetHeader from '~/snippets/components/snippet_header.vue';
import DeleteSnippetMutation from '~/snippets/mutations/deleteSnippet.mutation.graphql';
import { ApolloMutation } from 'vue-apollo';
import { GlButton, GlModal } from '@gitlab/ui';
2020-03-13 15:44:24 +05:30
import { shallowMount } from '@vue/test-utils';
2020-01-01 13:55:28 +05:30
describe('Snippet header component', () => {
let wrapper;
const snippet = {
2020-05-24 23:13:21 +05:30
id: 'gid://gitlab/PersonalSnippet/50',
title: 'The property of Thor',
visibilityLevel: 'private',
webUrl: 'http://personal.dev.null/42',
userPermissions: {
adminSnippet: true,
updateSnippet: true,
reportSnippet: false,
},
project: null,
author: {
name: 'Thor Odinson',
},
blob: {
binary: false,
2020-01-01 13:55:28 +05:30
},
};
const mutationVariables = {
mutation: DeleteSnippetMutation,
variables: {
2020-05-24 23:13:21 +05:30
id: snippet.id,
2020-01-01 13:55:28 +05:30
},
};
const errorMsg = 'Foo bar';
const err = { message: errorMsg };
2020-04-22 19:07:51 +05:30
const resolveMutate = jest.fn(() =>
Promise.resolve({ data: { destroySnippet: { errors: [] } } }),
);
2020-01-01 13:55:28 +05:30
const rejectMutation = jest.fn(() => Promise.reject(err));
const mutationTypes = {
RESOLVE: resolveMutate,
REJECT: rejectMutation,
};
function createComponent({
loading = false,
permissions = {},
mutationRes = mutationTypes.RESOLVE,
2020-05-24 23:13:21 +05:30
snippetProps = {},
2020-01-01 13:55:28 +05:30
} = {}) {
2020-05-24 23:13:21 +05:30
// const defaultProps = Object.assign({}, snippet, snippetProps);
const defaultProps = Object.assign(snippet, snippetProps);
2020-01-01 13:55:28 +05:30
if (permissions) {
2020-05-24 23:13:21 +05:30
Object.assign(defaultProps.userPermissions, {
2020-01-01 13:55:28 +05:30
...permissions,
});
}
const $apollo = {
queries: {
canCreateSnippet: {
loading,
},
},
mutate: mutationRes,
};
wrapper = shallowMount(SnippetHeader, {
mocks: { $apollo },
propsData: {
2020-05-24 23:13:21 +05:30
snippet: {
...defaultProps,
},
2020-01-01 13:55:28 +05:30
},
stubs: {
ApolloMutation,
},
});
}
afterEach(() => {
wrapper.destroy();
});
it('renders itself', () => {
createComponent();
expect(wrapper.find('.detail-page-header').exists()).toBe(true);
});
it('renders action buttons based on permissions', () => {
createComponent({
permissions: {
adminSnippet: false,
updateSnippet: false,
},
});
expect(wrapper.findAll(GlButton).length).toEqual(0);
createComponent({
permissions: {
adminSnippet: true,
updateSnippet: false,
},
});
expect(wrapper.findAll(GlButton).length).toEqual(1);
createComponent({
permissions: {
adminSnippet: true,
updateSnippet: true,
},
});
expect(wrapper.findAll(GlButton).length).toEqual(2);
createComponent({
permissions: {
adminSnippet: true,
updateSnippet: true,
},
});
wrapper.setData({
canCreateSnippet: true,
});
return wrapper.vm.$nextTick().then(() => {
expect(wrapper.findAll(GlButton).length).toEqual(3);
});
});
it('renders modal for deletion of a snippet', () => {
createComponent();
expect(wrapper.find(GlModal).exists()).toBe(true);
});
2020-05-24 23:13:21 +05:30
it('renders Edit button as disabled for binary snippets', () => {
createComponent({
snippetProps: {
blob: {
binary: true,
},
},
});
expect(wrapper.find('[href*="edit"]').props('disabled')).toBe(true);
});
2020-01-01 13:55:28 +05:30
describe('Delete mutation', () => {
const { location } = window;
beforeEach(() => {
delete window.location;
window.location = {
pathname: '',
};
});
afterEach(() => {
window.location = location;
});
it('dispatches a mutation to delete the snippet with correct variables', () => {
createComponent();
wrapper.vm.deleteSnippet();
expect(mutationTypes.RESOLVE).toHaveBeenCalledWith(mutationVariables);
});
it('sets error message if mutation fails', () => {
createComponent({ mutationRes: mutationTypes.REJECT });
expect(Boolean(wrapper.vm.errorMessage)).toBe(false);
wrapper.vm.deleteSnippet();
return wrapper.vm.$nextTick().then(() => {
expect(wrapper.vm.errorMessage).toEqual(errorMsg);
});
});
2020-05-24 23:13:21 +05:30
describe('in case of successful mutation, closes modal and redirects to correct listing', () => {
const createDeleteSnippet = (snippetProps = {}) => {
createComponent({
snippetProps,
});
wrapper.vm.closeDeleteModal = jest.fn();
2020-01-01 13:55:28 +05:30
2020-05-24 23:13:21 +05:30
wrapper.vm.deleteSnippet();
return wrapper.vm.$nextTick();
};
it('redirects to dashboard/snippets for personal snippet', () => {
return createDeleteSnippet().then(() => {
expect(wrapper.vm.closeDeleteModal).toHaveBeenCalled();
expect(window.location.pathname).toBe('dashboard/snippets');
});
});
it('redirects to project snippets for project snippet', () => {
const fullPath = 'foo/bar';
return createDeleteSnippet({
project: {
fullPath,
},
}).then(() => {
expect(wrapper.vm.closeDeleteModal).toHaveBeenCalled();
expect(window.location.pathname).toBe(`${fullPath}/snippets`);
});
2020-01-01 13:55:28 +05:30
});
});
});
});