1

I'm writing jasmine unit test for following code:

this.confirmationService.confirm({
  message: 'Are you sure that you want to delete?',
  accept: () => {
   //some code to test
  }
});

How I can fake clicking of Yes in the dialog, to test code inside accept() function?

Antikhippe
  • 6,316
  • 2
  • 28
  • 43

2 Answers2

0

In your template:

<p-confirmDialog #confirmDialog header="Confirmation" icon="fa fa-question-circle" width="425"></p-confirmDialog>

In your component:

@ViewChild('confirmDialog') confirmDialog: ConfirmDialog;

then you're able to invoke confirmDialog.accept().

Dmitrij Kuba
  • 998
  • 9
  • 14
  • You shouldn't add additional component properties only for test. To test it properly you should mock confirm function and call fake accept function inside to simulate correct behavior – Nickon Sep 15 '19 at 22:35
0

You can create spy like this:

spyOn(confirmationService, 'confirm').and.callFake((params: Confirmation) => {
    params.accept();
});
Sujith
  • 165
  • 2
  • 6