jasmine.createSpyObj in jest for MatDialog

Viewed 134

I'm have an Angular app and I'm moving from Jasmine/Karm to Jest. I have this function:

changeClientStatus(status: any, data: any, index: any) {
    const formValues = { ClientId: data.ClientId, Status: status };

    if (status === 'Cancelled' || status === 'Transferred') {
        const dialogRef = this.dialog.open(ConfirmationDialogComponent, { data: MESSAGES.UpdateJobsForCancelledClients });

        dialogRef.afterClosed().subscribe((resp) => {
            if (resp === true) {
                this.updateClientStatus(formValues, index);
            }
        });
    } else {
        this.updateClientStatus(formValues, index);
    }
}

My current unit test is:

describe('changeClientStatus', () => {
    beforeEach(() => {
        let dialogSpy: jasmine.Spy;
        const dialogRefSpyObj = jasmine.createSpyObj({
            afterClosed: of(true),
            close: null,
        });
        dialogRefSpyObj.componentInstance = { body: '' };
        dialogSpy = spyOn(TestBed.inject(MatDialog), 'open').and.returnValue(dialogRefSpyObj);
    });

    it('should call changeClientStatus', () => {
        component.clientList = objClientList;
        spyOn(component, 'changeClientStatus').and.callThrough();
        component.changeClientStatus('Transferred', { ClientId: 123 }, 0);

        expect(component.changeClientStatus).toHaveBeenCalledWith('Transferred', { ClientId: 123 }, 0);

        component.changeClientStatus('Active', { ClientId: 123 }, 0);

        expect(component.changeClientStatus).toHaveBeenCalledWith('Active', { ClientId: 123 }, 0);
    });
});

The jasmine.createSpyObj obviously needs to be changed. But I'm unsure how to go about doing this with Jest. I tried following this with no success what is the equivalent of jasmine.createSpyObj() for jest?

0 Answers
Related