diff options
Diffstat (limited to 'spec/frontend/branches/components/delete_branch_button_spec.js')
-rw-r--r-- | spec/frontend/branches/components/delete_branch_button_spec.js | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/spec/frontend/branches/components/delete_branch_button_spec.js b/spec/frontend/branches/components/delete_branch_button_spec.js new file mode 100644 index 00000000000..acbc83a9bdc --- /dev/null +++ b/spec/frontend/branches/components/delete_branch_button_spec.js @@ -0,0 +1,96 @@ +import { GlButton } from '@gitlab/ui'; +import { shallowMount } from '@vue/test-utils'; +import DeleteBranchButton from '~/branches/components/delete_branch_button.vue'; +import eventHub from '~/branches/event_hub'; + +let wrapper; +let findDeleteButton; + +const createComponent = (props = {}) => { + wrapper = shallowMount(DeleteBranchButton, { + propsData: { + branchName: 'test', + deletePath: '/path/to/branch', + defaultBranchName: 'main', + ...props, + }, + }); +}; + +describe('Delete branch button', () => { + let eventHubSpy; + + beforeEach(() => { + findDeleteButton = () => wrapper.findComponent(GlButton); + eventHubSpy = jest.spyOn(eventHub, '$emit'); + }); + + afterEach(() => { + wrapper.destroy(); + }); + + it('renders the button with default tooltip, style, and icon', () => { + createComponent(); + + expect(findDeleteButton().attributes()).toMatchObject({ + title: 'Delete branch', + variant: 'danger', + icon: 'remove', + }); + }); + + it('renders a different tooltip for a protected branch', () => { + createComponent({ isProtectedBranch: true }); + + expect(findDeleteButton().attributes()).toMatchObject({ + title: 'Delete protected branch', + variant: 'danger', + icon: 'remove', + }); + }); + + it('renders a different protected tooltip when it is both protected and disabled', () => { + createComponent({ isProtectedBranch: true, disabled: true }); + + expect(findDeleteButton().attributes()).toMatchObject({ + title: 'Only a project maintainer or owner can delete a protected branch', + variant: 'default', + }); + }); + + it('emits the data to eventHub when button is clicked', () => { + createComponent({ merged: true }); + + findDeleteButton().vm.$emit('click'); + + expect(eventHubSpy).toHaveBeenCalledWith('openModal', { + branchName: 'test', + defaultBranchName: 'main', + deletePath: '/path/to/branch', + isProtectedBranch: false, + merged: true, + }); + }); + + describe('#disabled', () => { + it('does not disable the button by default when mounted', () => { + createComponent(); + + expect(findDeleteButton().attributes()).toMatchObject({ + title: 'Delete branch', + variant: 'danger', + }); + }); + + // Used for unallowed users and for the default branch. + it('disables the button when mounted for a disabled modal', () => { + createComponent({ disabled: true, tooltip: 'The default branch cannot be deleted' }); + + expect(findDeleteButton().attributes()).toMatchObject({ + title: 'The default branch cannot be deleted', + disabled: 'true', + variant: 'default', + }); + }); + }); +}); |