Skip to content

Commit

Permalink
fix: displayed error message when download fails (#19038)
Browse files Browse the repository at this point in the history
A message is displayed when downloading a proposal document from a quote detail page fails. This was missing earlier.
  • Loading branch information
suprishi authored Jul 12, 2024
1 parent 411d1b1 commit 256359c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
3 changes: 2 additions & 1 deletion feature-libs/quote/assets/translations/en/quote.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@
"threshold": {
"underThresholdError": "Total price of requested quote does not meet the minimum threshold"
},
"expired": "This quote has expired. You must resubmit your quote request to receive another vendor quote."
"expired": "This quote has expired. You must resubmit your quote request to receive another vendor quote.",
"downloadPDFError": "Something went wrong. Unable to obtain the proposal document PDF."
},
"items": {
"regionTitle": "Cart items"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
I18nTestingModule,
Price,
FeatureConfigService,
GlobalMessageService,
HttpErrorModel,
Translatable,
GlobalMessageType,
} from '@spartacus/core';
import {
CartUtilsService,
Expand All @@ -24,7 +28,7 @@ import {
} from '@spartacus/quote/root';
import { FileDownloadService } from '@spartacus/storefront';
import { UrlTestingModule } from 'projects/core/src/routing/configurable-routes/url-translation/testing/url-testing.module';
import { BehaviorSubject, NEVER, Observable, of } from 'rxjs';
import { BehaviorSubject, NEVER, Observable, of, throwError } from 'rxjs';
import { createEmptyQuote } from '../../core/testing/quote-test-utils';
import { CommonQuoteTestUtilsService } from '../testing/common-quote-test-utils.service';
import { QuoteLinksComponent } from './quote-links.component';
Expand Down Expand Up @@ -59,6 +63,11 @@ const mockQuoteAttachment = (): File => {
return blob as File;
};

const errorResponse: HttpErrorModel = {
message: 'Bad request',
status: 400,
};

const mockQuoteDetails$ = new BehaviorSubject<Quote>(mockQuote);

class MockCommerceQuotesFacade implements Partial<QuoteFacade> {
Expand All @@ -84,6 +93,10 @@ class MockFeatureConfigService {
}
}

class MockGlobalMessageService implements Partial<GlobalMessageService> {
add(_: string | Translatable, __: GlobalMessageType, ___?: number): void {}
}

describe('QuoteLinksComponent', () => {
let fixture: ComponentFixture<QuoteLinksComponent>;
let htmlElem: HTMLElement;
Expand All @@ -93,6 +106,7 @@ describe('QuoteLinksComponent', () => {
let eventService: EventService;
let quoteFacade: QuoteFacade;
let fileDownloadService: FileDownloadService;
let globalMessageService: GlobalMessageService;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -119,6 +133,10 @@ describe('QuoteLinksComponent', () => {
provide: FeatureConfigService,
useClass: MockFeatureConfigService,
},
{
provide: GlobalMessageService,
useClass: MockGlobalMessageService,
},
],
}).compileComponents();
});
Expand All @@ -131,6 +149,7 @@ describe('QuoteLinksComponent', () => {
router = TestBed.inject(Router);
quoteFacade = TestBed.inject(QuoteFacade);
fileDownloadService = TestBed.inject(FileDownloadService);
globalMessageService = TestBed.inject(GlobalMessageService);
component = fixture.componentInstance;
mockQuoteDetails$.next(mockQuote);
fixture.detectChanges();
Expand Down Expand Up @@ -276,5 +295,29 @@ describe('QuoteLinksComponent', () => {
expect(spyDownload).toHaveBeenCalled();
});
});

it('should display error message when download fails', () => {
const spyDownloadAttachment = spyOn(
quoteFacade,
'downloadAttachment'
).and.returnValue(throwError(() => new Error(errorResponse.message)));
const spyMessage = spyOn(globalMessageService, 'add');
mockQuoteDetails$.next(vendorQuote);
fixture.detectChanges();
const downloadBtn = CommonQuoteTestUtilsService.getHTMLElement(
htmlElem,
'button'
);
downloadBtn.click();
fixture.detectChanges();
expect(spyDownloadAttachment).toHaveBeenCalledWith(
vendorQuote.code,
vendorQuote.code
);
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(spyMessage).toHaveBeenCalled();
});
});
});
});
22 changes: 17 additions & 5 deletions feature-libs/quote/components/links/quote-links.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
*/

import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { EventService, FeatureConfigService } from '@spartacus/core';
import {
EventService,
FeatureConfigService,
GlobalMessageService,
GlobalMessageType,
} from '@spartacus/core';
import {
CartUtilsService,
QuoteDetailsReloadQueryEvent,
Expand All @@ -25,6 +30,7 @@ export class QuoteLinksComponent {
protected eventService = inject(EventService);
protected fileDownloadService = inject(FileDownloadService);
private featureConfig = inject(FeatureConfigService);
protected globalMessageService = inject(GlobalMessageService);

quoteDetails$: Observable<Quote> = this.quoteFacade.getQuoteDetails();

Expand All @@ -47,12 +53,18 @@ export class QuoteLinksComponent {
onDownloadAttachment(quoteCode: string, attachments: QuoteAttachment[]) {
const attachmentId = attachments[0].id;
const filename = attachments[0].filename || attachmentId;
this.quoteFacade
.downloadAttachment(quoteCode, attachmentId)
.subscribe((res) => {
this.quoteFacade.downloadAttachment(quoteCode, attachmentId).subscribe({
next: (res) => {
const url = URL.createObjectURL(new Blob([res], { type: res.type }));
this.fileDownloadService.download(url, `${filename}.pdf`);
});
},
error: () => {
this.globalMessageService.add(
{ key: 'quote.httpHandlers.downloadPDFError' },
GlobalMessageType.MSG_TYPE_ERROR
);
},
});
}

/**
Expand Down

0 comments on commit 256359c

Please sign in to comment.