aboutsummaryrefslogtreecommitdiff
path: root/test/content/components/common/input.test.js
blob: 912ac3452324db58cc8ad39411bd27da2025d183 (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
import InputComponent from 'content/components/common/input';
import { expect } from "chai";

describe('InputComponent', () => {
  it('register callbacks', () => {
    let component = new InputComponent(window.document);
    component.onKey((key) => {
      expect(key).is.equals('a');
    });
    component.onKeyDown({ key: 'a' });
  });

  it('invoke callback once', () => {
    let component = new InputComponent(window.document);
    let a = 0, b = 0;
    component.onKey((key) => {
      if (key == 'a') {
        ++a;
      } else {
        key == 'b'
        ++b;
      }
    });
    component.onKeyDown({ key: 'a' });
    component.onKeyDown({ key: 'b' });
    component.onKeyPress({ key: 'a' });
    component.onKeyUp({ key: 'a' });
    component.onKeyPress({ key: 'b' });
    component.onKeyUp({ key: 'b' });

    expect(a).is.equals(1);
    expect(b).is.equals(1);
  })

  it('add prefix when ctrl pressed', () => {
    let component = new InputComponent(window.document);
    component.onKey((key) => {
      expect(key).is.equals('<C-A>');
    });
    component.onKeyDown({ key: 'a', ctrlKey: true });
  })

  it('press X', () => {
    let component = new InputComponent(window.document);
    component.onKey((key) => {
      expect(key).is.equals('X');
    });
    component.onKeyDown({ key: 'X', shiftKey: true });
  })

  it('press <Shift> + <Esc>', () => {
    let component = new InputComponent(window.document);
    component.onKey((key) => {
      expect(key).is.equals('<S-Esc>');
    });
    component.onKeyDown({ key: 'Escape', shiftKey: true });
  })

  it('press <Ctrl> + <Esc>', () => {
    let component = new InputComponent(window.document);
    component.onKey((key) => {
      expect(key).is.equals('<C-Esc>');
    });
    component.onKeyDown({ key: 'Escape', ctrlKey: true });
  })

  it('does not invoke only meta keys', () => {
    let component = new InputComponent(window.document);
    component.onKey((key) => {
      expect.fail();
    });
    component.onKeyDown({ key: 'Shift' });
    component.onKeyDown({ key: 'Control' });
    component.onKeyDown({ key: 'Alt' });
    component.onKeyDown({ key: 'OS' });
  })

  it('ignores events from input elements', () => {
    ['input', 'textarea', 'select'].forEach((name) => {
      let target = window.document.createElement(name);
      let component = new InputComponent(target);
      component.onKey((key) => {
        expect.fail();
      });
      component.onKeyDown({ key: 'x', target });
    });
  });

  it('ignores events from contenteditable  elements', () => {
    let target = window.document.createElement('div');
    let component = new InputComponent(target);
    component.onKey((key) => {
      expect.fail();
    });

    target.setAttribute('contenteditable', '');
    component.onKeyDown({ key: 'x', target });

    target.setAttribute('contenteditable', 'true');
    component.onKeyDown({ key: 'x', target });
  })
});