summaryrefslogtreecommitdiff
path: root/spec/frontend/integrations/edit/components/trigger_field_spec.js
blob: ed0b33247085687da59576e186860099e4b78d11 (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
import { nextTick } from 'vue';
import { shallowMount } from '@vue/test-utils';
import { GlFormCheckbox, GlFormInput } from '@gitlab/ui';

import TriggerField from '~/integrations/edit/components/trigger_field.vue';
import { integrationTriggerEventTitles } from '~/integrations/constants';

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

  const defaultProps = {
    event: { name: 'push_events' },
    type: 'gitlab_slack_application',
  };
  const mockField = { name: 'push_channel' };

  const createComponent = ({ props = {}, isInheriting = false } = {}) => {
    wrapper = shallowMount(TriggerField, {
      propsData: { ...defaultProps, ...props },
      computed: {
        isInheriting: () => isInheriting,
      },
    });
  };

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

  const findGlFormCheckbox = () => wrapper.findComponent(GlFormCheckbox);
  const findGlFormInput = () => wrapper.findComponent(GlFormInput);
  const findHiddenInput = () => wrapper.find('input[type="hidden"]');

  describe('template', () => {
    it('renders enabled GlFormCheckbox', () => {
      createComponent();

      expect(findGlFormCheckbox().attributes('disabled')).toBeUndefined();
    });

    it('when isInheriting is true, renders disabled GlFormCheckbox', () => {
      createComponent({ isInheriting: true });

      expect(findGlFormCheckbox().attributes('disabled')).toBe('true');
    });

    it('renders correct title', () => {
      createComponent();

      expect(findGlFormCheckbox().text()).toMatchInterpolatedText(
        integrationTriggerEventTitles[defaultProps.event.name],
      );
    });

    it('sets default value for hidden input', () => {
      createComponent();

      expect(findHiddenInput().attributes('value')).toBe('false');
    });

    it('renders hidden GlFormInput', () => {
      createComponent({
        props: {
          event: { name: 'push_events', field: mockField },
        },
      });

      expect(findGlFormInput().exists()).toBe(true);
      expect(findGlFormInput().isVisible()).toBe(false);
    });

    describe('checkbox is selected', () => {
      it('renders visible GlFormInput', async () => {
        createComponent({
          props: {
            event: { name: 'push_events', field: mockField },
          },
        });

        await findGlFormCheckbox().vm.$emit('input', true);

        expect(findGlFormInput().exists()).toBe(true);
        expect(findGlFormInput().isVisible()).toBe(true);
      });
    });

    it('toggles value of hidden input on checkbox input', async () => {
      createComponent({
        props: { event: { name: 'push_events', value: true } },
      });
      await nextTick;

      expect(findHiddenInput().attributes('value')).toBe('true');

      await findGlFormCheckbox().vm.$emit('input', false);

      expect(findHiddenInput().attributes('value')).toBe('false');
    });
  });
});