Skip to content

Commit

Permalink
fix(module:modal): no error stacks when nzOnOk/nzOnCancel is reject…
Browse files Browse the repository at this point in the history
…ed (#5561)


close #5321
  • Loading branch information
qqabcv520 authored Oct 9, 2020
1 parent afc83fc commit 6a4bddd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
21 changes: 11 additions & 10 deletions components/modal/modal-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ export class NzModalRef<T = NzSafeAny, R = NzSafeAny> implements NzModalLegacyAP
this.close(result);
}

triggerOk(): void {
this.trigger(NzTriggerAction.OK);
triggerOk(): Promise<void> {
return this.trigger(NzTriggerAction.OK);
}

triggerCancel(): void {
this.trigger(NzTriggerAction.CANCEL);
triggerCancel(): Promise<void> {
return this.trigger(NzTriggerAction.CANCEL);
}

/**
Expand Down Expand Up @@ -164,7 +164,7 @@ export class NzModalRef<T = NzSafeAny, R = NzSafeAny> implements NzModalLegacyAP
return this.overlayRef.backdropElement;
}

private trigger(action: NzTriggerAction): void {
private async trigger(action: NzTriggerAction): Promise<void> {
const trigger = { ok: this.config.nzOnOk, cancel: this.config.nzOnCancel }[action];
const loadingKey = { ok: 'nzOkLoading', cancel: 'nzCancelLoading' }[action] as 'nzOkLoading' | 'nzCancelLoading';
const loading = this.config[loadingKey];
Expand All @@ -175,16 +175,17 @@ export class NzModalRef<T = NzSafeAny, R = NzSafeAny> implements NzModalLegacyAP
trigger.emit(this.getContentComponent());
} else if (typeof trigger === 'function') {
const result = trigger(this.getContentComponent());
const caseClose = (doClose: boolean | void | {}) => doClose !== false && this.close(doClose as R);
if (isPromise(result)) {
this.config[loadingKey] = true;
const handleThen = (doClose: boolean | void | {}) => {
let doClose: boolean | void | {} = false;
try {
doClose = await result;
} finally {
this.config[loadingKey] = false;
this.closeWhitResult(doClose);
};
result.then(handleThen).catch(handleThen);
}
} else {
caseClose(result);
this.closeWhitResult(result);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions components/modal/modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,34 @@ describe('NzModal', () => {
expect(modalRef.getConfig().nzOkLoading).toBe(false);
expect(overlayContainerElement.querySelectorAll('nz-modal-container').length).toBe(1);

modalRef.close();
fixture.detectChanges();
flush();
expect(overlayContainerElement.querySelectorAll('nz-modal-container').length).toBe(0);
}));
it('should not close when the callback is return Promise.reject', fakeAsync(() => {
const modalRef = modalService.create({
nzContent: TestWithModalContentComponent,
nzOnOk: () => {
return new Promise((_, reject) => {
setTimeout(() => {
reject('Promise.reject');
}, 200);
});
}
});
fixture.detectChanges();

expectAsync(modalRef.triggerOk()).toBeRejectedWith('Promise.reject');
fixture.detectChanges();
expect(modalRef.getConfig().nzOkLoading).toBe(true);
expect(overlayContainerElement.querySelectorAll('nz-modal-container').length).toBe(1);
tick(200);
fixture.detectChanges();
flush();
expect(modalRef.getConfig().nzOkLoading).toBe(false);
expect(overlayContainerElement.querySelectorAll('nz-modal-container').length).toBe(1);

modalRef.close();
fixture.detectChanges();
flush();
Expand Down

0 comments on commit 6a4bddd

Please sign in to comment.