mardi 5 avril 2016

AngularJS Unit Test Controller with a promise

I have the following code defined in my controller

(function() {
    'use strict';

     angular
      .module('bolt')
      .controller('PackdownController', PackdownController);

      PackdownController.$inject = ['$location', '$timeout', 'packdownService', 'modelTransformer', 'Packdown'];

function PackdownController($location, $timeout, packdownService, modelTransformer, Packdown) {
var vm = this;  

vm.totalItemCount = 0;
vm.completedItemCount = 0;
vm.toDoItems = [];
vm.completedItems = [];

activatePackdown();

function activatePackdown() {
    packdownService.GetAllTasks().then(function(data) {
        if(data !== undefined && data !== null) {
            if(data.error.state) {
                    showError('Error retrieving tasks', 'Please try again. If the problem persists, contact your supervisor', 'Close', toggleErrorModal);
            }
            else {

                    vm.totalItemCount = tasks.openItems + tasks.closeItems;
                    vm.completedItemCount = tasks.closeItems;
                        angular.forEach(tasks.bayTasks, function(value, key) {
                            if(value.status.description !== 'COMPLETED_THIS_CYCLE') {
                                    var task = {
                                            deptNbr: value.department.number,
                                            bayName: value.aisle + '-' + value.bay,
                                            status: {
                                                code: value.status.code,
                                                description: value.status.description
                                            }
                                    };
                                    this.push(task);
                            }
                        }, vm.toDoItems);
                        angular.forEach(tasks.bayTasks, function(value, key) {
                            if(value.status.description === 'COMPLETED_THIS_CYCLE') {
                                    var task = {
                                            deptNbr: value.department.number,
                                            bayName: value.aisle + '-' + value.bay,
                                        completedBy: value.completedSystemUserId,
                                            completedOn:  new Date(value.completedTimeStamp)
                                    };
                                    this.push(task);
                            }
                        }, vm.completedItems);
            }
        }

    });


    }

  }
 })();

activatePackdown is a private function, so I can't test it directly, but I can test the value(s) of public properties once the controller is created. However, since that function calls a service method (that returns a promise), I don't know how to test inside the .then statement. I have the following for my unit test:

describe('activatePackdown', function(){

        it('should call GetAllTasks and then set completed/total counts', function(done) {
            var spy = sinon.spy(this.packdownService, 'GetAllTasks');

            var ctlr = $controller('PackdownController');
            expect(spy).toHaveBeenCalled();
        });
    });

the .toHaveBeenCalled passes just fine, but everything I've tried to do to test the state of properties in the .then. For instance, the value of vm.totalItemCount. I know I don't actually want the GetAllTasks method to run, so I created the spy using sinon.spy, but I'm at a point now where I don't know what to do next. I've tried several different suggestions I've found on varying blogs, but nothing seemed to work. Any and all help is greatly appreciated

Aucun commentaire:

Enregistrer un commentaire