summaryrefslogtreecommitdiff
path: root/spec/frontend/ml/experiment_tracking/components/delete_button_spec.js
blob: f2a9e3ad9eeec9774fc5bd546683e095dbf81923 (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
import { GlModal, GlDisclosureDropdown, GlDisclosureDropdownItem } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import DeleteButton from '~/ml/experiment_tracking/components/delete_button.vue';

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

const MODAL_BODY = 'MODAL_BODY';
const MODAL_TITLE = 'MODAL_TITLE';

describe('DeleteButton', () => {
  let wrapper;

  const findModal = () => wrapper.findComponent(GlModal);
  const findDropdown = () => wrapper.findComponent(GlDisclosureDropdown);
  const findDeleteButton = () => wrapper.findComponent(GlDisclosureDropdownItem);
  const findForm = () => wrapper.find('form');
  const findModalText = () => wrapper.findByText(MODAL_BODY);

  beforeEach(() => {
    wrapper = shallowMountExtended(DeleteButton, {
      propsData: {
        deletePath: '/delete',
        deleteConfirmationText: MODAL_BODY,
        actionPrimaryText: 'Delete!',
        modalTitle: MODAL_TITLE,
      },
    });
  });

  it('mounts the modal', () => {
    expect(findModal().exists()).toBe(true);
  });

  it('mounts the dropdown', () => {
    expect(findDropdown().exists()).toBe(true);
  });

  it('mounts the button', () => {
    expect(findDeleteButton().exists()).toBe(true);
  });

  describe('when modal is opened', () => {
    it('displays modal title', () => {
      expect(findModal().props('title')).toBe(MODAL_TITLE);
    });

    it('displays modal body', () => {
      expect(findModalText().exists()).toBe(true);
    });

    it('submits the form when primary action is clicked', () => {
      const submitSpy = jest.spyOn(findForm().element, 'submit');

      findModal().vm.$emit('primary');

      expect(submitSpy).toHaveBeenCalled();
    });

    it('displays form with correct action and inputs', () => {
      const form = findForm();

      expect(form.attributes('action')).toBe('/delete');
      expect(form.find('input[name="_method"]').attributes('value')).toBe('delete');
      expect(form.find('input[name="authenticity_token"]').attributes('value')).toBe(csrfToken);
    });
  });
});