summaryrefslogtreecommitdiff
path: root/spec/frontend/toggle_buttons_spec.js
blob: 435fd35744f3c1fa6e5f0e02eb866c5cc415e5da (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
import $ from 'jquery';
import waitForPromises from 'helpers/wait_for_promises';
import setupToggleButtons from '~/toggle_buttons';

function generateMarkup(isChecked = true) {
  return `
    <button type="button" class="${isChecked ? 'is-checked' : ''} js-project-feature-toggle">
      <input type="hidden" class="js-project-feature-toggle-input" value="${isChecked}" />
    </button>
  `;
}

function setupFixture(isChecked, clickCallback) {
  const wrapper = document.createElement('div');
  wrapper.innerHTML = generateMarkup(isChecked);

  setupToggleButtons(wrapper, clickCallback);

  return wrapper;
}

describe('ToggleButtons', () => {
  describe('when input value is true', () => {
    it('should initialize as checked', () => {
      const wrapper = setupFixture(true);

      expect(
        wrapper.querySelector('.js-project-feature-toggle').classList.contains('is-checked'),
      ).toEqual(true);

      expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('true');
    });

    it('should toggle to unchecked when clicked', () => {
      const wrapper = setupFixture(true);
      const toggleButton = wrapper.querySelector('.js-project-feature-toggle');

      toggleButton.click();

      return waitForPromises().then(() => {
        expect(toggleButton.classList.contains('is-checked')).toEqual(false);
        expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('false');
      });
    });
  });

  describe('when input value is false', () => {
    it('should initialize as unchecked', () => {
      const wrapper = setupFixture(false);

      expect(
        wrapper.querySelector('.js-project-feature-toggle').classList.contains('is-checked'),
      ).toEqual(false);

      expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('false');
    });

    it('should toggle to checked when clicked', () => {
      const wrapper = setupFixture(false);
      const toggleButton = wrapper.querySelector('.js-project-feature-toggle');

      toggleButton.click();

      return waitForPromises().then(() => {
        expect(toggleButton.classList.contains('is-checked')).toEqual(true);
        expect(wrapper.querySelector('.js-project-feature-toggle-input').value).toEqual('true');
      });
    });
  });

  it('should emit `trigger-change` event', () => {
    const changeSpy = jest.fn();
    const wrapper = setupFixture(false);
    const toggleButton = wrapper.querySelector('.js-project-feature-toggle');
    const input = wrapper.querySelector('.js-project-feature-toggle-input');

    $(input).on('trigger-change', changeSpy);

    toggleButton.click();

    return waitForPromises().then(() => {
      expect(changeSpy).toHaveBeenCalled();
    });
  });

  describe('clickCallback', () => {
    it('should show loading indicator while waiting', () => {
      const isChecked = true;
      const clickCallback = (newValue, toggleButton) => {
        const input = toggleButton.querySelector('.js-project-feature-toggle-input');

        expect(newValue).toEqual(false);

        // Check for the loading state
        expect(toggleButton.classList.contains('is-checked')).toEqual(false);
        expect(toggleButton.classList.contains('is-loading')).toEqual(true);
        expect(toggleButton.disabled).toEqual(true);
        expect(input.value).toEqual('true');

        // After the callback finishes, check that the loading state is gone
        return waitForPromises().then(() => {
          expect(toggleButton.classList.contains('is-checked')).toEqual(false);
          expect(toggleButton.classList.contains('is-loading')).toEqual(false);
          expect(toggleButton.disabled).toEqual(false);
          expect(input.value).toEqual('false');
        });
      };

      const wrapper = setupFixture(isChecked, clickCallback);
      const toggleButton = wrapper.querySelector('.js-project-feature-toggle');

      toggleButton.click();
    });
  });
});