summaryrefslogtreecommitdiff
path: root/spec/frontend/pages/admin/users/components/user_modal_manager_spec.js
blob: 6df2efd624da3e921da42f0900e97400267f1bfa (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { mount } from '@vue/test-utils';
import UserModalManager from '~/pages/admin/users/components/user_modal_manager.vue';
import ModalStub from './stubs/modal_stub';

describe('Users admin page Modal Manager', () => {
  const modalConfiguration = {
    action1: {
      title: 'action1',
      content: 'Action Modal 1',
    },
    action2: {
      title: 'action2',
      content: 'Action Modal 2',
    },
  };

  let wrapper;

  const createComponent = (props = {}) => {
    wrapper = mount(UserModalManager, {
      propsData: {
        modalConfiguration,
        csrfToken: 'dummyCSRF',
        ...props,
      },
      stubs: {
        DeleteUserModal: ModalStub,
      },
    });
  };

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

  describe('render behavior', () => {
    it('does not renders modal when initialized', () => {
      createComponent();
      expect(wrapper.find({ ref: 'modal' }).exists()).toBeFalsy();
    });

    it('throws if action has no proper configuration', () => {
      createComponent({
        modalConfiguration: {},
      });
      expect(() => wrapper.vm.show({ glModalAction: 'action1' })).toThrow();
    });

    it('renders modal with expected props when valid configuration is passed', () => {
      createComponent();
      wrapper.vm.show({
        glModalAction: 'action1',
        extraProp: 'extraPropValue',
      });

      return wrapper.vm.$nextTick().then(() => {
        const modal = wrapper.find({ ref: 'modal' });
        expect(modal.exists()).toBeTruthy();
        expect(modal.vm.$attrs.csrfToken).toEqual('dummyCSRF');
        expect(modal.vm.$attrs.extraProp).toEqual('extraPropValue');
        expect(modal.vm.showWasCalled).toBeTruthy();
      });
    });
  });

  describe('global listener', () => {
    beforeEach(() => {
      jest.spyOn(document, 'addEventListener');
      jest.spyOn(document, 'removeEventListener');
    });

    afterAll(() => {
      jest.restoreAllMocks();
    });

    it('registers global listener on mount', () => {
      createComponent();
      expect(document.addEventListener).toHaveBeenCalledWith('click', expect.any(Function));
    });

    it('removes global listener on destroy', () => {
      createComponent();
      wrapper.destroy();
      expect(document.removeEventListener).toHaveBeenCalledWith('click', expect.any(Function));
    });
  });

  describe('click handling', () => {
    let node;

    beforeEach(() => {
      node = document.createElement('div');
      document.body.appendChild(node);
    });

    afterEach(() => {
      node.remove();
      node = null;
    });

    it('ignores wrong clicks', () => {
      createComponent();
      const event = new window.MouseEvent('click', {
        bubbles: true,
        cancellable: true,
      });
      jest.spyOn(event, 'preventDefault');
      node.dispatchEvent(event);
      expect(event.preventDefault).not.toHaveBeenCalled();
    });

    it('captures click with glModalAction', () => {
      createComponent();
      node.dataset.glModalAction = 'action1';
      const event = new window.MouseEvent('click', {
        bubbles: true,
        cancellable: true,
      });
      jest.spyOn(event, 'preventDefault');
      node.dispatchEvent(event);

      expect(event.preventDefault).toHaveBeenCalled();
      return wrapper.vm.$nextTick().then(() => {
        const modal = wrapper.find({ ref: 'modal' });
        expect(modal.exists()).toBeTruthy();
        expect(modal.vm.showWasCalled).toBeTruthy();
      });
    });
  });
});