jeudi 2 août 2018

How to access function inside controller which is inside directive? Everything returns undefined

This is my directive mySchedule.directive.js

'use strict';
angular.module('myApp')
.directive('mySchedule', function () {
    return {
        restrict: 'E',
        scope: {
            mine: '='
        },
        templateUrl: 'App/directives/mySchedule.html?v={version}',

        controller: ['$scope', 'utils', '$location',
            function ($scope, utils, $location) {
                $scope.navigateToMySchedule = function (name) {
                    utils.showConfirmationDialog(myResources.resourceText.alert_Navigation, myResources.resourceText.confirmDialog_LeaveCurrentPage, 'Leave page', 'Stay on page').result.then(function () {
                        $location.path("/myCar/").search("route", name);
                    //on ok button press 
                    // end on ok button press
                }, function () {
                    //on cancel button press
                });
            }
        }]
    };
});

And this is my testing file mySchedule.directive.spec.js

/// <reference path="../../../chutzpah.conf.js" />
'use strict';


describe('mySchedule Directive', function () {
var $httpBackend;
var $scope;
var $compile;
var utilsService;
var $location;
var controller;
var $controller;

beforeEach(module('myApp', function ($provide) {

}));

beforeEach(
    inject(function ($rootScope, _$compile_, $injector, _$location_, utils, _$controller_) {
        $httpBackend = $injector.get('$httpBackend');
        $httpBackend.whenGET('App/directives/mySchedule.html').respond(200, 'OK');
        $scope = $rootScope.$new();
        $compile = _$compile_;
        $location = _$location_;
        $controller = _$controller_;
        utilsService = utils
        utilsService.showconfirmationdialog = function () {
        return true;
        }
    })
);

afterEach(function () {
    $httpBackend.verifyNoOutstandingExpectation();
});

it('should redirect to my schedule', function () {
    var element = angular.element('<my-schedule></my-schedule');
    var testcase = "name"
    element = $compile(element)($scope);
    //$httpBackend.flush();
    $scope.$digest();

    // NOW NONE OF THESE LINES AFTER THIS WORK

    element.isolateScope().navigateToMySchedule(testcase);
    var controller = $controller('mySchedule', { '$scope': $scope });
    $scope.navigateToMySchedule(testcase);
    controller.navigateToMySchedule(testcase);
    expect(location.path).toEqual("/route=name");
});

});

controller is undefined element.isolateScope() is undefined $scope.navigateToMySchedule(testcase) gives Object doesn't support property or method 'navigateToMySchedule' error

I have spent almost 2 days on this and couldn't figure out what to do. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire