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

create_and_submit_form_spec.js « utils « lib « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f2472c60f7d3541922be3e931943bb2da4bceda (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
import csrf from '~/lib/utils/csrf';
import { TEST_HOST } from 'helpers/test_constants';
import { createAndSubmitForm } from '~/lib/utils/create_and_submit_form';
import { joinPaths } from '~/lib/utils/url_utility';

const TEST_URL = '/foo/bar/lorem';
const TEST_DATA = {
  'test_thing[0]': 'Lorem Ipsum',
  'test_thing[1]': 'Dolar Sit',
  x: 123,
};
const TEST_CSRF = 'testcsrf00==';

describe('~/lib/utils/create_and_submit_form', () => {
  let submitSpy;

  const findForm = () => document.querySelector('form');
  const findInputsModel = () =>
    Array.from(findForm().querySelectorAll('input')).map((inputEl) => ({
      type: inputEl.type,
      name: inputEl.name,
      value: inputEl.value,
    }));

  beforeEach(() => {
    submitSpy = jest.spyOn(HTMLFormElement.prototype, 'submit');
    document.head.innerHTML = `<meta name="csrf-token" content="${TEST_CSRF}">`;
    csrf.init();
  });

  afterEach(() => {
    document.head.innerHTML = '';
    document.body.innerHTML = '';
  });

  describe('default', () => {
    beforeEach(() => {
      createAndSubmitForm({
        url: TEST_URL,
        data: TEST_DATA,
      });
    });

    it('creates form', () => {
      const form = findForm();

      expect(form.action).toBe(joinPaths(TEST_HOST, TEST_URL));
      expect(form.method).toBe('post');
      expect(form.style).toMatchObject({
        display: 'none',
      });
    });

    it('creates inputs', () => {
      expect(findInputsModel()).toEqual([
        ...Object.keys(TEST_DATA).map((key) => ({
          type: 'hidden',
          name: key,
          value: String(TEST_DATA[key]),
        })),
        {
          type: 'hidden',
          name: 'authenticity_token',
          value: TEST_CSRF,
        },
      ]);
    });

    it('submits form', () => {
      expect(submitSpy).toHaveBeenCalled();
    });
  });
});