Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_form_spec.js « edit « merge_requests « projects « pages « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72077038dff1a310ca99707491bf6eecabb9d1bf (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
import { setHTMLFixture, resetHTMLFixture } from 'jest/__helpers__/fixtures';
import initFormUpdate from '~/pages/projects/merge_requests/edit/update_form';

describe('Update form state', () => {
  const submitEvent = new Event('submit', {
    bubbles: true,
    cancelable: true,
  });

  const submitForm = () => document.querySelector('.merge-request-form').dispatchEvent(submitEvent);
  const hiddenInputs = () => document.querySelectorAll('input[type="hidden"]');
  const checkboxes = () => document.querySelectorAll('.js-form-update');

  beforeEach(() => {
    setHTMLFixture(`
    <form class="merge-request-form">
        <div class="form-check">
            <input type="hidden" name="merge_request[force_remove_source_branch]" value="0" autocomplete="off">
            <input type="checkbox" name="merge_request[force_remove_source_branch]" id="merge_request_force_remove_source_branch" value="1" class="form-check-input js-form-update">
        </div>
        <div class="form-check">
            <input type="hidden" name="merge_request[squash]" value="0" autocomplete="off">
            <input type="checkbox" name="merge_request[squash]" id="merge_request_squash" value="1" class="form-check-input js-form-update">
        </div>
    </form>`);
    initFormUpdate();
  });

  afterEach(() => {
    resetHTMLFixture();
  });

  it('at initial state', () => {
    submitForm();
    expect(hiddenInputs()).toHaveLength(2);
  });

  it('when one element is checked', () => {
    checkboxes()[0].setAttribute('checked', true);
    submitForm();
    expect(hiddenInputs()).toHaveLength(1);
  });

  it('when all elements are checked', () => {
    checkboxes()[0].setAttribute('checked', true);
    checkboxes()[1].setAttribute('checked', true);
    submitForm();
    expect(hiddenInputs()).toHaveLength(0);
  });

  it('when checked and then unchecked', () => {
    checkboxes()[0].setAttribute('checked', true);
    checkboxes()[0].removeAttribute('checked');
    checkboxes()[1].setAttribute('checked', true);
    checkboxes()[1].removeAttribute('checked');
    submitForm();
    expect(hiddenInputs()).toHaveLength(2);
  });
});