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

Fix race condition when rendering product attributes tab empty state #38354

Merged
merged 19 commits into from May 19, 2023

Conversation

mattsherman
Copy link
Contributor

@mattsherman mattsherman commented May 18, 2023

Submission Review Guidelines:

Changes proposed in this Pull Request:

This PR fixes a race condition that could occur when the first (empty) attribute form was automatically added to the attributes tab of the product (the updated empty state from #38126).

Basically, in an environment such as our e2e tests, where the UI interactions occurred very quickly, the new attribute form would be added using a stale value of the product type, resulting in a missing "Used for variations" checkbox. Note that this is very unlikely to be reproducible manually, as humans just aren't that fast when interacting with the UI.

Flow in code that caused issue:

  • Product edit screen loads
  • Ajax call to get new attribute form (empty state) was automatically made, with product type Simple
  • Product type was switched to Variable by user
  • Controls were shown/hidden based on updated product type
  • Ajax call to get new attribute form returned, and used the stale product type Simple to determine if "Used for variations" on the newly added attribute should be shown (thus hiding it)

The implementation has been updated to check the product type immediately before showing/hiding control on the new attribute form.

In addition, a number of refactors have been made to make this area of the code simpler and more maintainable, to lessen the chance of similar issues occurring in the future when further changes to the code are made.

There are no visible UI changes introduced in this PR, outside of the fixing of the visibility of the "Used for variations" checkbox.

How to test the changes in this Pull Request:

As this bug was introduced in #38126, the same test instructions from there should be followed (see below).

In addition, general smoke testing surrounding the following should be done:

  • Changing product type, and making sure controls/tabs show/hide appropriately

Common instructions

  • To create a new product, go to Products > Add New
  • To edit an existing product, go to Products > All Products
  • To add or remove global attributes, go to Products > Attributes
  • To save attributes, click Save attributes button on the Attributes tab
  • To save a product, click Publish (or Update, if an existing product)
  • To see Tracks events
    • Tools > WCA Test Helper > Tools > Enable wc-admin Tracking
    • Filter by recordevent in the browser dev console

No global attributes in system

  1. Remove all global attributes from the system
  2. Create a new product
  3. Go to the Attributes tab
  4. Verify that the empty attribute form is shown
  5. Fill out the attribute and save it
  6. Save the product and verify the attributes are correct
  7. Reload the product and verify the attributes are correct

Global attributes in system, adding a new local attribute

  1. Add at least one global attribute to the system, and configure terms for it
  2. Create a new product
  3. Go to the Attributes tab
  4. Verify that the empty attribute form is shown
  5. Fill out the attribute and save it
  6. Save the product and verify the attributes are correct
  7. Reload the product and verify the attributes are correct

Global attributes in system, adding a global attribute

  1. Add at least one global attribute to the system, and configure terms for it
  2. Create a new product
  3. Go to the Attributes tab
  4. Verify that the empty attribute form is shown
  5. Select a global attribute from the Add existing dropdown
  6. Verify the empty attribute form is removed
  7. Verify the same global attribute cannot be selected again from the Add existing dropdown
  8. Select terms for the global attribute and save it
  9. Save the product and verify the attributes are correct
  10. Reload the product and verify the attributes are correct

Editing an existing product with no attributes

  1. Edit one of your existing products no attributes
  2. Verify the empty attribute form is automatically shown when the Attributes tab is shown

Editing an existing product with local attributes

  1. Edit one of your existing products with local attributes (either saved from above or before)
  2. Verify the empty attribute form is not automatically shown when the Attributes tab is shown
  3. Verify the attributes are correct

Editing an existing product with global attributes

  1. Edit one of your existing products with global attributes (either saved from above or before)
  2. Verify the empty attribute form is not automatically shown when the Attributes tab is shown
  3. Verify the attributes are correct

Tracks events

  1. Clicking on Add new should log wcadmin_product_attributes_buttons with action: 'add_new'
  2. Clicking on an existing attribute in Add existing should log wcadmin_product_attributes_buttons with action: 'add_existing'
  3. Clicking on Create value for a global attribute should log wcadmin_product_attributes_add_term

Error handling

  1. Clicking on Add new when offline (or any other server-side error), should show a browser alert message and log the error to the browser console log.

@github-actions github-actions bot added focus: e2e tests Issues related to e2e tests plugin: woocommerce Issues related to the WooCommerce Core plugin. labels May 18, 2023
@mattsherman mattsherman changed the title Fix race condition when adding 1st attribute form to the product Fix race condition when rendering product attributes tab empty state May 18, 2023
@mattsherman mattsherman added type: bug The issue is a confirmed bug. focus: product management Related to product creation and editing. labels May 18, 2023
@mattsherman mattsherman self-assigned this May 18, 2023
@mattsherman mattsherman requested a review from a team May 18, 2023 20:05
@github-actions
Copy link
Contributor

github-actions bot commented May 18, 2023

Hi @octaedro,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

@github-actions
Copy link
Contributor

github-actions bot commented May 18, 2023

Test Results Summary

Commit SHA: 2b58151

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests26700202690m 53s
E2E Tests1940010020416m 58s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@mattsherman mattsherman marked this pull request as ready for review May 18, 2023 20:38
Comment on lines 761 to 767
// we've seen some flakiness where the "Used for variations" checkbox is not visible,
// so let's explicitly wait for it to be visible before proceeding
await page
.locator( '#product_attributes' )
.locator( `input[name="attribute_variation[${ i }]"]` )
.waitFor( { timeout: 10000 } );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rodelgc I added this explicit wait for the "Used for variations" checkbox to help assure us that the underlying issue is fixed. That way, if it is missing, the test will fail with a timeout. Or, do you think we either don't need this at all, or should make this an expect?

@octaedro octaedro requested review from octaedro and removed request for a team May 19, 2023 12:08

attribute_row_indexes();
async function add_attribute_to_list( globalAttributeId ) {
try {
Copy link
Contributor

@octaedro octaedro May 19, 2023

Choose a reason for hiding this comment

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

I think that if we will use exception handling, we should add a catch to this try.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't add a catch because I don't have anything terribly helpful to do at this point, so I figured I'd just let the error bubble up. I did the finally because because I didn't want to totally lock the UI up in this failure case.

I could put up an alert... do you think that is good enough?

Copy link
Contributor

@octaedro octaedro May 19, 2023

Choose a reason for hiding this comment

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

I could put up an alert... do you think that is good enough?

An alert mentioning the error sounds good to me 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I'll make that change and fix the conflicting test file right after lunch and ping you when ready for another review.

Copy link
Contributor

@octaedro octaedro left a comment

Choose a reason for hiding this comment

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

Good job @mattsherman! This is testing well here and the code looks good. I just left a small comment.

@mattsherman mattsherman force-pushed the fix/product-add-attributes-race-condition branch from a759995 to 2b58151 Compare May 19, 2023 17:52
@github-actions github-actions bot removed the focus: e2e tests Issues related to e2e tests label May 19, 2023
@mattsherman mattsherman requested a review from octaedro May 19, 2023 18:08
@mattsherman
Copy link
Contributor Author

@octaedro I added the alert.

You can test by loading the product screen and then using your browser's dev tools Network conditions > Network throttling set to Offline. Click "Add new" and you will trigger the fail case.

I removed the e2e test change I made, as the e2e tests have been restructured and looking at the new structure of them, I don't see a need to add my check back in. If we happen to have future issues, the existing e2e tests should cover things.

Ready for re-review!

Copy link
Contributor

@octaedro octaedro left a comment

Choose a reason for hiding this comment

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

Thank you @mattsherman for adding the change. The PR LGTM 🚀

@mattsherman mattsherman merged commit 08d7c24 into trunk May 19, 2023
17 of 18 checks passed
@mattsherman mattsherman deleted the fix/product-add-attributes-race-condition branch May 19, 2023 23:15
@github-actions github-actions bot added this to the 7.8.0 milestone May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: product management Related to product creation and editing. plugin: woocommerce Issues related to the WooCommerce Core plugin. type: bug The issue is a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants