summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/droplab_dropdown_button_spec.js
blob: e57c730ecee2c15fd51325899258485a4357ce48 (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
132
import { mount } from '@vue/test-utils';

import DroplabDropdownButton from '~/vue_shared/components/droplab_dropdown_button.vue';

const mockActions = [
  {
    title: 'Foo',
    description: 'Some foo action',
  },
  {
    title: 'Bar',
    description: 'Some bar action',
  },
];

const createComponent = ({
  size = '',
  dropdownClass = '',
  actions = mockActions,
  defaultAction = 0,
}) =>
  mount(DroplabDropdownButton, {
    propsData: {
      size,
      dropdownClass,
      actions,
      defaultAction,
    },
  });

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

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

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

  describe('data', () => {
    it('contains `selectedAction` representing value of `defaultAction` prop', () => {
      expect(wrapper.vm.selectedAction).toBe(0);
    });
  });

  describe('computed', () => {
    describe('selectedActionTitle', () => {
      it('returns string containing title of selected action', () => {
        wrapper.setData({ selectedAction: 0 });

        expect(wrapper.vm.selectedActionTitle).toBe(mockActions[0].title);

        wrapper.setData({ selectedAction: 1 });

        expect(wrapper.vm.selectedActionTitle).toBe(mockActions[1].title);
      });
    });

    describe('buttonSizeClass', () => {
      it('returns string containing button sizing class based on `size` prop', done => {
        const wrapperWithSize = createComponent({
          size: 'sm',
        });

        wrapperWithSize.vm.$nextTick(() => {
          expect(wrapperWithSize.vm.buttonSizeClass).toBe('btn-sm');

          done();
          wrapperWithSize.destroy();
        });
      });
    });
  });

  describe('methods', () => {
    describe('handlePrimaryActionClick', () => {
      it('emits `onActionClick` event on component with selectedAction object as param', () => {
        jest.spyOn(wrapper.vm, '$emit');

        wrapper.setData({ selectedAction: 0 });
        wrapper.vm.handlePrimaryActionClick();

        expect(wrapper.vm.$emit).toHaveBeenCalledWith('onActionClick', mockActions[0]);
      });
    });

    describe('handleActionClick', () => {
      it('emits `onActionSelect` event on component with selectedAction index as param', () => {
        jest.spyOn(wrapper.vm, '$emit');

        wrapper.vm.handleActionClick(1);

        expect(wrapper.vm.$emit).toHaveBeenCalledWith('onActionSelect', 1);
      });
    });
  });

  describe('template', () => {
    it('renders default action button', () => {
      const defaultButton = wrapper.findAll('.btn').at(0);

      expect(defaultButton.text()).toBe(mockActions[0].title);
    });

    it('renders dropdown button', () => {
      const dropdownButton = wrapper.findAll('.dropdown-toggle').at(0);

      expect(dropdownButton.isVisible()).toBe(true);
    });

    it('renders dropdown actions', () => {
      const dropdownActions = wrapper.findAll('.dropdown-menu li button');

      Array(dropdownActions.length)
        .fill()
        .forEach((_, index) => {
          const actionContent = dropdownActions.at(index).find('.description');

          expect(actionContent.find('strong').text()).toBe(mockActions[index].title);
          expect(actionContent.find('p').text()).toBe(mockActions[index].description);
        });
    });

    it('renders divider between dropdown actions', () => {
      const dropdownDivider = wrapper.find('.dropdown-menu .divider');

      expect(dropdownDivider.isVisible()).toBe(true);
    });
  });
});