From 69f3569ccfa384d193eeda514f9b2af26ecca04b Mon Sep 17 00:00:00 2001 From: ncoronel Date: Tue, 18 Aug 2026 09:18:45 -0300 Subject: [PATCH] feat(product-ticket-selector): enhance variant selection logic and update test cases for improved accuracy --- .../product-ticket-selector.component.html | 12 +++++-- .../product-ticket-selector.component.spec.ts | 19 ++++++----- .../product-ticket-selector.component.ts | 32 +++---------------- 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.html b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.html index f8d68f0..1327487 100644 --- a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.html +++ b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.html @@ -28,12 +28,18 @@ class="form-select variant-selector__select" [attr.aria-label]="selector.label" [disabled]="rowDisabled(row)" - [value]="selectedOptionKey(row, selector.key)" (change)="onSelectionKeyChange(row.id, selector, $any($event.target).value)" > - + @for (option of selector.options; track $index) { - + } } diff --git a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.spec.ts b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.spec.ts index 8826152..57338b0 100644 --- a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.spec.ts +++ b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.spec.ts @@ -228,17 +228,20 @@ describe('ProductTicketSelectorComponent', () => { { key: 'sector', label: 'Sector', - options: [{ value: 'a', label: 'Sector A' }], + options: [ + { value: 'a', label: 'Sector A' }, + { value: 'b', label: 'Sector B' }, + ], enabled: true, }, - { key: 'seat', label: 'Seat', options: ['1'], enabled: true }, + { key: 'seat', label: 'Seat', options: ['1', '2'], enabled: true }, ], - selected_values: { sector: 'a', seat: '1' }, + selected_values: { sector: 'b', seat: '2' }, resolved_variant: { id: 401, precio: '10000.00', stock_tecnico: 1, - values: { sector: { value: 'a', label: 'Sector A' }, seat: '1' }, + values: { sector: { value: 'b', label: 'Sector B' }, seat: '2' }, }, }), ), @@ -267,22 +270,22 @@ describe('ProductTicketSelectorComponent', () => { fixture.detectChanges(); expect(catalogService.getVariantOptions).toHaveBeenCalledWith(7, { - selected_values: { sector: 'a', seat: '1' }, + selected_values: {}, cart_item_id: 25, }); expect(fixture.componentInstance['rows']()[0]).toMatchObject({ variantId: 401, reservedVariantId: 401, cartItemId: 25, - selectedValues: { sector: 'a', seat: '1' }, + selectedValues: { sector: 'b', seat: '2' }, status: 'reserved', }); expect(cartService.addItem).not.toHaveBeenCalled(); expect(cartService.updateItemVariant).not.toHaveBeenCalled(); const selects = Array.from(fixture.nativeElement.querySelectorAll('select')); expect(selects.map((select) => select.selectedOptions[0]?.textContent?.trim())).toEqual([ - 'Sector A', - '1', + 'Sector B', + '2', ]); }); diff --git a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.ts b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.ts index 7b46f55..f3db64d 100644 --- a/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.ts +++ b/src/app/shared/components/product-ticket-selector/product-ticket-selector.component.ts @@ -16,7 +16,7 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { Subscription } from 'rxjs'; import { CartService } from '../../../core/services/cart/cart.service'; -import { CartItem, CartItemVariantValue } from '../../../core/services/cart/cart.interface'; +import { CartItem } from '../../../core/services/cart/cart.interface'; import { ModalService } from '../../../core/services/modal.service'; import { ToastService } from '../../../core/services/toast.service'; import { @@ -455,19 +455,14 @@ export class ProductTicketSelectorComponent { private createCartRow(item: CartItem, id = this.nextRowId++): TicketSelectionRow | null { if (item.variant_id === null) return null; - const variant = item.variant; - if (!variant || variant.id !== item.variant_id) return null; - - const selectedValues = this.normalizeCartValues(variant.values); - return { id, variantId: item.variant_id, reservedVariantId: item.variant_id, cartItemId: item.id, - selectedValues, + selectedValues: {}, selectors: [], - reservedSelectedValues: selectedValues, + reservedSelectedValues: {}, reservedSelectors: [], status: 'checking', error: null, @@ -495,11 +490,7 @@ export class ProductTicketSelectorComponent { if (!cartRow) return []; - if ( - existingRow && - existingRow.reservedVariantId === cartRow.reservedVariantId && - this.sameSelectedValues(existingRow.selectedValues, cartRow.selectedValues) - ) { + if (existingRow && existingRow.reservedVariantId === cartRow.reservedVariantId) { return [existingRow]; } @@ -543,21 +534,6 @@ export class ProductTicketSelectorComponent { this.rows.set(rows); } - private normalizeCartValues( - values: Record, - ): Record { - return Object.fromEntries( - Object.entries(values).map(([key, value]) => [key, this.normalizeValue(value)]), - ); - } - - private sameSelectedValues( - left: Record, - right: Record, - ): boolean { - return JSON.stringify(left) === JSON.stringify(right); - } - private findRow(rowId: number): TicketSelectionRow | undefined { return this.rows().find((row) => row.id === rowId); }