]>
git.proxmox.com Git - extjs.git/blob - extjs/classic/classic/test/specs/util/KeyboardInteractive.js
1 describe("Ext.util.KeyboardInteractive", function() {
2 var Event
= Ext
.event
.Event
,
3 createSpy
= jasmine
.createSpy
,
4 focusAndWait
= jasmine
.focusAndWait
,
5 waitAWhile
= jasmine
.waitAWhile
,
6 pressArrowKey
= jasmine
.pressArrowKey
,
7 fireKeyEvent
= jasmine
.fireKeyEvent
,
10 function stdComponent(config
) {
13 renderTo
: Ext
.getBody(),
18 getFocusEl: function() {
21 onKeyDefault
: Ext
.emptyFn
25 function makeComponent(config
) {
26 var cmpCfg
= stdComponent(config
);
28 c
= new Ext
.Component(cmpCfg
);
33 afterEach(function() {
41 describe("config handling", function() {
42 beforeEach(function() {
46 it("should accept binding as function", function() {
47 spyOn(Ext
.log
, 'warn');
49 c
.setKeyHandlers({ UP
: Ext
.emptyFn
});
51 expect(Ext
.log
.warn
).not
.toHaveBeenCalled();
53 var handlers
= c
.getKeyHandlers();
55 expect(handlers
.UP
).toBe(Ext
.emptyFn
);
58 it("should accept binding as fn name", function() {
59 c
.setKeyHandlers({ DOWN
: 'onKeyDefault' });
61 var handlers
= c
.getKeyHandlers();
63 expect(handlers
.DOWN
).toBe(Ext
.emptyFn
);
66 it("should throw on unknown keycode", function() {
67 var err
= "Unknown key: FOO in keyHandlers config for " + c
.id
+
68 ". Key names should be in UPPER CASE.";
71 c
.setKeyHandlers({ FOO
: 'onKeyFoo' });
75 it("should warn on undefined binding", function() {
76 // The warning is expected
77 spyOn(Ext
.log
, 'warn');
79 c
.setKeyHandlers({ UP
: 'onKeyUp' });
81 var have
= Ext
.log
.warn
.mostRecentCall
.args
[0],
82 want
= "Undefined binding onKeyUp for UP key " +
83 "in keyHandlers config for " + c
.id
;
85 expect(have
).toBe(want
);
89 describe("keydown listener", function() {
90 describe("w/o config", function() {
91 beforeEach(function() {
94 focusEl
= c
.getFocusEl();
97 it("should not attach listener initially", function() {
98 expect(focusEl
.hasListener('keydown')).toBe(false);
101 it("should attach listener on config update", function() {
102 c
.setKeyHandlers({ HOME
: 'onKeyDefault' });
104 expect(focusEl
.hasListener('keydown')).toBe(true);
108 describe("with config", function() {
109 beforeEach(function() {
116 focusEl
= c
.getFocusEl();
119 it("should attach listener after render", function() {
120 expect(focusEl
.hasListener('keydown')).toBe(true);
123 it("should not attach listener more than once", function() {
124 c
.setKeyHandlers({ RIGHT
: 'onKeyDefault' });
126 expect(focusEl
.hasListeners
.keydown
).toBe(1);
131 describe("handlers", function() {
132 var leftSpy
, rightSpy
;
134 beforeEach(function() {
135 leftSpy
= createSpy('left');
136 rightSpy
= createSpy('right');
149 afterEach(function() {
150 leftSpy
= rightSpy
= null;
153 describe("resolving", function() {
154 it("should resolve handler name to function", function() {
155 var handlers
= c
.getKeyHandlers();
157 expect(handlers
.LEFT
).toBe(leftSpy
);
161 describe("invoking", function() {
162 describe("matching a handler", function() {
163 it("should invoke the handler", function() {
164 pressArrowKey(c
, 'left');
167 expect(leftSpy
).toHaveBeenCalled();
171 it("should pass the key event", function() {
175 fireKeyEvent(c
.getFocusEl(), 'keydown', Event
.RIGHT
);
181 var args
= rightSpy
.mostRecentCall
.args
,
184 expect(ev
.getKey()).toBe(Event
.RIGHT
);
189 describe("not matching a handler", function() {
190 it("should not throw", function() {
195 fireKeyEvent(c
.getFocusEl(), 'keydown', Event
.UP
);