Skip to content

[CDX-392] Render data-cnstrc-item-variation-id even if getSwatches is invalid#213

Merged
Mudaafi merged 1 commit intomainfrom
cdx-392-plp-ui-return-data-cnstrc-item-variation-id-attribute
Mar 2, 2026
Merged

[CDX-392] Render data-cnstrc-item-variation-id even if getSwatches is invalid#213
Mudaafi merged 1 commit intomainfrom
cdx-392-plp-ui-return-data-cnstrc-item-variation-id-attribute

Conversation

@Mudaafi
Copy link
Contributor

@Mudaafi Mudaafi commented Feb 27, 2026

PR Description

useProductInfo doesn't return variationId if useSwatches doesn't successfully return a swatch list

Pull Request Checklist

Before you submit a pull request, please make sure you have to following:

  • I have added or updated TypeScript types for my changes, ensuring they are compatible with the existing codebase.
  • I have added JSDoc comments to my TypeScript definitions for improved documentation.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added any necessary documentation (if appropriate).
  • I have made sure my PR is up-to-date with the main branch.

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no API changes)
  • Documentation content changes
  • TypeScript type definitions update
  • Other... Please describe:

@Mudaafi Mudaafi requested a review from a team February 27, 2026 00:04
@Mudaafi Mudaafi requested a review from HHHindawy as a code owner February 27, 2026 00:04
Copilot AI review requested due to automatic review settings February 27, 2026 00:04
@constructor-claude-bedrock
Copy link

Code Review Results

✅ Strengths

The fix is minimal, targeted, and correctly identifies the root cause: when getSwatches throws, useProductSwatch leaves selectedVariation undefined, silently dropping variationId from the hook's return value.

🚨 Critical Issues

None.

⚠️ Important Issues

  • [File: src/hooks/useProduct.ts Line: 36] The fallback uses the || (logical OR) operator rather than ?? (nullish coalescing). If selectedVariation.variationId is ever an empty string "", the OR operator treats it as falsy and falls back to item.variationId, silently overriding a legitimate (if empty) variation ID. All other similar fallbacks in this file (e.g., itemName, itemPrice, itemImageUrl) intentionally use || to treat empty strings as absent, so if the same semantic is desired here, it should be documented. Otherwise, prefer `??":

    // If an empty variationId from a selected variation should still be respected:
    const variationId = productSwatch?.selectedVariation?.variationId ?? item.variationId;

💡 Suggestions

  • [File: spec/hooks/useProductInfo/useProductInfo.test.js Lines: 134–154] The new test is a focused addition, but the existing test 'Should return nothing properly with getters that throw errors' (line 97) already configures a throwing getSwatches and exercises the same code path. Consider adding the variationId assertion to that existing test rather than duplicating the getSwatches throwing setup, to keep the test surface consolidated.

  • [File: spec/hooks/useProductInfo/useProductInfo.test.js Lines: 134–154] The new test only covers the error path for getSwatches. A complementary test for the normal path — verifying that when getSwatches succeeds and a variation is selected, variationId returns selectedVariation.variationId (not the item-level fallback) — would make the contract of the new line explicit.

Overall Assessment: ⚠️ Needs Work

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug where the data-cnstrc-item-variation-id attribute was not being rendered when the getSwatches function throws an error. The fix adds a fallback to use item.variationId when productSwatch.selectedVariation.variationId is unavailable, following the same pattern already established for other fields like itemName, itemPrice, and itemImageUrl.

Changes:

  • Added fallback logic in useProductInfo hook to use item.variationId when productSwatch.selectedVariation.variationId is undefined
  • Added test coverage to verify the fallback behavior when getSwatches throws an error
  • Applied code formatting improvements to existing test cases

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/hooks/useProduct.ts Added fallback to item.variationId for the variationId variable, consistent with existing fallback patterns for other fields
spec/hooks/useProductInfo/useProductInfo.test.js Added new test case to verify fallback behavior when getSwatches throws an error, and applied formatting improvements to existing tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@Alexey-Pavlov Alexey-Pavlov left a comment

Choose a reason for hiding this comment

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

LGTM!

@Mudaafi Mudaafi merged commit 0e63789 into main Mar 2, 2026
15 of 16 checks passed
@Mudaafi Mudaafi deleted the cdx-392-plp-ui-return-data-cnstrc-item-variation-id-attribute branch March 2, 2026 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants