aboutsummaryrefslogtreecommitdiff
path: root/test/content/presenters/Hint.test.ts
blob: 0823bdc870b358a0287faffd2307f2766ea517c4 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 * @jest-environment jsdom
 */

import AbstractHint, {
  LinkHint,
  InputHint,
} from "../../../src/content/presenters/Hint";
import { expect } from "chai";

class Hint extends AbstractHint {}

describe("Hint", () => {
  beforeEach(() => {
    document.body.innerHTML = `<a id='test-link' href='#'>link</a>`;
  });

  describe("#constructor", () => {
    it("creates a hint element with tag name", () => {
      const link = document.getElementById("test-link")!;
      new Hint(link, "abc");

      const elem = document.querySelector(".vimvixen-hint");
      expect(elem!.textContent!.trim()).to.be.equal("abc");
    });
  });

  describe("#show", () => {
    it("shows an element", () => {
      const link = document.getElementById("test-link")!;
      const hint = new Hint(link, "abc");
      hint.hide();
      hint.show();

      const elem = document.querySelector(".vimvixen-hint") as HTMLElement;
      expect(elem.style.display).to.not.equal("none");
    });
  });

  describe("#hide", () => {
    it("hides an element", () => {
      const link = document.getElementById("test-link") as HTMLElement;
      const hint = new Hint(link, "abc");
      hint.hide();

      const elem = document.querySelector(".vimvixen-hint") as HTMLElement;
      expect(elem.style.display).to.equal("none");
    });
  });

  describe("#remove", () => {
    it("removes an element", () => {
      const link = document.getElementById("test-link")!;
      const hint = new Hint(link, "abc");

      const elem = document.querySelector(".vimvixen-hint")!;
      expect(elem.parentElement).to.not.be.null;
      hint.remove();
      expect(elem.parentElement).to.be.null;
    });
  });
});

describe("LinkHint", () => {
  beforeEach(() => {
    document.body.innerHTML = `
<a id='test-link1' href='https://google.com/'>link</a>
<a id='test-link2' href='https://yahoo.com/' target='_blank'>link</a>
<a id='test-link3' href='#' target='_blank'>link</a>
`;
  });

  describe("#getLink()", () => {
    it('returns value of "href" attribute', () => {
      const link = document.getElementById("test-link1") as HTMLAnchorElement;
      const hint = new LinkHint(link, "abc");

      expect(hint.getLink()).to.equal("https://google.com/");
    });
  });

  describe("#getLinkTarget()", () => {
    it('returns value of "target" attribute', () => {
      let link = document.getElementById("test-link1") as HTMLAnchorElement;
      let hint = new LinkHint(link, "abc");

      expect(hint.getLinkTarget()).to.be.null;

      link = document.getElementById("test-link2") as HTMLAnchorElement;
      hint = new LinkHint(link, "abc");

      expect(hint.getLinkTarget()).to.equal("_blank");
    });
  });

  describe("#click()", () => {
    it("clicks a element", (done) => {
      const link = document.getElementById("test-link3") as HTMLAnchorElement;
      const hint = new LinkHint(link, "abc");
      link.onclick = () => {
        done();
      };

      hint.click();
    });
  });
});

describe("InputHint", () => {
  describe("#activate()", () => {
    describe("<input>", () => {
      beforeEach(() => {
        document.body.innerHTML = `<input id='test-input'></input>`;
      });

      it("focuses to the input", () => {
        const input = document.getElementById("test-input") as HTMLInputElement;
        const hint = new InputHint(input, "abc");
        hint.activate();

        expect(document.activeElement).to.equal(input);
      });
    });

    describe('<input type="checkbox">', () => {
      beforeEach(() => {
        document.body.innerHTML = `<input type="checkbox" id='test-input'></input>`;
      });

      it("checks and focuses to the input", () => {
        const input = document.getElementById("test-input") as HTMLInputElement;
        const hint = new InputHint(input, "abc");
        hint.activate();

        expect(input.checked).to.be.true;
      });
    });
    describe("<textarea>", () => {
      beforeEach(() => {
        document.body.innerHTML = `<textarea id='test-textarea'></textarea>`;
      });

      it("focuses to the textarea", () => {
        const textarea = document.getElementById(
          "test-textarea"
        ) as HTMLTextAreaElement;
        const hint = new InputHint(textarea, "abc");
        hint.activate();

        expect(document.activeElement).to.equal(textarea);
      });
    });

    describe("<button>", () => {
      beforeEach(() => {
        document.body.innerHTML = `<button id='test-button'></button>`;
      });

      it("clicks the button", (done) => {
        const button = document.getElementById(
          "test-button"
        ) as HTMLButtonElement;
        button.onclick = () => {
          done();
        };

        const hint = new InputHint(button, "abc");
        hint.activate();
      });
    });
  });
});