|
1 | 1 | import { cleanup, fireEvent, render, act } from '@testing-library/react';
|
| 2 | +import KeyCode from 'rc-util/lib/KeyCode'; |
2 | 3 | import React from 'react';
|
| 4 | +import type { DrawerProps } from '../src'; |
3 | 5 | import Drawer from '../src';
|
4 | 6 |
|
5 | 7 | describe('rc-drawer-menu', () => {
|
@@ -34,29 +36,60 @@ describe('rc-drawer-menu', () => {
|
34 | 36 | expect(document.querySelector('.rc-drawer-content-hidden')).toBeTruthy();
|
35 | 37 | });
|
36 | 38 |
|
37 |
| - it('push', () => { |
38 |
| - const { unmount } = render( |
39 |
| - <Drawer push={{ distance: 903 }} rootClassName="outer" open> |
40 |
| - <Drawer open /> |
41 |
| - </Drawer>, |
42 |
| - ); |
| 39 | + describe('push', () => { |
| 40 | + const placementList: { |
| 41 | + placement: DrawerProps['placement']; |
| 42 | + transform: string; |
| 43 | + }[] = [ |
| 44 | + { |
| 45 | + placement: 'left', |
| 46 | + transform: 'translateX(903px)', |
| 47 | + }, |
| 48 | + { |
| 49 | + placement: 'right', |
| 50 | + transform: 'translateX(-903px)', |
| 51 | + }, |
| 52 | + { |
| 53 | + placement: 'top', |
| 54 | + transform: 'translateY(903px)', |
| 55 | + }, |
| 56 | + { |
| 57 | + placement: 'bottom', |
| 58 | + transform: 'translateY(-903px)', |
| 59 | + }, |
| 60 | + ]; |
43 | 61 |
|
44 |
| - expect( |
45 |
| - document.body |
46 |
| - .querySelector('.outer') |
47 |
| - .querySelector('.rc-drawer-content-wrapper'), |
48 |
| - ).toHaveStyle({ |
49 |
| - transform: 'translateX(-903px)', |
50 |
| - }); |
| 62 | + placementList.forEach(({ placement, transform }) => { |
| 63 | + it(placement, () => { |
| 64 | + const { unmount } = render( |
| 65 | + <Drawer |
| 66 | + push={{ distance: 903 }} |
| 67 | + rootClassName="outer" |
| 68 | + open |
| 69 | + placement={placement} |
| 70 | + > |
| 71 | + <Drawer open /> |
| 72 | + </Drawer>, |
| 73 | + ); |
51 | 74 |
|
52 |
| - expect(document.body).toHaveStyle({ |
53 |
| - overflow: 'hidden', |
54 |
| - }); |
| 75 | + expect( |
| 76 | + document.body |
| 77 | + .querySelector('.outer') |
| 78 | + .querySelector('.rc-drawer-content-wrapper'), |
| 79 | + ).toHaveStyle({ |
| 80 | + transform, |
| 81 | + }); |
55 | 82 |
|
56 |
| - unmount(); |
| 83 | + expect(document.body).toHaveStyle({ |
| 84 | + overflow: 'hidden', |
| 85 | + }); |
| 86 | + |
| 87 | + unmount(); |
57 | 88 |
|
58 |
| - expect(document.body).not.toHaveStyle({ |
59 |
| - overflow: 'hidden', |
| 89 | + expect(document.body).not.toHaveStyle({ |
| 90 | + overflow: 'hidden', |
| 91 | + }); |
| 92 | + }); |
60 | 93 | });
|
61 | 94 | });
|
62 | 95 |
|
@@ -167,4 +200,59 @@ describe('rc-drawer-menu', () => {
|
167 | 200 |
|
168 | 201 | expect(container.contains(document.activeElement)).toBeTruthy();
|
169 | 202 | });
|
| 203 | + |
| 204 | + it('tab should always in the content', () => { |
| 205 | + const { container } = render( |
| 206 | + <Drawer open getContainer={false}> |
| 207 | + <div>Hello World</div> |
| 208 | + </Drawer>, |
| 209 | + ); |
| 210 | + |
| 211 | + const list = Array.from( |
| 212 | + container.querySelector('.rc-drawer-content').children, |
| 213 | + ) as HTMLElement[]; |
| 214 | + |
| 215 | + const firstSentinel = list[0]; |
| 216 | + const lastSentinel = list[2]; |
| 217 | + |
| 218 | + // First shift to last |
| 219 | + firstSentinel.focus(); |
| 220 | + fireEvent.keyDown(firstSentinel, { |
| 221 | + shiftKey: true, |
| 222 | + keyCode: KeyCode.TAB, |
| 223 | + which: KeyCode.TAB, |
| 224 | + }); |
| 225 | + expect(document.activeElement).toBe(lastSentinel); |
| 226 | + |
| 227 | + // Last tab to first |
| 228 | + fireEvent.keyDown(lastSentinel, { |
| 229 | + keyCode: KeyCode.TAB, |
| 230 | + which: KeyCode.TAB, |
| 231 | + }); |
| 232 | + expect(document.activeElement).toBe(firstSentinel); |
| 233 | + }); |
| 234 | + |
| 235 | + describe('keyboard', () => { |
| 236 | + it('ESC to exit', () => { |
| 237 | + const onClose = jest.fn(); |
| 238 | + const { container } = render( |
| 239 | + <Drawer open getContainer={false} onClose={onClose} />, |
| 240 | + ); |
| 241 | + fireEvent.keyDown(container.querySelector('.rc-drawer-content'), { |
| 242 | + keyCode: KeyCode.ESC, |
| 243 | + }); |
| 244 | + expect(onClose).toHaveBeenCalled(); |
| 245 | + }); |
| 246 | + |
| 247 | + it('disable ESC to exit', () => { |
| 248 | + const onClose = jest.fn(); |
| 249 | + const { container } = render( |
| 250 | + <Drawer open getContainer={false} onClose={onClose} keyboard={false} />, |
| 251 | + ); |
| 252 | + fireEvent.keyDown(container.querySelector('.rc-drawer-content'), { |
| 253 | + keyCode: KeyCode.ESC, |
| 254 | + }); |
| 255 | + expect(onClose).not.toHaveBeenCalled(); |
| 256 | + }); |
| 257 | + }); |
170 | 258 | });
|
0 commit comments