I'm currently working on building a unit test that verifies the correct variable is being passed to the resolve
property within the ui.bootstrap.modal
from Angular-UI Bootstrap components. Here's my progress so far:
// Controller
angular.module('app')
.controller('WorkflowListCtrl', function ($scope, $modal) {
// Setting up an edit callback to open a modal
$scope.edit = function(name) {
var modalInstance = $modal.open({
templateUrl: 'partials/editWorkflowModal.html',
controller: 'WorkflowEditCtrl',
scope: $scope,
resolve: {
name: function() { return name; }
}
});
};
});
An important detail is that the resolve.name
property must be a function for the Angular-UI component to function correctly - I had previously attempted resolve: { name: name }
but it didn't produce the desired outcome.
// Unit Test
describe('Controller: WorkflowListCtrl', function () {
// Loading the controller's module
beforeEach(module('app'));
var workflowListCtrl,
scope,
modal;
// Initializing the controller and creating a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
modal = {
open: jasmine.createSpy()
};
workflowListCtrl = $controller('WorkflowListCtrl', {
$scope: scope,
$modal: modal
});
it('should allow a workflow to be edited', function() {
// Editing workflow triggers a modal.
scope.edit('Barney Rubble');
expect(modal.open).toHaveBeenCalledWith({
templateUrl: 'partials/editWorkflowModal.html',
controller: 'WorkflowEditCtrl',
scope: scope,
resolve: {
name: jasmine.any(Function)
}
});
});
}));
});
Currently, the test validates that the resolve.name
property is a function, but ideally, I would like to confirm that the resolve.name
function indeed returns Barney Rubble
. However, this syntax doesn't yield the expected result:
expect(modal.open).toHaveBeenCalledWith({
templateUrl: 'partials/editWorkflowModal.html',
controller: 'WorkflowEditCtrl',
scope: scope,
resolve: {
name: function() { return 'Barney Rubble'; }
}
});
It appears that I need to somehow spy on the resolve.name
function to ensure it was invoked with Barney Rubble
, but I'm struggling to find a solution. Any suggestions?