Skip to content

Commit

Permalink
tests: coverage improvement
Browse files Browse the repository at this point in the history
Signed-off-by: Harris Tzovanakis <me@drjova.com>
  • Loading branch information
drjova committed Apr 19, 2016
1 parent 4c1d01a commit e109e74
Show file tree
Hide file tree
Showing 8 changed files with 727 additions and 51 deletions.
1 change: 0 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,3 @@ <h1>Invenio Records Demo</h1>
</div>
</body>
</html>

118 changes: 89 additions & 29 deletions src/invenio-records-js/invenioRecords.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@
};

// The form model
vm.invenioRecordsModel = {};
// Set loading
vm.invenioRecordsLoading = true;
vm.invenioRecordsModel = null;
// Set endpoints
vm.invenioRecordsEndpoints = {};
// Set errors
vm.invenioRecordsError = {};
// Set notify
vm.invenioRecordsNotify = false;
vm.invenioRecordsEndpoints = null;

// Record Loading - If the invenioRecords has the state loading
vm.invenioRecordsLoading = true;

// Record Error - if the invenioRecords has any error
vm.invenioRecordsError = null;

// Record Warn - if the invenioRecords has any warning
vm.invenioRecordsWarning = null;

// Set action handler if everything is ok
var Handler = new InvenioRecordsActionsHandler();

Expand Down Expand Up @@ -84,13 +88,13 @@
/**
* Initialize the controller
* @memberof invenioRecordsController
* @function invenioRecordsInitialize
* @function invenioRecordsInit
* @param {Object} evt - The event object.
* @param {Object} args - The invenio records arguments.
* @param {Object} endpoints - The invenio endpoints for actions.
* @param {Object} record - The record object.
*/
function invenioRecordsInitialize(evt, args, endpoints, record) {
function invenioRecordsInit(evt, args, endpoints, record) {
// Assign the model
vm.invenioRecordsModel = angular.copy(record);
// Assign the args
Expand All @@ -112,14 +116,15 @@
invenioRecordsAPI.get(vm.invenioRecordsEndpoints.form)
.then(invenioRecordsSetForm)
]).then(function() {
vm.invenioRecordsLoading = false;
// Pass the endpoints to the factory
Handler.setEndpoint(vm.invenioRecordsEndpoints);
// Remove loading state
$scope.$broadcast('invenio.records.loading.stop');
});
}

/**
* Initialize the controller
* Records actions
* @memberof invenioRecordsController
* @function invenioRecordsActions
* @param {Object} evt - The event object.
Expand All @@ -129,11 +134,7 @@
*/
function invenioRecordsActions(evt, type, successCallback, errorCallback) {
// Set loading to true
vm.invenioRecordsLoading = true;
// Reset any errors
vm.invenioRecordsError = {};
// Reset any notifications
vm.invenioRecordsNotify = false;
$scope.$broadcast('invenio.records.loading.start');
// If the type function exists run it
if (typeof Handler[type] === 'function') {
// Make the request iff the type exists
Expand All @@ -143,7 +144,8 @@
successCallback || angular.noop,
errorCallback || angular.noop
).finally(function() {
vm.invenioRecordsLoading = false;
// Set loading to stop
$scope.$broadcast('invenio.records.loading.stop');
});
}
}
Expand All @@ -162,7 +164,7 @@
* @param {Object} response - The action request response.
*/
function _actionSuccessful(response) {
vm.invenioRecordsNotify = response.data || 'Successfully deleted!';
$scope.$broadcast('invenio.records.warn', response);
}

/**
Expand All @@ -172,7 +174,7 @@
* @param {Object} response - The action request response.
*/
function _actionErrored(response) {
vm.invenioRecordsError = response;
$scope.$broadcast('invenio.records.error', response);
}

// Request submission
Expand All @@ -184,21 +186,79 @@
);
}


/**
* Change the state to loading
* @memberof invenioRecordsController
* @function invenioRecordsLoadingStart
* @param {Object} evt - The event object.
*/
function invenioRecordsLoadingStart(evt) {
// Set the state to loading
vm.invenioRecordsLoading = true;
}

/**
* Change the state to normal
* @memberof invenioRecordsController
* @function invenioRecordsLoadingStop
* @param {Object} evt - The event object.
*/
function invenioRecordsLoadingStop(evt) {
// Set the state to normal
vm.invenioRecordsLoading = false;
}

/**
* Show error messages
* @memberof invenioRecordsController
* @function invenioRecordsError
* @param {Object} evt - The event object.
* @param {Object} error - The object with the errors.
*/
function invenioRecordsError(evt, error) {
// Reset the error
vm.invenioRecordsError = null;
// Attach the error to the scope
vm.invenioRecordsError = error;
}

