File size: 1,173 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @jest-environment jsdom
 */
import { render } from '@testing-library/react';
import CloseOnEscape from '../';

const simulateEscapeKeydown = () =>
	document.dispatchEvent( new window.KeyboardEvent( 'keydown', { keyCode: 27 } ) );

describe( 'CloseOnEscape', () => {
	describe( 'rendering', () => {
		test( 'renders nothing', () => {
			const { container } = render( <CloseOnEscape /> );
			expect( container ).toBeEmptyDOMElement();
		} );
	} );

	describe( 'escape keydown event', () => {
		test( 'calls the `onEscape` method of stacked components in LIFO order on each escape keydown', () => {
			const onEscapeSpy = jest.fn();

			const wrapper1 = render(
				<CloseOnEscape
					onEscape={ function () {
						onEscapeSpy( 1 );
						wrapper1.unmount();
					} }
				/>
			);

			const wrapper2 = render(
				<CloseOnEscape
					onEscape={ function () {
						onEscapeSpy( 2 );
						wrapper2.unmount();
					} }
				/>
			);

			simulateEscapeKeydown();
			expect( onEscapeSpy ).toHaveBeenCalledWith( 2 );
			expect( onEscapeSpy ).not.toHaveBeenCalledWith( 1 );

			simulateEscapeKeydown();
			expect( onEscapeSpy ).toHaveBeenCalledWith( 1 );
		} );
	} );
} );