Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewport: Refactor ifViewportMatches to use RTL #44766

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 15 additions & 28 deletions packages/viewport/src/test/if-viewport-matches.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,45 @@
/**
* External dependencies
*/
import TestRenderer, { act } from 'react-test-renderer';
import { render, screen } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { useViewportMatch as useViewportMatchMock } from '@wordpress/compose';
import { useViewportMatch } from '@wordpress/compose';

/**
* Internal dependencies
*/
import '../store';
import ifViewportMatches from '../if-viewport-matches';

jest.mock( '@wordpress/compose/src/hooks/use-viewport-match' );

describe( 'ifViewportMatches()', () => {
const Component = () => <div>Hello</div>;

afterEach( () => {
useViewportMatchMock.mockClear();
useViewportMatch.mockClear();
} );

it( 'should not render if query does not match', () => {
useViewportMatchMock.mockReturnValueOnce( false );
useViewportMatch.mockReturnValueOnce( false );
const EnhancedComponent = ifViewportMatches( '< wide' )( Component );
render( <EnhancedComponent /> );

let testRenderer;
act( () => {
testRenderer = TestRenderer.create( <EnhancedComponent /> );
} );

expect( useViewportMatchMock.mock.calls ).toEqual( [
[ 'wide', '<' ],
] );
expect( useViewportMatch ).toHaveBeenCalledWith( 'wide', '<' );

expect( testRenderer.root.findAllByType( Component ) ).toHaveLength(
0
);
expect( screen.queryByText( 'Hello' ) ).not.toBeInTheDocument();
} );

it( 'should render if query does match', () => {
useViewportMatchMock.mockReturnValueOnce( true );
useViewportMatch.mockReturnValueOnce( true );
const EnhancedComponent = ifViewportMatches( '>= wide' )( Component );
let testRenderer;
act( () => {
testRenderer = TestRenderer.create( <EnhancedComponent /> );
} );

expect( useViewportMatchMock.mock.calls ).toEqual( [
[ 'wide', '>=' ],
] );

expect( testRenderer.root.findAllByType( Component ) ).toHaveLength(
1
);
render( <EnhancedComponent /> );

expect( useViewportMatch ).toHaveBeenCalledWith( 'wide', '>=' );

expect( screen.queryByText( 'Hello' ) ).toBeInTheDocument();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to point out that query* queries are recommended only when we're querying for elements that don't exist. In case they are expected to exist, we use get* queries. See this post from the library creator as the rationale for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} );
} );