summaryrefslogtreecommitdiff
path: root/spec/frontend/integrations/edit/components/active_toggle_spec.js
blob: 8a11c200c152599e87a319e4af36bb3c11cb88f6 (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
import { mount } from '@vue/test-utils';
import ActiveToggle from '~/integrations/edit/components/active_toggle.vue';
import { GlToggle } from '@gitlab/ui';

const GL_TOGGLE_ACTIVE_CLASS = 'is-checked';

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

  const defaultProps = {
    initialActivated: true,
    disabled: false,
  };

  const createComponent = props => {
    wrapper = mount(ActiveToggle, {
      propsData: Object.assign({}, defaultProps, props),
    });
  };

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

  const findGlToggle = () => wrapper.find(GlToggle);
  const findButtonInToggle = () => findGlToggle().find('button');
  const findInputInToggle = () => findGlToggle().find('input');

  describe('template', () => {
    describe('initialActivated is false', () => {
      it('renders GlToggle as inactive', () => {
        createComponent({
          initialActivated: false,
        });

        expect(findGlToggle().exists()).toBe(true);
        expect(findButtonInToggle().classes()).not.toContain(GL_TOGGLE_ACTIVE_CLASS);
        expect(findInputInToggle().attributes('value')).toBe('false');
      });
    });

    describe('initialActivated is true', () => {
      beforeEach(() => {
        createComponent();
      });

      it('renders GlToggle as active', () => {
        expect(findGlToggle().exists()).toBe(true);
        expect(findButtonInToggle().classes()).toContain(GL_TOGGLE_ACTIVE_CLASS);
        expect(findInputInToggle().attributes('value')).toBe('true');
      });

      describe('on toggle click', () => {
        it('switches the form value', () => {
          findButtonInToggle().trigger('click');

          wrapper.vm.$nextTick(() => {
            expect(findButtonInToggle().classes()).not.toContain(GL_TOGGLE_ACTIVE_CLASS);
            expect(findInputInToggle().attributes('value')).toBe('false');
          });
        });
      });
    });
  });
});