File size: 1,368 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
50
51
/**
 * @jest-environment jsdom
 */

import accept from '../';

describe( '#accept()', () => {
	beforeEach( () => {
		// make up a fake wpcom element for the dialog to target
		document.body.innerHTML = '<div id="wpcom"></div>';
	} );

	test( 'should render a dialog to the document body', () => {
		const message = 'Are you sure?';

		accept( message, function () {} );

		const dialog = document.querySelector( '.accept__dialog' );
		expect( dialog ).toBeInstanceOf( window.Element );
		expect( dialog ).toHaveTextContent( message );
	} );

	test( 'should trigger the callback with an accepted prompt', () => {
		return new Promise( ( done ) => {
			accept( 'Are you sure?', function ( accepted ) {
				expect( accepted ).toBe( true );
				done();
			} );

			document.querySelector( '.button.is-primary' ).click();
		} );
	} );

	test( 'should trigger the callback with a denied prompt', () => {
		return new Promise( ( done ) => {
			accept( 'Are you sure?', function ( accepted ) {
				expect( accepted ).toBe( false );
				done();
			} );

			document.querySelector( '.button.is-cancel' ).click();
		} );
	} );

	test( 'should clean up after itself once the prompt is closed', () => {
		accept( 'Are you sure?', () => {} );
		document.querySelector( '.button.is-primary' ).click();
		expect( document.querySelector( '.accept__dialog' ) ).toBe( null );
	} );
} );