aboutsummaryrefslogtreecommitdiff
path: root/test/settings/components/form/BlacklistForm.test.tsx
blob: 9840856413772a7f81ed1699926bf56cc806fdf0 (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
120
121
122
123
124
125
/**
 * @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 { expect } from "chai";

import BlacklistForm from "../../../../src/settings/components/form/BlacklistForm";
import Blacklist from "../../../../src/shared/settings/Blacklist";
import AddButton from "../../../../src/settings/components/ui/AddButton";

describe("settings/form/BlacklistForm", () => {
  describe("render", () => {
    it("renders BlacklistForm", () => {
      const root = ReactTestRenderer.create(
        <BlacklistForm
          value={Blacklist.fromJSON(["*.slack.com", "www.google.com/maps"])}
        />
      ).root;

      const rows = root
        .findAllByType("div")
        .filter((instance) => instance.props.role === "listitem");
      expect(rows).to.have.lengthOf(2);
      expect(rows[0].findByProps({ name: "url" }).props.value).to.equal(
        "*.slack.com"
      );
      expect(rows[1].findByProps({ name: "url" }).props.value).to.equal(
        "www.google.com/maps"
      );

      expect(() => root.findByType(AddButton)).not.throw();
    });

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

      const rows = root.findAllByProps({
        className: "form-blacklist-form-row",
      });
      expect(rows).to.be.empty;
    });
  });

  describe("onChange", () => {
    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(
          <BlacklistForm
            value={Blacklist.fromJSON(["*.slack.com", "www.google.com/maps*"])}
            onChange={(value) => {
              const urls = value.items.map((item) => item.pattern);
              expect(urls).to.have.members([
                "gitter.im",
                "www.google.com/maps*",
              ]);
              done();
            }}
          />,
          container
        );
      });

      const input = document.querySelectorAll(
        "input[type=text]"
      )[0] as HTMLInputElement;
      input.value = "gitter.im";
      ReactTestUtils.Simulate.change(input);
    });

    it("invokes onChange event on delete", (done) => {
      ReactTestUtils.act(() => {
        ReactDOM.render(
          <BlacklistForm
            value={Blacklist.fromJSON(["*.slack.com", "www.google.com/maps*"])}
            onChange={(value) => {
              const urls = value.items.map((item) => item.pattern);
              expect(urls).to.have.members(["www.google.com/maps*"]);
              done();
            }}
          />,
          container
        );
      });

      const button = document.querySelectorAll("input[type=button]")[0];
      ReactTestUtils.Simulate.click(button);
    });

    it("invokes onChange event on add", (done) => {
      ReactTestUtils.act(() => {
        ReactDOM.render(
          <BlacklistForm
            value={Blacklist.fromJSON(["*.slack.com"])}
            onChange={(value) => {
              const urls = value.items.map((item) => item.pattern);
              expect(urls).to.have.members(["*.slack.com", ""]);
              done();
            }}
          />,
          container
        );
      });

      const button = document.querySelector(
        "input[type=button][name=add]"
      ) as HTMLButtonElement;
      ReactTestUtils.Simulate.click(button);
    });
  });
});