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

Gif block: refactor and add test coverage #19066

Merged
merged 9 commits into from
Mar 15, 2021
Merged

Gif block: refactor and add test coverage #19066

merged 9 commits into from
Mar 15, 2021

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Mar 8, 2021

Changes proposed in this Pull Request:

After trying to add test coverage to the Gif block in #19019, we thought we'd preserve some sanity by refactoring the block in order to:

  • Make the code more readable.
  • Make it all more testable.
  • Make life more comfortable

Jetpack product discussion

p1HpG7-b3G-p2

Does this pull request change what data or activity we track or use?

Nope.

Testing instructions:

Make sure there are no visual or functional regressions in the editor or the save versions. Nothing should change in this respect.

Check that:

Mar-09-2021 22-07-25

Screen Shot 2021-03-09 at 10 06 22 pm

Run the unit tests:

cd projects/plugins/jetpack
yarn fixtures:test extensions/blocks/gif/test

Proposed changelog entry for your changes:

Not sure if a changelog is required for a refactor and tests. No functionality/UI has changed.

  • GIF Block: refactored edit.js and added test coverage

@matticbot
Copy link
Contributor

Caution: This PR has changes that must be merged to WordPress.com
Hello ramonjd! These changes need to be synced to WordPress.com - If you 're an a11n, please commandeer and confirm D58239-code works as expected before merging this PR. Once this PR is merged, please commit the changes to WP.com. Thank you!
This revision will be updated with each commit to this PR

@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label Mar 8, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Mar 8, 2021

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


If you are an automattician, once your PR is ready for review add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.undefined


jetpack plugin:

  • Next scheduled release: April 6, 2021.
  • Scheduled code freeze: March 29, 2021

@github-actions github-actions bot added the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label Mar 8, 2021
@ramonjd ramonjd requested a review from a team March 9, 2021 11:11
expect( container.querySelector( 'figure' ) ).toBeInTheDocument();
expect( container.querySelector( 'figcaption' ) ).toBeInTheDocument();
expect( container.querySelector( '.wp-block-jetpack-gif-wrapper iframe' ) ).toBeInTheDocument();
expect( container.querySelectorAll( '.wp-block-jetpack-gif_thumbnail-container' ) ).toHaveLength( 2 );
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually wondering if I should just to a toMatchSnapShot() here... 🤔

@ramonjd ramonjd force-pushed the refactor/gif-block branch 2 times, most recently from 1a5786b to e0326e5 Compare March 10, 2021 10:46
searchText
) }&api_key=${ encodeURIComponent( GIPHY_API_KEY ) }&limit=10`;
};
}, [ giphyData ] );
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm aware of the missing function dependency.

Here we only care about the currency of giphyData, which changes after every API update. Otherwise it won't fire.

setSelectedGiphy is used here and when we select a new thumbnail in the results so it's going to change the function reference.

As it is, the state is only updated once and when we desire it.

We could wrap it in a useCallback, I've tried this but I think it's an optimisation we can leave for later.

Copy link
Member

Choose a reason for hiding this comment

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

Could you then add an eslint-disable and a quick explanation in the code?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review @jeherve

I've had a change of heart and decided to appease the linter, and have extracted the method that does reference state/props. Reference: https://reactjs.org/docs/hooks-faq.html#is-it-safe-to-omit-functions-from-the-list-of-dependencies

It might save us some bugs in the future if this block is further developed :D

searchText
) }&api_key=${ encodeURIComponent( GIPHY_API_KEY ) }&limit=10`;
};
}, [ giphyData ] );
Copy link
Member

Choose a reason for hiding this comment

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

Could you then add an eslint-disable and a quick explanation in the code?

 Importing regenerator runtime to polyfill transpiled generator functions for react hooks testing library
 Added custom hook to fetch api data
 Added tests for custom hook and updated tests for edit.js
…edGiphyAttributes is not referencing any props or state so we can use it in the useEffect hook and not reference it.
@ramonjd ramonjd force-pushed the refactor/gif-block branch 4 times, most recently from 7b74cdf to dcdb32d Compare March 11, 2021 02:14
…s a little stricter than testing for the index of the domain.
Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

This tests well for me, nice rework! 🚢

@jeherve jeherve added this to the jetpack/9.6 milestone Mar 12, 2021
@jeherve jeherve added [Status] Ready to Merge Go ahead, you can push that green button! [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! [Status] Needs Team Review [Type] Task labels Mar 12, 2021
@ramonjd ramonjd merged commit 2c1efe3 into master Mar 15, 2021
@ramonjd ramonjd deleted the refactor/gif-block branch March 15, 2021 05:44
@matticbot matticbot removed the [Status] Ready to Merge Go ahead, you can push that green button! label Mar 15, 2021
@github-actions
Copy link
Contributor

Great news! One last step: head over to your WordPress.com diff, D58239-code, and commit it.
Once you've done so, come back to this PR and add a comment with your changeset ID.

Thank you!

@ramonjd
Copy link
Member Author

ramonjd commented Mar 15, 2021

r222722-wpcom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] GIF [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ Touches WP.com Files [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants