aboutsummaryrefslogtreecommitdiff
path: root/test/settings/components/form/PropertiesForm.test.tsx
blob: acf02b8c3c9caa79e889f1ab985e98d2e50c8fd6 (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
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
116
117
118
119
import React from "react";
import ReactDOM from "react-dom";
import ReactTestRenderer from "react-test-renderer";
import ReactTestUtils from "react-dom/test-utils";
import PropertiesForm from "../../../../src/settings/components/form/PropertiesForm";
import { expect } from "chai";

describe("settings/form/PropertiesForm", () => {
  describe("render", () => {
    it("renders PropertiesForm", () => {
      const types = {
        mystr: "string",
        mynum: "number",
        mybool: "boolean",
        empty: "string",
      };
      const values = {
        mystr: "abc",
        mynum: 123,
        mybool: true,
      };

      const root = ReactTestRenderer.create(
        <PropertiesForm types={types} value={values} />
      ).root;

      let input = root.findByProps({ name: "mystr" });
      expect(input.props.type).to.equals("text");
      expect(input.props.value).to.equal("abc");

      input = root.findByProps({ name: "mynum" });
      expect(input.props.type).to.equals("number");
      expect(input.props.value).to.equal(123);

      input = root.findByProps({ name: "mybool" });
      expect(input.props.type).to.equals("checkbox");
      expect(input.props.value).to.equal(true);
    });
  });

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

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

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

    it("invokes onChange event on text changed", (done) => {
      ReactTestUtils.act(() => {
        ReactDOM.render(
          <PropertiesForm
            types={{ myvalue: "string" }}
            value={{ myvalue: "abc" }}
            onChange={(value) => {
              expect(value).to.have.property("myvalue", "abcd");
              done();
            }}
          />,
          container
        );
      });

      const input = document.querySelector(
        "input[name=myvalue]"
      ) as HTMLInputElement;
      input.value = "abcd";
      ReactTestUtils.Simulate.change(input);
    });

    it("invokes onChange event on number changeed", (done) => {
      ReactTestUtils.act(() => {
        ReactDOM.render(
          <PropertiesForm
            types={{ myvalue: "number" }}
            value={{ "": 123 }}
            onChange={(value) => {
              expect(value).to.have.property("myvalue", 1234);
              done();
            }}
          />,
          container
        );
      });

      const input = document.querySelector(
        "input[name=myvalue]"
      ) as HTMLInputElement;
      input.value = "1234";
      ReactTestUtils.Simulate.change(input);
    });

    it("invokes onChange event on checkbox changed", (done) => {
      ReactTestUtils.act(() => {
        ReactDOM.render(
          <PropertiesForm
            types={{ myvalue: "boolean" }}
            value={{ myvalue: false }}
            onChange={(value) => {
              expect(value).to.have.property("myvalue", true);
              done();
            }}
          />,
          container
        );
      });

      const input = document.querySelector(
        "input[name=myvalue]"
      ) as HTMLInputElement;
      input.checked = true;
      ReactTestUtils.Simulate.change(input);
    });
  });
});