-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
test: Fix Cover block color picker test #52943
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||
/** | ||||
* External dependencies | ||||
*/ | ||||
import { Image } from 'react-native'; | ||||
import { Image, Pressable } from 'react-native'; | ||||
import { | ||||
getEditorHtml, | ||||
initializeEditor, | ||||
|
@@ -12,6 +12,7 @@ import { | |||
getBlock, | ||||
openBlockSettings, | ||||
} from 'test/helpers'; | ||||
import HsvColorPicker from 'react-native-hsv-color-picker'; | ||||
|
||||
/** | ||||
* WordPress dependencies | ||||
|
@@ -65,7 +66,6 @@ const COVER_BLOCK_CUSTOM_HEIGHT_HTML = `<!-- wp:cover {"url":"https://cldup.com/ | |||
const COLOR_PINK = '#f78da7'; | ||||
const COLOR_RED = '#cf2e2e'; | ||||
const COLOR_GRAY = '#abb8c3'; | ||||
const COLOR_WHITE = '#ffffff'; | ||||
const GRADIENT_GREEN = | ||||
'linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%)'; | ||||
|
||||
|
@@ -533,38 +533,36 @@ describe( 'color settings', () => { | |||
} ); | ||||
|
||||
it( 'displays the hex color value in the custom color picker', async () => { | ||||
HsvColorPicker.mockImplementation( ( props ) => { | ||||
return <Pressable { ...props } testID="hsv-color-picker" />; | ||||
} ); | ||||
const screen = await initializeEditor( { | ||||
initialHtml: COVER_BLOCK_PLACEHOLDER_HTML, | ||||
} ); | ||||
|
||||
const block = await screen.findByLabelText( 'Cover block. Empty' ); | ||||
expect( block ).toBeDefined(); | ||||
|
||||
// Select a color from the placeholder palette. | ||||
const colorPalette = await screen.findByTestId( 'color-palette' ); | ||||
const colorButton = within( colorPalette ).getByTestId( | ||||
'custom-color-picker' | ||||
const colorButton = screen.getByA11yHint( | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When possible, queries relying upon synchronous, user-facing text are generally preferred over asynchronous or test ID queries as they are more stable and simpler to understand. I applied this principle here and a few other places. I also removed the now unused test IDs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is a good tip, thank you. |
||||
'Navigates to custom color picker' | ||||
); | ||||
|
||||
expect( colorButton ).toBeDefined(); | ||||
fireEvent.press( colorButton ); | ||||
|
||||
// Wait for Block Settings to be visible. | ||||
const blockSettingsModal = screen.getByTestId( 'block-settings-modal' ); | ||||
await waitForModalVisible( blockSettingsModal ); | ||||
|
||||
// Assertion to check the text content of bottomLabelText before tapping color picker | ||||
const bottomLabelText = screen.getByTestId( | ||||
'color-picker-bottom-label-text' | ||||
); | ||||
expect( bottomLabelText ).toHaveTextContent( 'Select a color' ); | ||||
// Assert label text before tapping color picker | ||||
expect( screen.getByText( 'Select a color' ) ).toBeVisible(); | ||||
|
||||
// Tap color picker | ||||
const colorPicker = screen.getByTestId( 'color-picker' ); | ||||
fireEvent.press( colorPicker ); | ||||
const colorPicker = screen.getByTestId( 'hsv-color-picker' ); | ||||
fireEvent( colorPicker, 'onHuePickerPress', { | ||||
hue: 120, | ||||
saturation: 12, | ||||
value: 50, | ||||
} ); | ||||
|
||||
// Assertion to check the hex value in bottomLabelText | ||||
expect( bottomLabelText ).toHaveTextContent( COLOR_WHITE ); | ||||
// Assert label hex value after tapping color picker | ||||
expect( screen.getByText( '#00FF00' ) ).toBeVisible(); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When selecting white as the "custom" color, the following logic results in absence of a custom color as white is an available preset color. Therefore, I modified this to be a "random" color.
I am unsure if the above logic needs to be modified to support white and other preset colors... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just using white as a placeholder color here. I had yet to think through how we might actually select a color on the color picker within the test. I think using |
||||
} ); | ||||
} ); | ||||
|
||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,4 +202,6 @@ module.exports = { | |
embed__icon: { | ||
fill: 'black', | ||
}, | ||
picker: {}, | ||
pickerPointer: {}, | ||
Comment on lines
+205
to
+206
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to the way we rely upon Sass files, reference individual styles from them in JavaScript, and subsequently mock them in tests, we have to add individual style mocks in this file. It is quite cryptic and cumbersome. An alternative is mocking the values within the test file itself, which is arguably less cryptic. The inline mock is a recent practice introduced by @geriux, one that is likely worth embracing IMO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for pointing this out. This is the part I was missing. |
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This query and a few others appeared (arguably) unnecessary as later queries implicitly assert the presence of this element as children. In general, I try to use as few queries/assertions as necessary to keep test logic simple, but I do understand there is sometimes value in numerous assertions to communicate subtleties.
Please feel free to reinstate this query if you find it valuable, though.