aboutsummaryrefslogtreecommitdiff
path: root/test/settings/components/form/KeymapsForm.test.tsx
blob: 4701a967ccede3450ffe9dd7ebc3a9bf9013ac1e (plain) (blame)
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
/**
 * @jest-environment jsdom
 */

import React from "react";
import ReactDOM from "react-dom";
import ReactTestRenderer from "react-test-renderer";
import ReactTestUtils from "react-dom/test-utils";
import KeymapsForm from "../../../../src/settings/components/form/KeymapsForm";
import { FormKeymaps } from "../../../../src/shared/SettingData";

describe("settings/form/KeymapsForm", () => {
  describe("render", () => {
    it("renders keymap fields", () => {
      const root = ReactTestRenderer.create(
        <KeymapsForm
          value={FormKeymaps.fromJSON({
            'scroll.vertically?{"count":1}': "j",
            'scroll.vertically?{"count":-1}': "k",
          })}
        />
      ).root;

      const inputj = root.findByProps({ id: 'scroll.vertically?{"count":1}' });
      const inputk = root.findByProps({ id: 'scroll.vertically?{"count":-1}' });

      expect(inputj.props.value).toEqual("j");
      expect(inputk.props.value).toEqual("k");
    });

    it("renders blank value", () => {
      const root = ReactTestRenderer.create(<KeymapsForm />).root;

      const inputj = root.findByProps({ id: 'scroll.vertically?{"count":1}' });
      const inputk = root.findByProps({ id: 'scroll.vertically?{"count":-1}' });

      expect(inputj.props.value).toHaveLength(0);
      expect(inputk.props.value).toHaveLength(0);
    });
  });

  describe("onChange event", () => {
    let container: HTMLDivElement;

    beforeEach(() => {
      container = document.createElement("div");
      document.body.appendChild(container);
    });

    afterEach(() => {
      document.body.removeChild(container);
    });

    it("invokes onChange event on edit", (done) => {
      ReactTestUtils.act(() => {
        ReactDOM.render(
          <KeymapsForm
            value={FormKeymaps.fromJSON({
              'scroll.vertically?{"count":1}': "j",
              'scroll.vertically?{"count":-1}': "k",
            })}
            onChange={(value) => {
              expect(value.toJSON()['scroll.vertically?{"count":1}']).toEqual(
                "jjj"
              );
              done();
            }}
          />,
          container
        );
      });

      const input = document.getElementById(
        'scroll.vertically?{"count":1}'
      ) as HTMLInputElement;
      input.value = "jjj";
      ReactTestUtils.Simulate.change(input);
    });
  });
});