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

Block Editor: Refactor withColors tests to @testing-library/react #43976

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
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

This file was deleted.

49 changes: 37 additions & 12 deletions packages/block-editor/src/components/colors/test/with-colors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* External dependencies
*/
import { shallow, mount } from 'enzyme';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

/**
* Internal dependencies
Expand All @@ -13,18 +14,37 @@ describe( 'createCustomColorsHOC', () => {
const withCustomColors = createCustomColorsHOC( [
{ name: 'Red', slug: 'red', color: 'ff0000' },
] );
const EnhancedComponent = withCustomColors( 'backgroundColor' )( () => (
<div />
) );
const BaseComponent = jest.fn( () => <div /> );
const EnhancedComponent =
withCustomColors( 'backgroundColor' )( BaseComponent );

const wrapper = shallow(
render(
<EnhancedComponent attributes={ { backgroundColor: null } } />
);

expect( wrapper.dive() ).toMatchSnapshot();
expect( BaseComponent ).toHaveBeenCalledWith(
expect.objectContaining( {
attributes: {
backgroundColor: null,
},
backgroundColor: {
class: undefined,
color: undefined,
},
colorUtils: {
getMostReadableColor: expect.any( Function ),
},
colors: undefined,
setBackgroundColor: expect.any( Function ),
} ),
expect.anything()
);
} );

it( 'setting the color to a value in the provided custom color array updated the backgroundColor attribute', () => {
it( 'setting the color to a value in the provided custom color array updated the backgroundColor attribute', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const withCustomColors = createCustomColorsHOC( [
{ name: 'Red', slug: 'red', color: 'ff0000' },
] );
Expand All @@ -38,21 +58,25 @@ describe( 'createCustomColorsHOC', () => {

const setAttributes = jest.fn();

const wrapper = mount(
render(
<EnhancedComponent
attributes={ { backgroundColor: null } }
setAttributes={ setAttributes }
/>
);

wrapper.find( 'button' ).simulate( 'click' );
await user.click( screen.getByRole( 'button' ) );

expect( setAttributes ).toHaveBeenCalledWith( {
backgroundColor: 'red',
customBackgroundColor: undefined,
} );
} );

it( 'setting the color to a value not in the provided custom color array updates customBackgroundColor attribute', () => {
it( 'setting the color to a value not in the provided custom color array updates customBackgroundColor attribute', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const withCustomColors = createCustomColorsHOC( [
{ name: 'Red', slug: 'red', color: 'ff0000' },
] );
Expand All @@ -66,14 +90,15 @@ describe( 'createCustomColorsHOC', () => {

const setAttributes = jest.fn();

const wrapper = mount(
render(
<EnhancedComponent
attributes={ { backgroundColor: null } }
setAttributes={ setAttributes }
/>
);

wrapper.find( 'button' ).simulate( 'click' );
await user.click( screen.getByRole( 'button' ) );

expect( setAttributes ).toHaveBeenCalledWith( {
backgroundColor: undefined,
customBackgroundColor: '000000',
Expand Down