summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_shared/components/modal_spec.js
blob: 8412df74f98e4004527f42175e6a302d8c332ddb (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
import Vue from 'vue';
import modal from '~/vue_shared/components/modal.vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper';

const modalComponent = Vue.extend(modal);

describe('Modal', () => {
  let vm;

  afterEach(() => {
    vm.$destroy();
  });

  describe('props', () => {
    describe('without primaryButtonLabel', () => {
      beforeEach(() => {
        vm = mountComponent(modalComponent, {
          primaryButtonLabel: null,
        });
      });

      it('does not render a primary button', () => {
        expect(vm.$el.querySelector('.js-primary-button')).toBeNull();
      });
    });

    describe('with id', () => {
      describe('does not render a primary button', () => {
        beforeEach(() => {
          vm = mountComponent(modalComponent, {
            id: 'my-modal',
          });
        });

        it('assigns the id to the modal', () => {
          expect(vm.$el.querySelector('#my-modal.modal')).not.toBeNull();
        });

        it('does not show the modal immediately', () => {
          expect(vm.$el.querySelector('#my-modal.modal')).not.toHaveClass('show');
        });

        it('does not show a backdrop', () => {
          expect(vm.$el.querySelector('modal-backdrop')).toBeNull();
        });
      });
    });

    it('works with data-toggle="modal"', (done) => {
      setFixtures(`
        <button id="modal-button" data-toggle="modal" data-target="#my-modal"></button>
        <div id="modal-container"></div>
      `);

      const modalContainer = document.getElementById('modal-container');
      const modalButton = document.getElementById('modal-button');
      vm = mountComponent(modalComponent, {
        id: 'my-modal',
      }, modalContainer);
      const modalElement = vm.$el.querySelector('#my-modal');
      $(modalElement).on('shown.bs.modal', () => done());

      modalButton.click();
    });
  });
});