summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/toggle_button_spec.js
blob: 2822b1999bcfb3c524b9c5831b8e133231438a7a (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
import { shallowMount } from '@vue/test-utils';
import { GlIcon } from '@gitlab/ui';
import ToggleButton from '~/vue_shared/components/toggle_button.vue';

describe('Toggle Button component', () => {
  let wrapper;

  function createComponent(propsData = {}) {
    wrapper = shallowMount(ToggleButton, {
      propsData,
    });
  }

  const findInput = () => wrapper.find('input');
  const findButton = () => wrapper.find('button');
  const findToggleIcon = () => wrapper.find(GlIcon);

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

  it('renders input with provided name', () => {
    createComponent({
      name: 'foo',
    });

    expect(findInput().attributes('name')).toBe('foo');
  });

  describe.each`
    value    | iconName
    ${true}  | ${'status_success_borderless'}
    ${false} | ${'status_failed_borderless'}
  `('when `value` prop is `$value`', ({ value, iconName }) => {
    beforeEach(() => {
      createComponent({
        value,
        name: 'foo',
      });
    });

    it('renders input with correct value attribute', () => {
      expect(findInput().attributes('value')).toBe(`${value}`);
    });

    it('renders correct icon', () => {
      const icon = findToggleIcon();
      expect(icon.isVisible()).toBe(true);
      expect(icon.props('name')).toBe(iconName);
      expect(findButton().classes('is-checked')).toBe(value);
    });

    describe('when clicked', () => {
      it('emits `change` event with correct event', async () => {
        findButton().trigger('click');
        await wrapper.vm.$nextTick();

        expect(wrapper.emitted('change')).toStrictEqual([[!value]]);
      });
    });
  });

  describe('when `disabledInput` prop is `true`', () => {
    beforeEach(() => {
      createComponent({
        value: true,
        disabledInput: true,
      });
    });

    it('renders disabled button', () => {
      expect(findButton().classes()).toContain('is-disabled');
    });

    it('does not emit change event when clicked', async () => {
      findButton().trigger('click');
      await wrapper.vm.$nextTick();

      expect(wrapper.emitted('change')).toBeFalsy();
    });
  });

  describe('when `isLoading` prop is `true`', () => {
    beforeEach(() => {
      createComponent({
        value: true,
        isLoading: true,
      });
    });

    it('renders loading class', () => {
      expect(findButton().classes()).toContain('is-loading');
    });
  });
});