Skip to content

Commit 840a0f6

Browse files
Robin Métraldiasbruno
Robin Métral
authored andcommittedOct 14, 2022
Add tests
- extend dispatchMockEvent test helper to support both keyCode and code - duplicate two tests to cover the KeyboardEvent.code logic via escKeyDownWithCode and tabKeyDownWithCode
1 parent 742e9f5 commit 840a0f6

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed
 

‎specs/Modal.events.spec.js

+38
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
mouseDownAt,
1212
mouseUpAt,
1313
escKeyDown,
14+
escKeyDownWithCode,
1415
tabKeyDown,
16+
tabKeyDownWithCode,
1517
withModal,
1618
withElementCollector,
1719
createHTMLElement
@@ -109,6 +111,23 @@ export default () => {
109111
});
110112
});
111113

114+
it("traps tab in the modal on shift + tab with KeyboardEvent.code", () => {
115+
const topButton = <button>top</button>;
116+
const bottomButton = <button>bottom</button>;
117+
const modalContent = (
118+
<div>
119+
{topButton}
120+
{bottomButton}
121+
</div>
122+
);
123+
const props = { isOpen: true };
124+
withModal(props, modalContent, modal => {
125+
const content = mcontent(modal);
126+
tabKeyDownWithCode(content, { shiftKey: true });
127+
document.activeElement.textContent.should.be.eql("bottom");
128+
});
129+
});
130+
112131
describe("shouldCloseOnEsc", () => {
113132
context("when true", () => {
114133
it("should close on Esc key event", () => {
@@ -129,6 +148,25 @@ export default () => {
129148
}
130149
);
131150
});
151+
152+
it("should close on Esc key event with KeyboardEvent.code", () => {
153+
const requestCloseCallback = sinon.spy();
154+
withModal(
155+
{
156+
isOpen: true,
157+
shouldCloseOnEsc: true,
158+
onRequestClose: requestCloseCallback
159+
},
160+
null,
161+
modal => {
162+
escKeyDownWithCode(mcontent(modal));
163+
requestCloseCallback.called.should.be.ok();
164+
// Check if event is passed to onRequestClose callback.
165+
const event = requestCloseCallback.getCall(0).args[0];
166+
event.should.be.ok();
167+
}
168+
);
169+
});
132170
});
133171

134172
context("when false", () => {

‎specs/helper.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,41 @@ const dispatchMockEvent = eventCtor => (key, code) => (element, opts) =>
182182
{},
183183
{
184184
key: key,
185-
keyCode: code,
186185
which: code
187186
},
187+
code,
188188
opts
189189
)
190190
);
191191

192192
const dispatchMockKeyDownEvent = dispatchMockEvent(Simulate.keyDown);
193193

194194
/**
195-
* Dispatch an 'esc' key down event from an element.
195+
* @deprecated will be replaced by `escKeyDownWithCode` when `react-modal`
196+
* drops support for React <18.
197+
*
198+
* Dispatch an 'esc' key down event using the legacy KeyboardEvent.keyCode.
199+
*/
200+
export const escKeyDown = dispatchMockKeyDownEvent("ESC", { keyCode: 27 });
201+
/**
202+
* Dispatch an 'esc' key down event.
203+
*/
204+
export const escKeyDownWithCode = dispatchMockKeyDownEvent("ESC", {
205+
code: "Escape"
206+
});
207+
/**
208+
* @deprecated will be replaced by `escKeyDownWithCode` when `react-modal`
209+
* drops support for React <18.
210+
*
211+
* Dispatch a 'tab' key down event using the legacy KeyboardEvent.keyCode.
196212
*/
197-
export const escKeyDown = dispatchMockKeyDownEvent("ESC", 27);
213+
export const tabKeyDown = dispatchMockKeyDownEvent("TAB", { keyCode: 9 });
198214
/**
199-
* Dispatch a 'tab' key down event from an element.
215+
* Dispatch a 'tab' key down event.
200216
*/
201-
export const tabKeyDown = dispatchMockKeyDownEvent("TAB", 9);
217+
export const tabKeyDownWithCode = dispatchMockKeyDownEvent("TAB", {
218+
code: "Tab"
219+
});
202220
/**
203221
* Dispatch a 'click' event at a node.
204222
*/

0 commit comments

Comments
 (0)
Please sign in to comment.