Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protractor $timeout workaround #337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build/loading-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ angular.module('cfp.loadingBar', [])
this.spinnerTemplate = '<div id="loading-bar-spinner"><div class="spinner-icon"></div></div>';
this.loadingBarTemplate = '<div id="loading-bar"><div class="bar"><div class="peg"></div></div></div>';

this.$get = ['$injector', '$document', '$timeout', '$rootScope', function ($injector, $document, $timeout, $rootScope) {
this.$get = ['$injector', '$document', '$timeout', '$interval', '$rootScope', function ($injector, $document, $timeout, $interval, $rootScope) {
var $animate;
var $parentSelector = this.parentSelector,
loadingBarContainer = angular.element(this.loadingBarTemplate),
Expand Down Expand Up @@ -251,10 +251,10 @@ angular.module('cfp.loadingBar', [])
// progress but make sure to cancel the previous timeouts so we don't
// have multiple incs running at the same time.
if (autoIncrement) {
$timeout.cancel(incTimeout);
incTimeout = $timeout(function() {
$interval.cancel(incTimeout);
incTimeout = $interval(function() {
_inc();
}, 250);
}, 250, 1);
}
}

Expand Down
2 changes: 1 addition & 1 deletion build/loading-bar.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/loading-bar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/loading-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ angular.module('cfp.loadingBar', [])
this.spinnerTemplate = '<div id="loading-bar-spinner"><div class="spinner-icon"></div></div>';
this.loadingBarTemplate = '<div id="loading-bar"><div class="bar"><div class="peg"></div></div></div>';

this.$get = ['$injector', '$document', '$timeout', '$rootScope', function ($injector, $document, $timeout, $rootScope) {
this.$get = ['$injector', '$document', '$timeout', '$interval', '$rootScope', function ($injector, $document, $timeout, $interval, $rootScope) {
var $animate;
var $parentSelector = this.parentSelector,
loadingBarContainer = angular.element(this.loadingBarTemplate),
Expand Down Expand Up @@ -245,10 +245,10 @@ angular.module('cfp.loadingBar', [])
// progress but make sure to cancel the previous timeouts so we don't
// have multiple incs running at the same time.
if (autoIncrement) {
$timeout.cancel(incTimeout);
incTimeout = $timeout(function() {
$interval.cancel(incTimeout);
incTimeout = $interval(function() {
_inc();
}, 250);
}, 250, 1);
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/loading-bar-interceptor-config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ describe 'loadingBarInterceptor Service - config options', ->
module 'chieffancypants.loadingBar', (cfpLoadingBarProvider) ->
cfpLoadingBarProvider.autoIncrement = true
return
inject ($timeout, cfpLoadingBar) ->
inject ($timeout, $interval, cfpLoadingBar) ->
cfpLoadingBar.start()
$timeout.flush()
$interval.flush(250)
cfpLoadingBar.set(.5)
$timeout.flush()
$interval.flush(250)
expect(cfpLoadingBar.status()).toBeGreaterThan .5
cfpLoadingBar.complete()
$timeout.flush()
Expand Down
36 changes: 19 additions & 17 deletions test/loading-bar-interceptor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ flush = null

describe 'loadingBarInterceptor Service', ->

$http = $httpBackend = $document = $timeout = result = loadingBar = $animate = null
$http = $httpBackend = $document = $timeout = $interval = result = loadingBar = $animate = null
response = {message:'OK'}
endpoint = '/service'

Expand All @@ -21,11 +21,12 @@ describe 'loadingBarInterceptor Service', ->
return

result = null
inject (_$http_, _$httpBackend_, _$document_, _$timeout_, _$animate_) ->
inject (_$http_, _$httpBackend_, _$document_, _$timeout_, _$interval_, _$animate_) ->
$http = _$http_
$httpBackend = _$httpBackend_
$document = _$document_
$timeout = _$timeout_
$interval = _$interval_
$animate = _$animate_

# Angular 1.4 removed triggerCalbacks(), so try them both:
Expand Down Expand Up @@ -250,7 +251,7 @@ describe 'loadingBarInterceptor Service', ->

it 'should get and set status', inject (cfpLoadingBar) ->
cfpLoadingBar.start()
$timeout.flush()
$interval.flush(250)

cfpLoadingBar.set(0.4)
expect(cfpLoadingBar.status()).toBe 0.4
Expand All @@ -264,21 +265,21 @@ describe 'loadingBarInterceptor Service', ->

it 'should increment things randomly', inject (cfpLoadingBar) ->
cfpLoadingBar.start()
$timeout.flush()
$interval.flush(250)

# increments between 3 - 6%
cfpLoadingBar.set(0.1)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
$interval.flush(250)
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(3, 6)

cfpLoadingBar.set(0.2)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
$interval.flush(250)
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(3, 6)
Expand All @@ -287,15 +288,15 @@ describe 'loadingBarInterceptor Service', ->
cfpLoadingBar.set(0.25)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
$interval.flush(250)
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(0, 3)

cfpLoadingBar.set(0.5)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
$interval.flush(250)
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(0, 3)
Expand All @@ -304,15 +305,15 @@ describe 'loadingBarInterceptor Service', ->
cfpLoadingBar.set(0.65)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
$interval.flush(250)
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(0, 2)

cfpLoadingBar.set(0.75)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
$interval.flush(250)
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(0, 2)
Expand All @@ -321,15 +322,15 @@ describe 'loadingBarInterceptor Service', ->
cfpLoadingBar.set(0.9)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
$interval.flush(250)
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBe 0.5

cfpLoadingBar.set(0.97)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
$interval.flush(250)
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBe 0.5
Expand All @@ -338,7 +339,7 @@ describe 'loadingBarInterceptor Service', ->
cfpLoadingBar.set(0.99)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
$interval.flush(250)
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBe width

Expand Down Expand Up @@ -445,21 +446,22 @@ describe 'loadingBarInterceptor Service', ->


describe 'LoadingBar only', ->
cfpLoadingBar = $document = $timeout = $animate = null
cfpLoadingBar = $document = $timeout = $interval = $animate = null

beforeEach ->
module 'cfp.loadingBar', 'ngAnimateMock'

inject (_$http_, _$httpBackend_, _$document_, _$timeout_, _$animate_, _cfpLoadingBar_) ->
inject (_$http_, _$httpBackend_, _$document_, _$timeout_, _$interval_, _$animate_, _cfpLoadingBar_) ->
$timeout = _$timeout_
$interval = _$interval_
$document = _$document_
$animate = _$animate_
cfpLoadingBar = _cfpLoadingBar_

it 'should be capable of being used alone', ->
# just a simple quick test to make sure:
cfpLoadingBar.start()
$timeout.flush()
$interval.flush(250)

# test setting progress
cfpLoadingBar.set(0.4)
Expand All @@ -478,7 +480,7 @@ describe 'LoadingBar only', ->

it 'should start after multiple calls to complete()', ->
cfpLoadingBar.start()
$timeout.flush()
$interval.flush(250)
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe true

cfpLoadingBar.complete()
Expand Down