lundi 26 janvier 2015

bug in test adding items to array twice

Hi I have been trying to find a bug for three hours now without any luck. Right not my scope.images.total should eqaul 4 after running my tests however they are comming out as 6. On the same note scope.images.items should be [ 'sponser_image01.png', 'sponser_image02.png', 'sponser_image03.png', 'sponser_image04.png' ] but it is coming out as [ 'sponser_image01.png', 'sponser_image02.png', 'sponser_image03.png', 'sponser_image04.png', 'sponser_image03.png', 'sponser_image04.png' ]


I am sure it has something to do with the way I organized my code but I cannot seem to find a system that works.


Tests:



describe('pbImagePickerModalController', function () {
beforeEach(module('pb.campaigns.controllers'));
beforeEach(module('ui.router'));
beforeEach(module('ui.bootstrap'));

//var mockWindow = {};
//var mockDocument = {};

var mockState = {};
var mockModal = {};
//var mockGlobal = {};
var mockStateParams = {};
var mockAccountFileService = {};
var continuationToken = {};

var mockGlobal = {
setFormSubmitInProgress: function (boolean) {
this.formProgress = boolean;
},
formProgress: false,
activeOrganizationId: 0
};

var fileListStub = ['file_1', 'file_2', 'file_3'];

beforeEach(inject(function ($q) {
mockStateParams = {
accountId: 543,
length: 12
};

mockModalInstance = {
close: jasmine.createSpy('mockModalInstance.close'),
dismiss: jasmine.createSpy('mockModalInstance.dismiss'),
result: {
then: jasmine.createSpy('mockModalInstance.result.then')
}
};

mockAccountFileService = {
images: {
continuationToken: 6432,
total: 2,
items: ['sponser_image03.png', 'sponser_image04.png']
},

getFiles: function (activeOrganizationId, accountId, length, continuationToken) {
var defer = $q.defer();
defer.resolve(this.images);
return defer.promise;
}
};
}));


beforeEach(inject(function ($rootScope, _$controller_) {
scope = $rootScope.$new();
$controller = _$controller_;
controller = $controller('pbImagePickerModalController', {
$scope: scope,
$state: mockState,
$stateParams: mockStateParams,
global: mockGlobal,
accountFileService: mockAccountFileService,
$modal: mockModal,
$modalInstance: mockModalInstance,
//$window: mockWindow,
//$document: mockDocument,
accountId: mockStateParams.accountId
//accountId: function () {
// return mockStateParams.accountId;
//}
});

}));

describe("init() function", function () {

it('should call getImages()', function () {
spyOn(scope, 'getImages');
expect(scope.getImages).toHaveBeenCalledWith(50);
});

});

describe("getImages() function", function () {

it('should call getFiles with proper params', function () {
spyOn(mockAccountFileService, 'getFiles').and.callThrough();
scope.getImages(mockStateParams.length, continuationToken);
scope.$digest();
expect(mockAccountFileService.getFiles).toHaveBeenCalledWith(mockGlobal.activeOrganizationId, mockStateParams.accountId, mockStateParams.length, continuationToken);
});

it('should assign scope.images to promise result if scope.images does not exist', function () {
scope.getImages(mockStateParams.length, continuationToken);
scope.$digest();
expect(scope.images).toEqual(mockAccountFileService.images);
});

describe('with existing images', function () {

beforeEach(function () {
var existingLinks = {
continuationToken: 0002,
total: 2,
items: ['sponser_image01.png', 'sponser_image02.png']
};
scope.images = existingLinks;
});

it('should assign promise continuationToken to scope.images.continuationToken if scope.images exists', function () {

scope.getImages(mockStateParams.length, continuationToken);
scope.$digest();
expect(scope.images.continuationToken).toEqual(mockAccountFileService.images.continuationToken);
});

it('should assign promise data and existing images to scope if scope.images exists', function () {

scope.getImages(mockStateParams.length, continuationToken);
scope.$digest();
expect(scope.images.total).toEqual(4);
});

it('should push exisiting campaign links to link Array', function () {

scope.getImages(mockStateParams.length, continuationToken);
scope.$digest();

expect(scope.images.items).toEqual(['sponser_image01.png', 'sponser_image02.png', 'sponser_image03.png', 'sponser_image04.png']);
});

});

});

describe('filesUploaded', function () {

it('should add new files to scope.images.items', function () {
scope.images = mockAccountFileService.images;
console.log(scope.images.total);
scope.$broadcast('filesUploaded', fileListStub); //broadcast the event with stub data
scope.$digest(); //Triger digest cycle to invoke on handler
console.log(scope.images.total);
console.log(fileListStub.length);
expect(scope.images.total).toEqual(fileListStub.length + scope.images.total);
});

});

describe('filesUploadCompleted', function () {

it('should add new files to scope.images.items', function () {
scope.$broadcast('filesUploadCompleted');
spyOn(scope, $apply);
expect(scope.$apply).toHaveBeenCalled();
});

});

describe("select() function", function () {

it("should call modalInstance.close", function () {
scope.select(mockAccountFileService.images.items[1]);
expect(mockModalInstance.close).toHaveBeenCalledWith(mockAccountFileService.images.items[1]);
});

});

describe("cancel() function", function () {

it("changes formProgress from true to false", function () {
mockGlobal.setFormSubmitInProgress.formProgress = true;
scope.cancel();
expect(mockModalInstance.dismiss).toHaveBeenCalled();
expect(mockGlobal.formProgress).toEqual(false);
});

});

});


Controller:



angular.module('pb.campaigns.controllers')
.controller('pbImagePickerModalController', ['$window', '$scope', '$document', '$modal', '$modalInstance', 'global', 'accountId', 'accountFileService', function ($window, $scope, $document, $modal, $modalInstance, global, accountId, accountFileService) {

$scope.currentAccountId = accountId;

var init = function () {
$scope.getImages(50);
};

$scope.getImages = function (length, continuationToken) {
// show all existing images
accountFileService.getFiles(global.activeOrganizationId, accountId, length, continuationToken).then(function (data) {
if ($scope.images) {
$scope.images.continuationToken = data.continuationToken;
$scope.images.total += data.total;
angular.forEach(data.items, function (value, key) {
$scope.images.items.push(data.items[key]);
});
} else {
$scope.images = data;
}
});
};

init();

$scope.$on("filesUploaded", function (e, files) {
for (var i = 0; i < files.length; i++) {
$scope.images.items.unshift(files[i]);
}
$scope.images.total += files.length;
});

$scope.$on("filesUploadCompleted", function () {
if (!$scope.$$phase) $scope.$apply();
});

$scope.select = function (image) {
$modalInstance.close(image);
};

$scope.cancel = function () {
global.setFormSubmitInProgress(false);
$modalInstance.dismiss('cancel');
};

}]);

Aucun commentaire:

Enregistrer un commentaire