/
with-viewport-match.js
67 lines (56 loc) · 1.75 KB
/
with-viewport-match.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
58
59
60
61
62
63
64
65
66
67
/**
* External dependencies
*/
import renderer from 'react-test-renderer';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { useViewportMatch as useViewportMatchMock } from '@wordpress/compose';
/**
* Internal dependencies
*/
import '../store';
import withViewportMatch from '../with-viewport-match';
describe( 'withViewportMatch()', () => {
afterEach( () => {
useViewportMatchMock.mockClear();
} );
const ChildComponent = () => <div>Hello</div>;
// this is needed because TestUtils does not accept a stateless component.
// anything run through a HOC ends up as a stateless component.
const getTestComponent = ( WrappedComponent ) => {
class TestComponent extends Component {
render() {
return <WrappedComponent { ...this.props } />;
}
}
return <TestComponent />;
};
it( 'should render with result of query as custom prop name', () => {
const EnhancedComponent = withViewportMatch( {
isWide: '>= wide',
isSmall: '>= small',
isLarge: 'large',
isLessThanSmall: '< small',
}
)( ChildComponent );
useViewportMatchMock.mockReturnValueOnce( false );
useViewportMatchMock.mockReturnValueOnce( true );
useViewportMatchMock.mockReturnValueOnce( true );
useViewportMatchMock.mockReturnValueOnce( false );
const wrapper = renderer.create( getTestComponent( EnhancedComponent ) );
expect( useViewportMatchMock.mock.calls ).toEqual( [
[ 'wide', '>=' ],
[ 'small', '>=' ],
[ 'large', '>=' ],
[ 'small', '<' ],
] );
const { props } = wrapper.root.findByType( ChildComponent );
expect( props.isWide ).toBe( false );
expect( props.isSmall ).toBe( true );
expect( props.isLarge ).toBe( true );
expect( props.isLessThanSmall ).toBe( false );
wrapper.unmount();
} );
} );