jeudi 30 juin 2016

How do I fake a "click to continue" in the middle of a save method for testing?

The below code is in the body of the save method and the clickHandler for the toaster finishes the save process. So, what I need to do is fake a click during my save method. I know how to retrieve an element and fire a click, but I don't know how to do it correctly for this particular test.

return promise.then(() => {

    // warn user
    if (!mustPromptUser) {
        ctrl.toasterService.warning({
            body: "A message",
            timeout: 0,
            showCloseButton: true,
            clickHandler: onSaveFunc, <--- must assert this happened
            bodyOutputType: "trustedHtml"
        });
    } else {
        onSaveFunc();
    }
}).catch(() => {
    // some code
});

Here is the test:

    it("Can I get this damned test working", () => {
        var controller = initializeController(model) as MyController;
        controller.form = {
            $pristine: true,
            $setPristine: null
        } as ng.IFormController;
        spyOn(controller, "onSave");
        spyOn(controller.form, "$setPristine");
        spyOn(service, "create");
        controller.save();
        $timeout.flush();

        expect(service.create).toHaveBeenCalled();
        expect(toaster.warning).toHaveBeenCalled();
        // This is the assertion that's failing because the user has to click to call this method in the toaster clickHandler. 
        expect(controller.onSave).toHaveBeenCalled();
        expect(controller.form.$pristine).toBeTruthy();

    });

Aucun commentaire:

Enregistrer un commentaire