Skip to content

Commit b408cba

Browse files
committed
Added tests
1 parent bc47e15 commit b408cba

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

src/utils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,33 @@ export function getAlignPopupClassName(builtinPlacements, prefixCls, align, isAl
2828
export function saveRef(name, component) {
2929
this[name] = component;
3030
}
31+
32+
// TestUtils.Simulate.keyDown doesn't work on PhantomJS
33+
// https://github.com/ariya/phantomjs/issues/11289#issuecomment-278147426
34+
export function keyboardEvent(eventType, init) {
35+
try {
36+
return new KeyboardEvent(eventType, init);
37+
} catch (error) {
38+
const modKeys = [
39+
init.ctrlKey ? 'Control' : '',
40+
init.shiftKey ? 'Shift' : '',
41+
init.altKey ? 'Alt' : '',
42+
init.altGrKey ? 'AltGr' : '',
43+
init.metaKey ? 'Meta' : '',
44+
].join(' ');
45+
const keyEvent = document.createEvent('KeyboardEvent');
46+
keyEvent.initKeyboardEvent(
47+
eventType, // type
48+
false, // canBubble
49+
false, // cancelable
50+
window, // view
51+
init.char || '', // char
52+
init.keyCode || 0, // key
53+
0, // location
54+
modKeys,
55+
init.repeat || false,
56+
);
57+
keyEvent.key = init.key;
58+
return keyEvent;
59+
}
60+
}

tests/basic.spec.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import '../assets/index.less';
1111
import Trigger from '../index';
1212
import './basic.less';
1313
import async from 'async';
14-
import { saveRef } from '../src/utils';
14+
import { saveRef, keyboardEvent } from '../src/utils';
1515

1616
const Simulate = TestUtils.Simulate;
1717
const scryRenderedDOMComponentsWithClass = TestUtils.scryRenderedDOMComponentsWithClass;
@@ -895,4 +895,64 @@ describe('rc-trigger', function main() {
895895
expect(domNode.className).to.be('target className-in-trigger-1 className-in-trigger-2');
896896
});
897897
});
898+
899+
describe('keyboard', () => {
900+
let visible;
901+
const onChange = (value) => {
902+
visible = value;
903+
};
904+
905+
it('esc key works if keyboard=true ', () => {
906+
const trigger = ReactDOM.render(
907+
<Trigger
908+
keyboard
909+
popup={<div />}
910+
action={['click']}
911+
popupAlign={placementAlignMap.right}
912+
onPopupVisibleChange={onChange}
913+
>
914+
<div>click</div>
915+
</Trigger>,
916+
div
917+
);
918+
const domNode = ReactDOM.findDOMNode(trigger);
919+
920+
// click trigger and show popup
921+
Simulate.click(domNode);
922+
expect(visible).to.be(true);
923+
924+
// click escape
925+
const event = keyboardEvent('keydown', { key: 'Escape', keyCode: 27 });
926+
document.dispatchEvent(event);
927+
928+
// assert that popup hidden
929+
expect(visible).to.be(false);
930+
});
931+
932+
it('esc key doesn\'t work if keyboard=false ', () => {
933+
const trigger = ReactDOM.render(
934+
<Trigger
935+
popup={<div />}
936+
action={['click']}
937+
popupAlign={placementAlignMap.right}
938+
onPopupVisibleChange={onChange}
939+
>
940+
<div>click</div>
941+
</Trigger>,
942+
div
943+
);
944+
const domNode = ReactDOM.findDOMNode(trigger);
945+
946+
// click trigger and show popup
947+
Simulate.click(domNode);
948+
expect(visible).to.be(true);
949+
950+
// click escape
951+
const event = keyboardEvent('keydown', { key: 'Escape', keyCode: 27 });
952+
document.dispatchEvent(event);
953+
954+
// assert that popup still visible
955+
expect(visible).to.be(true);
956+
});
957+
});
898958
});

0 commit comments

Comments
 (0)