Client side unit test for watcher until $onDestroy

This commit is contained in:
Carlos 2017-10-13 18:26:38 +02:00
parent d7668c278d
commit 496fd22b60
1 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,69 @@
import './watcher.js';
describe('Component vnWatcher', () => {
let $componentController;
let $scope;
let $element;
let $state;
let $transitions;
let $httpBackend;
let vnApp;
let $translate;
beforeEach(() => {
angular.mock.module('client');
});
beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$state_, _$transitions_, _$httpBackend_, _vnApp_, _$translate_) => {
$componentController = _$componentController_;
$scope = $rootScope.$new();
$element = angular.element('<div></div>');
$state = _$state_;
vnApp = _vnApp_;
$transitions = _$transitions_;
$httpBackend = _$httpBackend_;
$translate = _$translate_;
}));
describe('$onInit() ', () => {
it(`should call fetchData() if controllers get and url properties are defined`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
controller.get = () => {};
controller.url = 'test.com';
spyOn(controller, 'fetchData');
controller.$onInit();
expect(controller.fetchData).toHaveBeenCalledWith();
});
it(`should throw an error if $onInit is called without url defined`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
controller.get = () => {};
expect(function() {
controller.$onInit();
}).toThrow(new Error('Error: Parameter url ommitted'));
});
});
describe('$onChanges() ', () => {
it(`should call updateOriginalData() if controllers data is defined`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
controller.data = [];
spyOn(controller, 'updateOriginalData');
controller.$onChanges();
expect(controller.updateOriginalData).toHaveBeenCalledWith();
});
});
describe('$onDestroy() ', () => {
it(`should allways call deregisterCallback()`, () => {
let controller = $componentController('vnWatcher', {$scope, $element, $state, vnApp, $transitions, $httpBackend, $translate});
spyOn(controller, 'deregisterCallback');
controller.$onDestroy();
expect(controller.deregisterCallback).toHaveBeenCalledWith();
});
});
});