Compare commits
3 Commits
163444c91f
...
0726b2219e
| Author | SHA1 | Date | |
|---|---|---|---|
| 0726b2219e | |||
| 7845c8f20e | |||
| c41c60f349 |
@@ -41,9 +41,15 @@
|
||||
<app-icon-button
|
||||
variant="download"
|
||||
ariaLabel="Descargar ticket"
|
||||
[disabled]="isGeneratingPdf()"
|
||||
(clicked)="download.emit()"
|
||||
/>
|
||||
<app-icon-button variant="share" ariaLabel="Compartir ticket" (clicked)="share.emit()" />
|
||||
<app-icon-button
|
||||
variant="share"
|
||||
ariaLabel="Compartir ticket"
|
||||
[disabled]="isGeneratingPdf()"
|
||||
(clicked)="share.emit()"
|
||||
/>
|
||||
}
|
||||
</div>
|
||||
</article>
|
||||
|
||||
@@ -16,6 +16,7 @@ export class TicketComponent {
|
||||
readonly date = input<string | null>(null);
|
||||
readonly disabled = input(false);
|
||||
readonly expired = input(false);
|
||||
readonly isGeneratingPdf = input(false);
|
||||
readonly selected = model(false);
|
||||
|
||||
readonly viewQr = output<void>();
|
||||
|
||||
@@ -30,4 +30,14 @@ export class TicketService {
|
||||
this.http.get<{ data: TicketResponse[] }>(`${this.tenantService.getTenantApiUrl()}/tickets`),
|
||||
).then((response) => response.data ?? []);
|
||||
}
|
||||
|
||||
downloadPdf(ticketIds: number[]): Promise<Blob> {
|
||||
return firstValueFrom(
|
||||
this.http.post(
|
||||
`${this.tenantService.getTenantApiUrl()}/tickets/pdf`,
|
||||
{ ticket_ids: ticketIds },
|
||||
{ responseType: 'blob' },
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
<section class="tickets-page">
|
||||
<h1 class="tickets-page__title">MIS TICKETS</h1>
|
||||
|
||||
<div class="tickets-list" aria-busy="{{ isLoading() }}">
|
||||
<div class="tickets-list" [attr.aria-busy]="isLoading() || isGeneratingPdf()">
|
||||
@if (isGeneratingPdf()) {
|
||||
<div class="tickets-list__generating" role="status" aria-live="polite">
|
||||
<span class="tickets-list__generating-spinner" aria-hidden="true"></span>
|
||||
<span>Generando PDF...</span>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (activeTickets().length) {
|
||||
<header class="tickets-list__header">
|
||||
<label class="tickets-list__select-all">
|
||||
@@ -15,11 +22,21 @@
|
||||
|
||||
<div class="tickets-list__bulk-actions">
|
||||
<span class="tickets-list__bulk-action">
|
||||
<app-icon-button variant="download" ariaLabel="Descargar tickets seleccionados" />
|
||||
<app-icon-button
|
||||
variant="download"
|
||||
ariaLabel="Descargar tickets seleccionados"
|
||||
[disabled]="isGeneratingPdf()"
|
||||
(clicked)="downloadSelected()"
|
||||
/>
|
||||
<small>Descargar</small>
|
||||
</span>
|
||||
<span class="tickets-list__bulk-action">
|
||||
<app-icon-button variant="share" ariaLabel="Compartir tickets seleccionados" />
|
||||
<app-icon-button
|
||||
variant="share"
|
||||
ariaLabel="Compartir tickets seleccionados"
|
||||
[disabled]="isGeneratingPdf()"
|
||||
(clicked)="shareSelected()"
|
||||
/>
|
||||
<small>Compartir</small>
|
||||
</span>
|
||||
</div>
|
||||
@@ -60,8 +77,11 @@
|
||||
[ticketId]="ticket.id"
|
||||
[date]="formatDate(ticket)"
|
||||
[selected]="isSelected(ticket.id)"
|
||||
[isGeneratingPdf]="isGeneratingPdf()"
|
||||
(selectedChange)="toggleTicket(ticket.id, $event)"
|
||||
(viewQr)="viewQr(ticket)"
|
||||
(download)="downloadTicket(ticket)"
|
||||
(share)="shareTicket(ticket)"
|
||||
/>
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,35 @@
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.tickets-list {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.tickets-list__generating {
|
||||
position: absolute;
|
||||
z-index: 10;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
min-height: 180px;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 12px;
|
||||
border-radius: 8px;
|
||||
color: #26322a;
|
||||
font-size: 14px;
|
||||
font-weight: 700;
|
||||
backdrop-filter: blur(2px);
|
||||
}
|
||||
|
||||
.tickets-list__generating-spinner {
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
border: 3px solid #dce8e0;
|
||||
border-top-color: var(--tenant-primary, #009933);
|
||||
border-radius: 50%;
|
||||
animation: tickets-spinner 0.75s linear infinite;
|
||||
}
|
||||
|
||||
.tickets-list__header {
|
||||
display: flex;
|
||||
min-height: 58px;
|
||||
@@ -170,6 +199,12 @@
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes tickets-spinner {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 767.98px) {
|
||||
.tickets-page__title {
|
||||
margin-top: 24px;
|
||||
|
||||
@@ -26,6 +26,122 @@ const ticket = (overrides: Partial<TicketResponse>): TicketResponse => ({
|
||||
});
|
||||
|
||||
describe('TicketsPage', () => {
|
||||
it('shares the generated PDF as a file without text that Windows could prioritize', async () => {
|
||||
const share = vi.fn().mockResolvedValue(undefined);
|
||||
const canShare = vi.fn().mockReturnValue(true);
|
||||
vi.stubGlobal('navigator', { canShare, share });
|
||||
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [TicketsPage],
|
||||
providers: [{ provide: ToastService, useValue: { danger: vi.fn() } }],
|
||||
})
|
||||
.overrideComponent(TicketsPage, {
|
||||
set: {
|
||||
providers: [
|
||||
{
|
||||
provide: TicketService,
|
||||
useValue: {
|
||||
getTickets: () => Promise.resolve([ticket({})]),
|
||||
downloadPdf: () => Promise.resolve(new Blob(['pdf-content'])),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
const fixture = TestBed.createComponent(TicketsPage);
|
||||
fixture.detectChanges();
|
||||
await fixture.whenStable();
|
||||
fixture.detectChanges();
|
||||
|
||||
const page = fixture.nativeElement as HTMLElement;
|
||||
page.querySelector<HTMLButtonElement>('[aria-label="Compartir ticket"]')?.click();
|
||||
|
||||
await vi.waitFor(() => expect(share).toHaveBeenCalledOnce());
|
||||
|
||||
const shareData = share.mock.calls[0][0] as ShareData;
|
||||
const [sharedFile] = shareData.files ?? [];
|
||||
|
||||
expect(Object.keys(shareData)).toEqual(['files']);
|
||||
expect(canShare).toHaveBeenCalledWith({ files: [sharedFile] });
|
||||
expect(sharedFile).toBeInstanceOf(File);
|
||||
expect(sharedFile.name).toBe('tickets_1.pdf');
|
||||
expect(sharedFile.type).toBe('application/pdf');
|
||||
expect(sharedFile.size).toBeGreaterThan(0);
|
||||
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('shows a loading overlay and disables PDF actions while the file is generated', async () => {
|
||||
let resolvePdf!: (pdf: Blob) => void;
|
||||
const downloadPdf = vi.fn(
|
||||
() =>
|
||||
new Promise<Blob>((resolve) => {
|
||||
resolvePdf = resolve;
|
||||
}),
|
||||
);
|
||||
const createObjectUrl = vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:ticket');
|
||||
const revokeObjectUrl = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined);
|
||||
let downloadedFilename = '';
|
||||
const linkClick = vi
|
||||
.spyOn(HTMLAnchorElement.prototype, 'click')
|
||||
.mockImplementation(function (this: HTMLAnchorElement) {
|
||||
downloadedFilename = this.download;
|
||||
});
|
||||
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [TicketsPage],
|
||||
providers: [{ provide: ToastService, useValue: { danger: vi.fn() } }],
|
||||
})
|
||||
.overrideComponent(TicketsPage, {
|
||||
set: {
|
||||
providers: [
|
||||
{
|
||||
provide: TicketService,
|
||||
useValue: {
|
||||
getTickets: () => Promise.resolve([ticket({})]),
|
||||
downloadPdf,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
const fixture = TestBed.createComponent(TicketsPage);
|
||||
fixture.detectChanges();
|
||||
await fixture.whenStable();
|
||||
fixture.detectChanges();
|
||||
|
||||
const page = fixture.nativeElement as HTMLElement;
|
||||
page.querySelector<HTMLButtonElement>('[aria-label="Descargar ticket"]')?.click();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(page.querySelector('.tickets-list__generating')?.textContent).toContain(
|
||||
'Generando PDF...',
|
||||
);
|
||||
expect(page.querySelector('.tickets-list')?.getAttribute('aria-busy')).toBe('true');
|
||||
expect(page.querySelector<HTMLButtonElement>('[aria-label="Descargar ticket"]')?.disabled).toBe(
|
||||
true,
|
||||
);
|
||||
expect(page.querySelector<HTMLButtonElement>('[aria-label="Compartir ticket"]')?.disabled).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
resolvePdf(new Blob(['pdf'], { type: 'application/pdf' }));
|
||||
await fixture.whenStable();
|
||||
await vi.waitFor(() => expect(downloadedFilename).toBe('tickets_1.pdf'));
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(page.querySelector('.tickets-list__generating')).toBeNull();
|
||||
expect(page.querySelector('.tickets-list')?.getAttribute('aria-busy')).toBe('false');
|
||||
|
||||
createObjectUrl.mockRestore();
|
||||
revokeObjectUrl.mockRestore();
|
||||
linkClick.mockRestore();
|
||||
});
|
||||
|
||||
it('hides the active tickets header when there are no available tickets', async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [TicketsPage],
|
||||
|
||||
@@ -27,6 +27,7 @@ export class TicketsPage implements OnInit {
|
||||
);
|
||||
protected readonly selectedIds = signal<Set<number>>(new Set());
|
||||
protected readonly isLoading = signal(true);
|
||||
protected readonly isGeneratingPdf = signal(false);
|
||||
protected readonly allSelected = computed(
|
||||
() =>
|
||||
this.activeTickets().length > 0 && this.selectedIds().size === this.activeTickets().length,
|
||||
@@ -86,4 +87,91 @@ export class TicketsPage implements OnInit {
|
||||
ticket: ticket.ticket,
|
||||
});
|
||||
}
|
||||
|
||||
protected async downloadTicket(ticket: TicketResponse): Promise<void> {
|
||||
await this.downloadTickets([ticket]);
|
||||
}
|
||||
|
||||
protected async downloadSelected(): Promise<void> {
|
||||
const tickets = this.activeTickets().filter((ticket) => this.selectedIds().has(ticket.id));
|
||||
|
||||
if (!tickets.length) {
|
||||
this.toastService.danger('Seleccioná al menos un ticket para descargar.');
|
||||
return;
|
||||
}
|
||||
|
||||
await this.downloadTickets(tickets);
|
||||
}
|
||||
|
||||
protected async shareTicket(ticket: TicketResponse): Promise<void> {
|
||||
await this.shareTickets([ticket]);
|
||||
}
|
||||
|
||||
protected async shareSelected(): Promise<void> {
|
||||
const tickets = this.activeTickets().filter((ticket) => this.selectedIds().has(ticket.id));
|
||||
|
||||
if (!tickets.length) {
|
||||
this.toastService.danger('Seleccioná al menos un ticket para compartir.');
|
||||
return;
|
||||
}
|
||||
|
||||
await this.shareTickets(tickets);
|
||||
}
|
||||
|
||||
private async downloadTickets(tickets: TicketResponse[]): Promise<void> {
|
||||
const pdf = await this.generateTicketsPdf(tickets);
|
||||
if (pdf) this.downloadPdf(pdf, this.pdfFilename(tickets));
|
||||
}
|
||||
|
||||
private async shareTickets(tickets: TicketResponse[]): Promise<void> {
|
||||
const pdf = await this.generateTicketsPdf(tickets);
|
||||
if (!pdf) return;
|
||||
|
||||
try {
|
||||
const filename = this.pdfFilename(tickets);
|
||||
const file = new File([pdf], filename, { type: 'application/pdf' });
|
||||
|
||||
if (navigator.canShare?.({ files: [file] })) {
|
||||
await navigator.share({ files: [file] });
|
||||
return;
|
||||
}
|
||||
|
||||
this.downloadPdf(pdf, filename);
|
||||
this.toastService.success('Tu navegador no permite compartir archivos; descargamos el PDF.');
|
||||
} catch (error) {
|
||||
if (error instanceof DOMException && error.name === 'AbortError') return;
|
||||
|
||||
this.toastService.danger('No se pudo compartir el PDF de los tickets.');
|
||||
}
|
||||
}
|
||||
|
||||
private async generateTicketsPdf(tickets: TicketResponse[]): Promise<Blob | null> {
|
||||
if (this.isGeneratingPdf()) return null;
|
||||
|
||||
this.isGeneratingPdf.set(true);
|
||||
|
||||
try {
|
||||
return await this.ticketService.downloadPdf(tickets.map((ticket) => ticket.id));
|
||||
} catch {
|
||||
this.toastService.danger('No se pudo generar el PDF de los tickets.');
|
||||
return null;
|
||||
} finally {
|
||||
this.isGeneratingPdf.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
private pdfFilename(tickets: TicketResponse[]): string {
|
||||
return `tickets_${tickets.map((ticket) => ticket.id).join('_')}.pdf`;
|
||||
}
|
||||
|
||||
private downloadPdf(pdf: Blob, filename: string): void {
|
||||
const url = URL.createObjectURL(pdf);
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
window.setTimeout(() => URL.revokeObjectURL(url), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
<div class="checkout-page">
|
||||
<div
|
||||
class="checkout-page__stepper-col"
|
||||
[class.checkout-page__stepper-col--editing]="isEditingItems()"
|
||||
[attr.aria-hidden]="isEditingItems()"
|
||||
[attr.inert]="isEditingItems() ? '' : null"
|
||||
>
|
||||
<app-stepper #stepper>
|
||||
@if (isLoadingPurchase()) {
|
||||
<div class="checkout-page__loading" role="status" aria-live="polite">
|
||||
<span class="checkout-page__spinner" aria-hidden="true"></span>
|
||||
<span>Cargando compra...</span>
|
||||
</div>
|
||||
} @else {
|
||||
<div class="checkout-page">
|
||||
<div
|
||||
class="checkout-page__stepper-col"
|
||||
[class.checkout-page__stepper-col--editing]="isEditingItems()"
|
||||
[attr.aria-hidden]="isEditingItems()"
|
||||
[attr.inert]="isEditingItems() ? '' : null"
|
||||
>
|
||||
<app-stepper #stepper [initialStepIndex]="checkoutStepIndex()">
|
||||
<app-step label="Datos" [isValid]="isStep1Valid()">
|
||||
<app-checkout-data-step
|
||||
[form]="form"
|
||||
@@ -34,32 +40,33 @@
|
||||
(retryQrPolling)="retryQrPolling()"
|
||||
/>
|
||||
</app-step>
|
||||
</app-stepper>
|
||||
</div>
|
||||
|
||||
@if (isEditingItems()) {
|
||||
<div class="checkout-page__editing-notice" role="status">
|
||||
<p>Terminá de modificar las cantidades para continuar con el pago.</p>
|
||||
</app-stepper>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="checkout-page__cart-col">
|
||||
<app-cart
|
||||
title="COMPRA"
|
||||
[items]="mappedCartItems()"
|
||||
[subtotal]="cartSubtotal()"
|
||||
[discount]="cartDiscount()"
|
||||
[total]="cartTotal()"
|
||||
[allowEditing]="
|
||||
createdPurchase()?.status === 'created' || createdPurchase()?.status === 'pending_payment'
|
||||
"
|
||||
[allowRemove]="false"
|
||||
[persistQuantityChanges]="false"
|
||||
[editing]="isEditingItems()"
|
||||
[editingDisabled]="isUpdatingItem() || isPreparingItemEdit()"
|
||||
backgroundColor="transparent"
|
||||
(editingChange)="onEditingItemsChange($event)"
|
||||
(itemQuantityChange)="onPurchaseItemQuantityChange($event)"
|
||||
/>
|
||||
@if (isEditingItems()) {
|
||||
<div class="checkout-page__editing-notice" role="status">
|
||||
<p>Terminá de modificar las cantidades para continuar con el pago.</p>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="checkout-page__cart-col">
|
||||
<app-cart
|
||||
title="COMPRA"
|
||||
[items]="mappedCartItems()"
|
||||
[subtotal]="cartSubtotal()"
|
||||
[discount]="cartDiscount()"
|
||||
[total]="cartTotal()"
|
||||
[allowEditing]="
|
||||
createdPurchase()?.status === 'created' || createdPurchase()?.status === 'pending_payment'
|
||||
"
|
||||
[allowRemove]="false"
|
||||
[persistQuantityChanges]="false"
|
||||
[editing]="isEditingItems()"
|
||||
[editingDisabled]="isUpdatingItem() || isPreparingItemEdit()"
|
||||
backgroundColor="transparent"
|
||||
(editingChange)="onEditingItemsChange($event)"
|
||||
(itemQuantityChange)="onPurchaseItemQuantityChange($event)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -36,10 +36,33 @@
|
||||
}
|
||||
|
||||
&__cart-col {
|
||||
|
||||
max-height: 80vh;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
|
||||
.checkout-page__loading {
|
||||
display: grid;
|
||||
min-height: 420px;
|
||||
place-items: center;
|
||||
align-content: center;
|
||||
gap: 1rem;
|
||||
color: #666666;
|
||||
}
|
||||
|
||||
.checkout-page__spinner {
|
||||
width: 3rem;
|
||||
height: 3rem;
|
||||
border: 0.25rem solid rgba(32, 32, 32, 0.08);
|
||||
border-top-color: var(--tenant-primary);
|
||||
border-radius: 50%;
|
||||
animation: checkout-page-spin 0.8s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes checkout-page-spin {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,6 +234,9 @@ describe('CheckoutPageComponent payment validation', () => {
|
||||
const purchase = {
|
||||
id: 25,
|
||||
cart_id: null,
|
||||
status: 'created',
|
||||
payment_method: null,
|
||||
transfer_payer_dni: null,
|
||||
nombre_apellido: 'Datos de la compra',
|
||||
email: 'compra@example.com',
|
||||
dni: '11111111',
|
||||
@@ -293,6 +296,99 @@ describe('CheckoutPageComponent payment validation', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps the checkout hidden while the purchase is loading', async () => {
|
||||
let resolvePurchase!: (purchase: any) => void;
|
||||
routeQueryParamMap = convertToParamMap({ purchase: 25 });
|
||||
checkoutServiceStub.getPurchase.mockReturnValue(
|
||||
new Promise((resolve) => {
|
||||
resolvePurchase = resolve;
|
||||
}),
|
||||
);
|
||||
const { component } = createComponent();
|
||||
|
||||
expect(component.isLoadingPurchase()).toBe(true);
|
||||
|
||||
resolvePurchase({
|
||||
id: 25,
|
||||
status: 'created',
|
||||
payment_method: null,
|
||||
transfer_payer_dni: null,
|
||||
items: [],
|
||||
subtotal: '0.00',
|
||||
total: '0.00',
|
||||
});
|
||||
await Promise.resolve();
|
||||
|
||||
expect(component.isLoadingPurchase()).toBe(false);
|
||||
expect(component.checkoutStepIndex()).toBe(0);
|
||||
});
|
||||
|
||||
it('opens a pending purchase on the payment step and restores its payment method', async () => {
|
||||
routeQueryParamMap = convertToParamMap({ purchase: 25 });
|
||||
checkoutServiceStub.getPurchase.mockResolvedValue({
|
||||
id: 25,
|
||||
status: 'pending_payment',
|
||||
payment_method: 'transfer',
|
||||
transfer_payer_dni: '12345678',
|
||||
items: [],
|
||||
subtotal: '100.00',
|
||||
total: '100.00',
|
||||
});
|
||||
const { component } = createComponent();
|
||||
await Promise.resolve();
|
||||
|
||||
expect(component.checkoutStepIndex()).toBe(1);
|
||||
expect(component.selectedPaymentMethod()).toBe('transfer');
|
||||
expect(component.transferDni()).toBe('12345678');
|
||||
expect(component.isLoadingPurchase()).toBe(false);
|
||||
});
|
||||
|
||||
it('generates a new QR when reopening a pending QR purchase', async () => {
|
||||
routeQueryParamMap = convertToParamMap({ purchase: 25 });
|
||||
checkoutServiceStub.getPurchase.mockResolvedValue({
|
||||
id: 25,
|
||||
status: 'pending_payment',
|
||||
payment_method: 'qr',
|
||||
transfer_payer_dni: null,
|
||||
items: [],
|
||||
subtotal: '100.00',
|
||||
total: '100.00',
|
||||
});
|
||||
const { component } = createComponent();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
expect(component.checkoutStepIndex()).toBe(1);
|
||||
expect(component.selectedPaymentMethod()).toBe('qr');
|
||||
expect(checkoutServiceStub.generatePaymentIntent).toHaveBeenCalledWith(
|
||||
'tenant-test',
|
||||
25,
|
||||
'qr',
|
||||
);
|
||||
expect(component.qrData()).toBe('qr-value');
|
||||
expect(component.qrPaymentStatus()).toBe('waiting');
|
||||
});
|
||||
|
||||
it.each(['paid', 'cancelled', 'rejected', 'expired'])(
|
||||
'redirects a %s purchase to its status page',
|
||||
async (status) => {
|
||||
routeQueryParamMap = convertToParamMap({ purchase: 25 });
|
||||
checkoutServiceStub.getPurchase.mockResolvedValue({
|
||||
id: 25,
|
||||
status,
|
||||
payment_method: null,
|
||||
transfer_payer_dni: null,
|
||||
items: [],
|
||||
subtotal: '100.00',
|
||||
total: '100.00',
|
||||
});
|
||||
createComponent();
|
||||
await Promise.resolve();
|
||||
|
||||
expect(routerStub.navigate).toHaveBeenCalledWith(['/checkout/status', 25]);
|
||||
},
|
||||
);
|
||||
|
||||
it('updates a purchase item while editing and refreshes checkout totals', async () => {
|
||||
const updatedPurchase = {
|
||||
id: 25,
|
||||
@@ -318,12 +414,7 @@ describe('CheckoutPageComponent payment validation', () => {
|
||||
quantity: 3,
|
||||
});
|
||||
|
||||
expect(checkoutServiceStub.updateItemQuantity).toHaveBeenCalledWith(
|
||||
'tenant-test',
|
||||
25,
|
||||
91,
|
||||
3,
|
||||
);
|
||||
expect(checkoutServiceStub.updateItemQuantity).toHaveBeenCalledWith('tenant-test', 25, 91, 3);
|
||||
expect(component.createdPurchase()).toBe(updatedPurchase);
|
||||
expect(component.isUpdatingItem()).toBe(false);
|
||||
});
|
||||
|
||||
@@ -77,6 +77,8 @@ export class CheckoutPageComponent implements OnInit, OnDestroy {
|
||||
});
|
||||
|
||||
protected readonly createdPurchase = signal<PurchaseDetailResponse | null>(null);
|
||||
protected readonly isLoadingPurchase = signal(true);
|
||||
protected readonly checkoutStepIndex = signal(0);
|
||||
|
||||
protected readonly cartSubtotal = computed(() => {
|
||||
const purchase = this.createdPurchase();
|
||||
@@ -198,10 +200,7 @@ export class CheckoutPageComponent implements OnInit, OnDestroy {
|
||||
|
||||
this.isPreparingItemEdit.set(true);
|
||||
try {
|
||||
const purchase = await this.checkoutService.prepareItemEditing(
|
||||
tenant.codigo,
|
||||
purchaseId,
|
||||
);
|
||||
const purchase = await this.checkoutService.prepareItemEditing(tenant.codigo, purchaseId);
|
||||
this.createdPurchase.set(purchase);
|
||||
} catch (error) {
|
||||
console.error('Failed to prepare purchase item editing:', error);
|
||||
@@ -574,8 +573,39 @@ export class CheckoutPageComponent implements OnInit, OnDestroy {
|
||||
|
||||
try {
|
||||
const purchase = await this.checkoutService.getPurchase(tenant.codigo, purchaseId);
|
||||
|
||||
if (
|
||||
purchase.status === 'paid' ||
|
||||
purchase.status === 'cancelled' ||
|
||||
purchase.status === 'rejected' ||
|
||||
purchase.status === 'expired'
|
||||
) {
|
||||
this.navigateToPurchaseStatus(purchaseId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (purchase.status !== 'created' && purchase.status !== 'pending_payment') {
|
||||
void this.router.navigate(['/']);
|
||||
return;
|
||||
}
|
||||
|
||||
this.createdPurchaseId.set(purchase.id);
|
||||
this.createdPurchase.set(purchase);
|
||||
this.checkoutStepIndex.set(purchase.status === 'pending_payment' ? 1 : 0);
|
||||
|
||||
if (
|
||||
purchase.status === 'pending_payment' &&
|
||||
this.paymentMethods.some((method) => method.id === purchase.payment_method)
|
||||
) {
|
||||
this.selectedPaymentMethod.set(purchase.payment_method as PaymentMethod);
|
||||
this.transferDni.set(purchase.transfer_payer_dni ?? '');
|
||||
}
|
||||
|
||||
this.isLoadingPurchase.set(false);
|
||||
|
||||
if (purchase.status === 'pending_payment' && purchase.payment_method === 'qr') {
|
||||
await this.selectPaymentMethod('qr');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load purchase:', error);
|
||||
void this.router.navigate(['/']);
|
||||
|
||||
@@ -14,7 +14,7 @@ import { StepComponent } from './step.component';
|
||||
<div id="content-2">Content 2</div>
|
||||
</app-step>
|
||||
</app-stepper>
|
||||
`
|
||||
`,
|
||||
})
|
||||
class TestHostComponent {
|
||||
@ViewChild('stepper') stepper!: StepperComponent;
|
||||
@@ -25,7 +25,7 @@ class TestHostComponent {
|
||||
describe('StepperComponent & StepComponent', () => {
|
||||
async function setup() {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [TestHostComponent, StepperComponent, StepComponent]
|
||||
imports: [TestHostComponent, StepperComponent, StepComponent],
|
||||
}).compileComponents();
|
||||
|
||||
const fixture = TestBed.createComponent(TestHostComponent);
|
||||
@@ -50,6 +50,18 @@ describe('StepperComponent & StepComponent', () => {
|
||||
expect(content2).toBeNull();
|
||||
});
|
||||
|
||||
it('can initialize on a specified step', async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [StepperComponent, StepComponent],
|
||||
}).compileComponents();
|
||||
|
||||
const fixture = TestBed.createComponent(StepperComponent);
|
||||
fixture.componentRef.setInput('initialStepIndex', 1);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.componentInstance.currentStepIndex()).toBe(1);
|
||||
});
|
||||
|
||||
it('advances to step 2 when next() is called and current step is valid', async () => {
|
||||
const { fixture, component } = await setup();
|
||||
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { ChangeDetectionStrategy, Component, contentChildren, signal } from '@angular/core';
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
contentChildren,
|
||||
input,
|
||||
linkedSignal,
|
||||
} from '@angular/core';
|
||||
import { StepComponent } from './step.component';
|
||||
import { NgClass } from '@angular/common';
|
||||
|
||||
@@ -11,7 +17,8 @@ import { NgClass } from '@angular/common';
|
||||
})
|
||||
export class StepperComponent {
|
||||
readonly steps = contentChildren(StepComponent);
|
||||
readonly currentStepIndex = signal(0);
|
||||
readonly initialStepIndex = input(0);
|
||||
readonly currentStepIndex = linkedSignal(() => this.initialStepIndex());
|
||||
|
||||
next() {
|
||||
const currentSteps = this.steps();
|
||||
|
||||
Reference in New Issue
Block a user