summaryrefslogtreecommitdiff
path: root/spec/frontend/projects/components/shared/delete_button_spec.js
blob: 45c39ee91d888a6b06f625bd5439031b2ca33bc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { GlModal } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { stubComponent } from 'helpers/stub_component';
import SharedDeleteButton from '~/projects/components/shared/delete_button.vue';

jest.mock('~/lib/utils/csrf', () => ({ token: 'test-csrf-token' }));

describe('Project remove modal', () => {
  let wrapper;

  const findFormElement = () => wrapper.find('form');
  const findConfirmButton = () => wrapper.find('.js-modal-action-primary');
  const findAuthenticityTokenInput = () => findFormElement().find('input[name=authenticity_token]');
  const findModal = () => wrapper.find(GlModal);
  const findTitle = () => wrapper.find('[data-testid="delete-alert-title"]');
  const findAlertBody = () => wrapper.find('[data-testid="delete-alert-body"]');

  const defaultProps = {
    confirmPhrase: 'foo',
    formPath: 'some/path',
    isFork: false,
    issuesCount: 1,
    mergeRequestsCount: 2,
    forksCount: 3,
    starsCount: 4,
  };

  const createComponent = (data = {}, stubs = {}, props = {}) => {
    wrapper = shallowMount(SharedDeleteButton, {
      propsData: {
        ...defaultProps,
        ...props,
      },
      data: () => data,
      stubs: {
        GlModal: stubComponent(GlModal, {
          template: `
            <div>
              <slot name="modal-title"></slot>
              <slot></slot>
            </div>`,
        }),
        ...stubs,
      },
    });
  };

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('intialized', () => {
    beforeEach(() => {
      createComponent();
    });

    it('matches the snapshot', () => {
      expect(wrapper.element).toMatchSnapshot();
    });

    it('sets a csrf token on the authenticity form input', () => {
      expect(findAuthenticityTokenInput().element.value).toEqual('test-csrf-token');
    });

    it('sets the form action to the provided path', () => {
      expect(findFormElement().attributes('action')).toEqual(defaultProps.formPath);
    });
  });

  describe('when the user input does not match the confirmPhrase', () => {
    beforeEach(() => {
      createComponent({ userInput: 'bar' }, { GlModal });
    });

    it('the confirm button is disabled', () => {
      expect(findConfirmButton().attributes('disabled')).toBe('true');
    });
  });

  describe('when the user input matches the confirmPhrase', () => {
    beforeEach(() => {
      createComponent({ userInput: defaultProps.confirmPhrase }, { GlModal });
    });

    it('the confirm button is not disabled', () => {
      expect(findConfirmButton().attributes('disabled')).toBe(undefined);
    });
  });

  describe('when the modal is confirmed', () => {
    beforeEach(() => {
      createComponent();
      findModal().vm.$emit('ok');
    });

    it('submits the form element', () => {
      expect(findFormElement().element.submit).toHaveBeenCalled();
    });
  });

  describe('when project is a fork', () => {
    beforeEach(() => {
      createComponent({}, {}, { isFork: true });
    });

    it('matches the fork title', () => {
      expect(findTitle().text()).toEqual('You are about to delete this forked project containing:');
    });

    it('matches the fork body', () => {
      expect(findAlertBody().attributes().message).toEqual(
        'This process deletes the project repository and all related resources.',
      );
    });
  });
});