jeudi 19 avril 2018

Testing that ng-click will call the appropriate function in Jasmine test

So I am trying to create a test case,that will test whether a particular function would be called when ng-click is triggered.
Here is my Html code:

<a class="back-btn" accesskey="h" href="../home/index.html" ng-click="homeNavigate()" id="bahmniHome">  

Here is the code for homeNavigate() in the controller called PatientCommonController:

$rootScope.homeNavigate = function () {
            if ($scope.showSaveConfirmDialogConfig) {
                event.preventDefault();
                $scope.targetUrl = event.currentTarget.getAttribute('href');
                naviConfirmBox(event);
            }
        };

As seen above,homeNavigate() in turn calls naviConfirmBox().
I wanted to write a Test case that checks whether naviConfirmBox() is called when ng-click is triggered.
Here is the Jasmine test case :

'use strict';

describe('PatientCommonController', function () {

var $aController, $httpBackend, scope, appService, rootScope, patientAttributeService;
var spinner = jasmine.createSpyObj('spinner', ['forPromise']);

beforeEach(module('bahmni.registration', 'ngDialog'));

beforeEach(module(function ($provide) {
    $provide.value('patientAttributeService', {});
}));

beforeEach(
    inject(function ($controller, _$httpBackend_, $rootScope) {
        $aController = $controller;
        $httpBackend = _$httpBackend_;
        scope = $rootScope.$new();
        rootScope = $rootScope;
    })
);


beforeEach(function () {
    appService = jasmine.createSpyObj('appService', ['getAppDescriptor']);

    rootScope.genderMap = {};

    scope.patient = {};

    appService.getAppDescriptor = function () {
        return {
            getConfigValue: function (config) {
                return true;
            }

        };
    };

    $aController('PatientCommonController', {
        $scope: scope,
        $rootScope: rootScope,
        appService: appService
    });

    $httpBackend.whenGET(Bahmni.Common.Constants.globalPropertyUrl + '?property=concept.reasonForDeath').respond({});
    $httpBackend.when('GET', Bahmni.Common.Constants.conceptUrl).respond({});
    $httpBackend.flush();

});

it('checks if the confirmation popup is prompted when the home button is clicked when config is true', function() {
    var spyEvent = spyOnEvent('#bahmniHome', 'click');
    $('#bahmniHome').click()
    scope.showSaveConfirmDialogConfig = true;
    expect(naviConfirmBox()).toHaveBeenCalled();
});

it('checks if the confirmation popup is not prompted when the home button is clicked when config is false', function() {
    var spyEvent = spyOnEvent('#bahmniHome', 'click');
    $('#bahmniHome').click()
    scope.showSaveConfirmDialogConfig = false;
    expect(naviConfirmBox()).not.toHaveBeenCalled();
});

When I run the test I get the following error:

Firefox 52.0.0 (Linux 0.0.0) PatientCommonController checks if the confirmation popup is prompted when the home button is clicked when config is true FAILED
ReferenceError: naviConfirmBox is not defined in /home/krishnanspace/Projects_2/bahmni/openmrs-module-bahmniapps/ui/test/unit/registration/controllers/patientCommonController.spec.js (line 56)
@/home/krishnanspace/Projects_2/bahmni/openmrs-module-bahmniapps/ui/test/unit/registration/controllers/patientCommonController.spec.js:56:6
@/home/krishnanspace/Projects_2/bahmni/openmrs-module-bahmniapps/ui/test/unit/registration/controllers/createPatientController.spec.js:459:13

Firefox 52.0.0 (Linux 0.0.0) PatientCommonController checks if the confirmation popup is not prompted when the home button is clicked when config is false FAILED ReferenceError: naviConfirmBox is not defined in /home/krishnanspace/Projects_2/bahmni/openmrs-module-bahmniapps/ui/test/unit/registration/controllers/patientCommonController.spec.js (line 63) @/home/krishnanspace/Projects_2/bahmni/openmrs-module-bahmniapps/ui/test/unit/registration/controllers/patientCommonController.spec.js:63:6

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire