summaryrefslogtreecommitdiff
path: root/spec/frontend/admin/topics/components/remove_avatar_spec.js
blob: 97d257c682cd131802ad2fc4ed821455481d6803 (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
import { GlButton, GlModal, GlSprintf } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
import RemoveAvatar from '~/admin/topics/components/remove_avatar.vue';

const modalID = 'fake-id';
const path = 'topic/path/1';
const name = 'Topic 1';

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

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

  const createComponent = () => {
    wrapper = shallowMount(RemoveAvatar, {
      provide: {
        path,
        name,
      },
      directives: {
        GlModal: createMockDirective(),
      },
      stubs: {
        GlSprintf,
      },
    });
  };

  const findButton = () => wrapper.findComponent(GlButton);
  const findModal = () => wrapper.findComponent(GlModal);
  const findForm = () => wrapper.find('form');

  beforeEach(() => {
    createComponent();
  });

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

  describe('the button component', () => {
    it('displays the remove button', () => {
      const button = findButton();

      expect(button.exists()).toBe(true);
      expect(button.text()).toBe('Remove avatar');
    });

    it('contains the correct modal ID', () => {
      const buttonModalId = getBinding(findButton().element, 'gl-modal').value;

      expect(buttonModalId).toBe(modalID);
    });
  });

  describe('the modal component', () => {
    it('displays the modal component', () => {
      const modal = findModal();

      expect(modal.exists()).toBe(true);
      expect(modal.props('title')).toBe('Remove topic avatar');
      expect(modal.text()).toBe(`Topic avatar for ${name} will be removed. This cannot be undone.`);
    });

    it('contains the correct modal ID', () => {
      expect(findModal().props('modalId')).toBe(modalID);
    });

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

      describe('form submission', () => {
        let formSubmitSpy;

        beforeEach(() => {
          formSubmitSpy = jest.spyOn(wrapper.vm.$refs.deleteForm, 'submit');
          findModal().vm.$emit('primary');
        });

        it('submits the form on the modal primary action', () => {
          expect(formSubmitSpy).toHaveBeenCalled();
        });
      });
    });
  });
});