Skip to content

Commit

Permalink
Added coverage / build status and added a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
rpocklin committed Mar 27, 2015
1 parent 23a0351 commit c2e9da9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 48 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Angular UI View Spinner

[![Build Status](https://secure.travis-ci.org/rpocklin/angular-ui-view-spinner.svg)](http:/travis-ci.org/rpocklin/angular-ui-view-spinner)
[![Coverage Status](https://coveralls.io/repos/rpocklin/angular-ui-view-spinner/badge.svg)](https://coveralls.io/r/rpocklin/angular-ui-view-spinner)

A declarative, powerful drop-in addition to UI Router, enabling spinners to be shown when executing `resolves` before
the route change is complete.

Expand Down
4 changes: 2 additions & 2 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'example', ['angularSpinner', 'ui.router', 'angular-ui-view-spinner']
);

app.config(
app.config(['$stateProvider',
function($stateProvider) {
$stateProvider.state(
'example', {
Expand Down Expand Up @@ -53,6 +53,6 @@
}
}
);
}
}]
);
})();
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link rel="stylesheet" href="../src/angular-ui-view-spinner.css" />
</head>

<body ng-app="example">
<body ng-app="example" ng-strict-di>

<div ui-view></div>

Expand Down
32 changes: 10 additions & 22 deletions src/angular-ui-view-spinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

angular.module('angular-ui-view-spinner', []);

// http://www.technofattie.com/2014/07/27/easy-loading-indicator-when-switching-views-in-angular.html
// http://www.technofattie.com/2014/07/27/easy-loading-indicator-when-switching-views-in-angular.html
// tokenises the string from the last index of the separator back

var strLeftBack = function(str, separator) {
Expand Down Expand Up @@ -55,7 +55,6 @@
},

compile: function(elem, attrs) {

return {
post: function(scope, elem, attrs) {

Expand Down Expand Up @@ -87,30 +86,22 @@

scope.showSpinner = function() {
return scope.isSpinnerEnabled() || scope.isNextRouteLoading();
// TODO: remove
//return true;
};

// TODO: make this configurable
var SHOW_SPINNER_DELAY_ON_FIRST_PAGE_LOAD = 250;
/* msec */

// keeps track of stateChange events in directive to unbind on destroy
var boundEvents = [];

var clearSpinnerSettings = function(isGlobalLoading) {

scope._unbindClearSpinnerSettings = $timeout(
function() {
console.log('cleared');
scope.spinnerEnabled = false;
scope.globalRoute = false;

scope.nextRouteLoaded = true;
scope.nextRouteLoading = false;
}, 0, true
);

};

var setSpinnerLoadingSettings = function(scope, to, parentTo, isGlobalLoading) {
Expand All @@ -121,8 +112,8 @@

var updateSpinnerState = function(scope, fromState, toState, routeLoadingState) {

var from = fromState.name;
var to = toState.name;
var from = fromState ? fromState.name : '';
var to = toState ? toState.name : '';

var parentFrom = strLeftBack(from, '.');
var parentTo = strLeftBack(to, '.');
Expand Down Expand Up @@ -153,11 +144,9 @@
if (isGlobalLoading && !rootState) {

if (routeLoadingState) {
//console.log('global route');
setSpinnerLoadingSettings(scope, to, parentTo, isGlobalLoading);
}
else {
console.log('global clear');
clearSpinnerSettings(isGlobalLoading);
}

Expand All @@ -180,7 +169,6 @@
boundEvents.push(
$rootScope.$on(
'$stateChangeError', function(event, toState, toParams, fromState, fromParams) {
console.log('cancelled');
updateSpinnerState(scope, fromState, toState, false);
}
)
Expand All @@ -190,8 +178,8 @@
$rootScope.$on(
'$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {

var from = fromState.name;
var to = toState.name;
var from = fromState ? fromState.name : '';
var to = toState ? toState.name : '';

var parentFrom = strLeftBack(from, '.');
var parentTo = strLeftBack(to, '.');
Expand All @@ -208,8 +196,8 @@
'$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {

// if fromState = 'example' || toState = sibling
var from = fromState.name;
var to = toState.name;
var from = fromState ? fromState.name : '';
var to = toState ? toState.name : '';

var parentFrom = strLeftBack(from, '.');
var parentTo = strLeftBack(to, '.');
Expand All @@ -222,12 +210,11 @@
);

var cancelCurrentSpinner = function() {

//prevents multiple route changes causing problems
angular.forEach(
[scope._unbindClearSpinnerSettings], function(timeoutEvent) {
console.log(timeoutEvent);
if (timeoutEvent) {
console.log('cancelling');
$timeout.cancel(timeoutEvent);
}
}
Expand All @@ -238,7 +225,7 @@

angular.forEach(
boundEvents, function(unbindEventListener) {
unbindEventListener(); // unbinds listener events
unbindEventListener();
}
);

Expand Down Expand Up @@ -269,6 +256,7 @@
);
})();

// future tests
// example.two.b -> example.one (should show rootstate=example spinner only)
// example.one -> example.two.b (should show rootstate=example spinner only)
// example.two.b -> example.one.a (should show rootstate=example.two spinner only)
Expand Down
53 changes: 30 additions & 23 deletions src/angular-ui-view-spinner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
}
};

var $ng_view;
var directive_scope;

var create_view = function(html, scope) {

inject(
function($compile) {
$ng_view = $compile(html)(scope);
}
);

scope.$digest();
directive_scope = $ng_view.isolateScope();

return $ng_view;
};

beforeEach(
function() {

Expand Down Expand Up @@ -58,32 +75,15 @@
describe(
'Directive : UI Router : View Spinner', function() {

var root_scope, scope, directive_scope, view, element, state, spy, q;
var createView, get_current_state, spinnerIsHidden, spinnerIsVisible, viewIsVisible;
var $ngView;
var root_scope, scope, view, state, spy, q;
var get_current_state, spinnerIsHidden, spinnerIsVisible, viewIsVisible;

beforeEach(
inject(
function($rootScope, $state, $templateCache, $q) {

q = $q;
defer = $q.defer();

createView = function(html, scope) {
element = angular.element(view);

inject(
function($compile) {
$ngView = $compile(html)(scope);
}
);

scope.$digest();
directive_scope = $ngView.isolateScope();

return $ngView;
};

scope = $rootScope.$new();
state = $state;

Expand All @@ -94,14 +94,14 @@
};

view = '<ui-loading-view root-state="menu"></ui-loading-view>';
$ngView = createView(view, scope);
$ng_view = create_view(view, scope);

spinnerIsHidden = function() {
return $ngView.find('.view-loading-spinner').hasClass('ng-hide');
return $ng_view.find('.view-loading-spinner').hasClass('ng-hide');
};

viewIsVisible = function() {
return !$ngView.find('div[ui-view]').find('div[ui-view]').hasClass('ng-hide');
return !$ng_view.find('div[ui-view]').find('div[ui-view]').hasClass('ng-hide');
};

spinnerIsVisible = function() {
Expand All @@ -118,10 +118,17 @@
)
);

xit('should define the view spinner directive with isolated scope', function() {
it('should define the view spinner directive with isolated scope', function() {
expect(directive_scope).toBeDefined();
});

it('should throw an error when specifying an incorrect size value', function() {
view = '<ui-loading-view size="smallish"></ui-loading-view>';
expect(function() {
create_view(view, scope);
}).toThrow();
});

it('should show the spinner when a new child route is being loaded', function() {

expect(spinnerIsHidden()).toBeTruthy();
Expand Down

0 comments on commit c2e9da9

Please sign in to comment.