/**
* Show warning messages
* @memberof invenioRecordsController
* @function invenioRecordsWarn
* @param {Object} evt - The event object.
* @param {Object} warning - The object with the warnings.
*/
function invenioRecordsWarn(evt, warning) {
// Reset the error
vm.invenioRecordsWarning = null;
// Attach the warning to the scope
vm.invenioRecordsWarning = warning;
}

// Attach fuctions to the scope
vm.actionHandler = invenioRecordsHandler;

////////////

// Listeners

// When invenio.records initialization requested
$scope.$on(
'invenio.records.initialization', invenioRecordsInitialize
);
// When invenio.records action requested
$scope.$on(
'invenio.records.action', invenioRecordsActions
);
$scope.$on('invenio.records.action', invenioRecordsActions);

// When the module initialized
$scope.$on('invenio.records.init', invenioRecordsInit);

// When there is an error
$scope.$on('invenio.records.error', invenioRecordsError);
// When there is a warning
$scope.$on('invenio.records.warn', invenioRecordsWarn);

// When loading requested to start
$scope.$on('invenio.records.loading.start', invenioRecordsLoadingStart);
// When loading requested to stop
$scope.$on('invenio.records.loading.stop', invenioRecordsLoadingStop);
}

// Inject depedencies
Expand Down Expand Up @@ -272,7 +332,7 @@
* @ngdoc factory
* @namei invenioRecordsActionsHandler
* @namespace invenioRecordsActionsHandler
* @param {service} $http - Angular http requests service.
* @param {service} invenioRecordsAPI - Invenio Records API.
* @param {service} $q - Angular promise service.
* @description
* Call the records API
Expand Down Expand Up @@ -426,7 +486,7 @@

// Spread the love of initialization
scope.$broadcast(
'invenio.records.initialization', args, endpoints, record
'invenio.records.init', args, endpoints, record
);
}

Expand Down
105 changes: 105 additions & 0 deletions test/unit/invenio-records-js/controllers/controllersSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* This file is part of Invenio.
* Copyright (C) 2016 CERN.
*
* Invenio is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* Invenio is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Invenio; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* In applying this license, CERN does not
* waive the privileges and immunities granted to it by virtue of its status
* as an Intergovernmental Organization or submit itself to any jurisdiction.
*/

'use strict';

describe('Unit: testing controllers', function() {

var $controller;
var $rootScope;
var ctrl;
var scope;

// Inject the angular module
beforeEach(angular.mock.module('invenioRecords'));

beforeEach(inject(function(_$controller_, _$rootScope_) {
// Controller
$controller = _$controller_;
// The Scope
$rootScope = _$rootScope_;
// Set the scope
scope = $rootScope;
// The controller
ctrl = $controller('invenioRecordsController', {
$scope: scope,
});
}));

it('should have the default parameters', function() {
// Expect loading to be ``true``
expect(ctrl.invenioRecordsLoading).to.be.equal(true);

// Expect error to be ``null``
expect(ctrl.invenioRecordsError).to.be.equal(null);
// Expect notify to be ``null``
expect(ctrl.invenioRecordsWarning).to.be.equal(null);

// Expect model to be ``{}``
expect(ctrl.invenioRecordsModel).to.be.equal(null);
// Expect endpoints to be ``{}``
expect(ctrl.invenioRecordsEndpoints).to.be.equal(null);

// Expect the request args
var args = {
url: '/',
method: 'GET'
};
expect(ctrl.invenioRecordsArgs).to.deep.equal(args);
});

it('should trigger the events', function() {

// Trigger error event
scope.$broadcast('invenio.records.error', {
message: 'Bruce Wayne is not Superman Clark Kent is, dah!'
});

// Expect error to be the above message
expect(ctrl.invenioRecordsError.message).to.be.equal(
'Bruce Wayne is not Superman Clark Kent is, dah!'
);

// Trigger warning event
scope.$broadcast('invenio.records.warn', {
message: 'Tell me, do you bleed?'
});

// Expect warning to be the above message
expect(ctrl.invenioRecordsWarning.message).to.be.equal(
'Tell me, do you bleed?'
);

// Trigger loading start event
scope.$broadcast('invenio.records.loading.start');

// Expect loading to be ``false``
expect(ctrl.invenioRecordsLoading).to.be.equal(true);

// Trigger loading start event
scope.$broadcast('invenio.records.loading.stop');

// Expect loading to be ``false``
expect(ctrl.invenioRecordsLoading).to.be.equal(false);
});
});

0 comments on commit e109e74

Please sign in to comment.