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

E2E Framework: fix empty array check in RestAPIClient. #74741

Merged
merged 5 commits into from
Mar 22, 2023

Conversation

worldomonation
Copy link
Contributor

Related to #74479 (comment).

Proposed Changes

This PR fixes the empty array check introduced in #74479.

Testing Instructions

Pre-merge Checklist

  • Has the general commit checklist been followed? (PCYsg-hS-p2)
  • Have you written new tests for your changes?
  • Have you tested the feature in Simple (P9HQHe-k8-p2), Atomic (P9HQHe-jW-p2), and self-hosted Jetpack sites (PCYsg-g6b-p2)?
  • Have you checked for TypeScript, React or other console errors?
  • Have you used memoizing on expensive computations? More info in Memoizing with create-selector and Using memoizing selectors and Our Approach to Data
  • Have we added the "[Status] String Freeze" label as soon as any new strings were ready for translation (p4TIVU-5Jq-p2)?
  • For changes affecting Jetpack: Have we added the "[Status] Needs Privacy Updates" label if this pull request changes what data or activity we track or use (p4TIVU-ajp-p2)?

@github-actions
Copy link

github-actions bot commented Mar 21, 2023

@worldomonation worldomonation marked this pull request as ready for review March 21, 2023 21:20
@worldomonation worldomonation requested a review from a team as a code owner March 21, 2023 21:20
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Mar 21, 2023
@matticbot
Copy link
Contributor

This PR does not affect the size of JS and CSS bundles shipped to the user's browser.

Generated by performance advisor bot at iscalypsofastyet.com.

Copy link
Contributor

@noahtallen noahtallen left a comment

Choose a reason for hiding this comment

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

Thanks! There is actually one other example I forgot to mention:

if ( response.errors === [] ) {

Could you also add a fix for that?

@noahtallen noahtallen mentioned this pull request Mar 21, 2023
9 tasks
- fix another instance of comparison
@worldomonation
Copy link
Contributor Author

Thanks! There is actually one other example I forgot to mention:

if ( response.errors === [] ) {

Could you also add a fix for that?

Done - thanks for the heads up!

@@ -360,7 +360,7 @@ export class RestAPIClient {
);
}

if ( response.errors === [] ) {
if ( response.errors.length === 0 ) {
console.log( response );
throw new Error( `Failed to create invite: ${ response.errors }` );
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to check if there are errors here!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I didn't read the rest of the lines before making the change, but am a little confused at this logic now - I think we handled the error scenario in Line 357-360, so not sure why we're checking another error here.

Going to play with the console for a bit to see what is up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah okay, I should have written some comments because it isn't straightforward at first glance.

The first hasOwnProperty is checking for API errors.
The second error check is checking for errors relating to the invite itself.

I've added comment, fixed the logic for the if clause and added a helpful console.error level output.

- fix if logic.
- add output.
- add comments.
for ( const err of response.errors ) {
console.error( `${ err.code }: ${ err.message }` );
}
throw new Error( `Failed to create invite due to ${ response.errors.length } errors.` );
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice change, this makes a lot more sense to me 👍

throw new Error( `Failed to create invite: ${ response.errors }` );
// This handles "errors" relating to the invite itself and can be an array.
// For instance, if a user tries to invite itself, or invite an already added user.
if ( response.errors.length !== 0 ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpicky review comment alert :D

I kinda prefer > 0 to !== 0, but it doesn't make a practical difference. Or, you can even just do if ( response.errors.length ) for a quick truthy check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, I like that.

Copy link
Contributor

@karenroldan karenroldan left a comment

Choose a reason for hiding this comment

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

Nice catch on the empty array check!

@worldomonation worldomonation merged commit 1d87e23 into trunk Mar 22, 2023
@worldomonation worldomonation deleted the fix/rest-api-client-emtpy-array-check branch March 22, 2023 17:59
@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants