-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
ColorPicker: Add component test for Copy button with clipboard mocking #69223
base: trunk
Are you sure you want to change the base?
ColorPicker: Add component test for Copy button with clipboard mocking #69223
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Hi @Mamaduka, I have added a test for the copy functionality in the ColorPicker component. When you have a moment, please review my PR. As a follow-up, I can create a comprehensive list of components where similar functionality exists, so we can add tests for them as well. I believe this is a commonly used feature, and adding tests for it would be beneficial. Let me know what you think. cc: @chad1008 |
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.
Thanks, @im3dabasia!
I'm not sure if the current shape of the test is proper. It's mocking the readText
to return the color
value and then asserts that it equals the same color
value.
It seems that we're not actually testing anything.
Hi @Mamaduka, Thank you for your feedback! While it may seem like we aren’t testing much, we are indeed verifying the copy functionality in a way that aligns with how the clipboard works in the testing environment. Since navigator.clipboard is not available by default in a test environment (as it requires a secure context), I had to mock the readText API. After researching similar challenges that other developers faced, I found useful references, including [Stack Overflow link1], [Stack Overflow link2], and [a blog post]. Quoting the documentation: Related reference I initially tried a different approach, assuming that since userEvent.setup({writeToClipboard:true}) mocks the clipboard API, we could directly store the copied value from the internal mock and compare it. However, when I attempted this, the stored value ended up being undefined, meaning this method didn’t work as expected. const color = '#000';
const user = userEvent.setup( { writeToClipboard: true } );
render( <ColorPicker color={ color } enableAlpha={ false } /> );
const copyButton = screen.getByRole( 'button', { name: 'Copy' } );
expect( copyButton ).toBeVisible();
await user.click( copyButton );
const pastedText = await user.paste();
expect( pastedText ).toBe( color ); Would love to hear your thoughts on this! Let me know if you have any suggestions or if there's a better way to handle this. |
Maybe @WordPress/gutenberg-components have a suggestion here. |
After a quick look, I also agree with @Mamaduka that we're not really testing the copy functionality. If we do provide a mock, I feel like it should be a more feature-full mock, able to "copy" and "store" the copied text, and return it when the clipboard is "read"— ie. without any values being hardcoded. I also wanted to point out that the copy button uses the |
Thank you, @ciampo! After looking into it further, I discovered that Thank you @ciampo and @Mamaduka for your wonderful insights. EDIT: To correctly simulate the entire copy workflow, we need to mock both I will soon have the code polished and send it for review. |
Since it seems that this is a lot of work to mock, and ColorPicker is just a consumer of these upstream clipboard utilities, another strategy that comes to mind here is to simply assert that jest.mock( '@wordpress/compose', () => ( {
...jest.requireActual( '@wordpress/compose' ),
useCopyToClipboard: jest.fn(),
} ) ); // there might be a cleaner way to do this 😅
const value = useCopyToClipboard.mock.calls[ 0 ][ 0 ]();
expect( value ).toBe( '#000' ); Something like that? And then maybe we can do the heavy duty testing for Just a thought. |
I appreciate your feedback! I will explore this idea as well. I've pushed a couple of commits where I simulated an actual copy. Could you please check them when you have a moment? |
I agree with @mirka — for the purpose of |
Work in Progress
Follow-up PR for: #49698 (comment)
What?
Adds a component test for the Copy button in the ColorPicker component, ensuring clipboard functionality is properly mocked and tested.
Why?
The Copy button functionality wasn't previously tested. This PR introduces a test to verify that the correct color value is copied to the clipboard when the button is clicked.
How?
navigator.clipboard.readText
usingjest.spyOn
.Testing Instructions
Screenshot