2
votes

While writing unit tests for a function that handles a angular material 2 dialog using the example code from material 2 i run into problems. I'm a Jasmine newbie but I didn't had problems to write unit test before. I have to test the result of the afterClose function but i can't get the handle to dialogRef.

Could it be a problem how the material2 dialog API is engineered?

        let dialogRef = this.dialog.open(ExtractPageDialog, {
            width: this.EXPORT_DIALOG_WIDTH,
            data: {
                document: this.document
            }
        });
        dialogRef.afterClosed().subscribe((result: any) => {
            if (result) {
                let fileId = this.document.fileId;
                this.docProvider.extractPage(this.document.fileId, result.fromPage, result.toPage).subscribe(() => {
                   () => { //totest },
                   (error) => { //totest }
                });
            } else {
                //totest
            }
        });

DOCS: https://material.angular.io/components/component/dialog

1

1 Answers

0
votes

one solution could be to split the subscribed function in multiple functions and test them

dialogRef.afterClosed().subscribe(this.functionName);

functionName(result: any) {
                if (result) {
                    let fileId = this.document.fileId;
                    this.docProvider.extractPage(this.document.fileId, result.fromPage, result.toPage).subscribe(() => {
                       () => { //totest },
                       (error) => { //totest }
                    });
                } else {
                    //totest
                }
            }
}