-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
57 lines (50 loc) · 1.57 KB
/
index.js
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
52
53
54
55
56
57
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import PostStickyCheck from '../check';
jest.mock( '@wordpress/data/src/components/use-select', () => {
// This allows us to tweak the returned value on each test.
const mock = jest.fn();
return mock;
} );
function setupUseSelectMock( { hasStickyAction, postType } ) {
useSelect.mockImplementation( ( cb ) => {
return cb( () => ( {
getCurrentPostType: () => postType,
getCurrentPost: () => ( {
_links: {
'wp:action-sticky': hasStickyAction,
},
} ),
} ) );
} );
}
describe( 'PostSticky', () => {
it( 'should not render anything if the post type is not "post"', () => {
setupUseSelectMock( { hasStickyAction: true, postType: 'page' } );
render( <PostStickyCheck>Can Toggle Sticky</PostStickyCheck> );
expect(
screen.queryByText( 'Can Toggle Sticky' )
).not.toBeInTheDocument();
} );
it( "should not render anything if post doesn't support stickying", () => {
setupUseSelectMock( { hasStickyAction: false, postType: 'post' } );
render( <PostStickyCheck>Can Toggle Sticky</PostStickyCheck> );
expect(
screen.queryByText( 'Can Toggle Sticky' )
).not.toBeInTheDocument();
} );
it( 'should render if the post supports stickying', () => {
setupUseSelectMock( { hasStickyAction: true, postType: 'post' } );
render( <PostStickyCheck>Can Toggle Sticky</PostStickyCheck> );
expect( screen.getByText( 'Can Toggle Sticky' ) ).toBeVisible();
} );
} );