This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mdMedia): avoid unnecessary digest and make changes apply quicker
Previously, the `updateAll()` method relied on `$timeout` for scheduling a digest. This made the possible changes apply after the next rendering and could possibly lead to extra digests. Replaces the call to `$timeout` with a call to `$evalAsync()`, which avoids an extra digest if one is already in progress and also applies the changes before the next rendering. Closes #978.
- Loading branch information
1 parent
8736c7c
commit 98247bc
Showing
3 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,81 @@ | ||
describe('$mdMedia', function() { | ||
var matchMediaResult; | ||
var queriesCache; | ||
var resultsCache; | ||
|
||
|
||
beforeEach(module('material.core')); | ||
|
||
var matchMediaResult = false; | ||
beforeEach(inject(function($window) { | ||
beforeEach(inject(function($cacheFactory, $mdMedia, $window) { | ||
matchMediaResult = false; | ||
|
||
queriesCache = $cacheFactory.get('$mdMedia:queries'); | ||
resultsCache = $cacheFactory.get('$mdMedia:results'); | ||
|
||
spyOn($window, 'matchMedia').andCallFake(function() { | ||
return { matches: matchMediaResult }; | ||
return {matches: matchMediaResult}; | ||
}); | ||
})); | ||
|
||
it('should validate input', inject(function($window, $mdMedia) { | ||
afterEach(function() { | ||
queriesCache.removeAll(); | ||
resultsCache.removeAll(); | ||
}); | ||
|
||
|
||
it('should look up queries in `$mdConstant.MEDIA`', inject( | ||
function($mdConstant, $mdMedia, $window) { | ||
$mdConstant.MEDIA.somePreset = 'someQuery'; | ||
|
||
$mdMedia('somePreset'); | ||
expect($window.matchMedia).toHaveBeenCalledWith('someQuery'); | ||
|
||
delete($mdConstant.MEDIA.somePreset); | ||
} | ||
)); | ||
|
||
it('should look up validated queries in `queriesCache`', inject(function($mdMedia, $window) { | ||
queriesCache.put('originalQuery', 'validatedQuery'); | ||
|
||
$mdMedia('originalQuery'); | ||
expect($window.matchMedia).toHaveBeenCalledWith('validatedQuery'); | ||
})); | ||
|
||
it('should validate queries', inject(function($mdMedia, $window) { | ||
$mdMedia('something'); | ||
expect($window.matchMedia).toHaveBeenCalledWith('(something)'); | ||
})); | ||
|
||
it('should return result of matchMedia and recalculate on resize', inject(function($window, $mdMedia) { | ||
it('should cache validated queries in `queriesCache`', inject(function($mdMedia) { | ||
$mdMedia('query'); | ||
expect(queriesCache.get('query')).toBe('(query)'); | ||
})); | ||
|
||
it('should return cached results if available', inject(function($mdMedia) { | ||
resultsCache.put('(query)', 'result'); | ||
expect($mdMedia('(query)')).toBe('result'); | ||
})); | ||
|
||
it('should cache results in `resultsCache`', inject(function($mdMedia) { | ||
$mdMedia('(query)'); | ||
expect(resultsCache.get('(query)')).toBe(false); | ||
})); | ||
|
||
it('should recalculate on resize', inject(function($mdMedia, $window) { | ||
matchMediaResult = true; | ||
expect($mdMedia('foo')).toBe(true); | ||
expect($mdMedia('query')).toBe(true); | ||
expect($window.matchMedia.callCount).toBe(1); | ||
|
||
expect($mdMedia('query')).toBe(true); | ||
expect($window.matchMedia.callCount).toBe(1); | ||
|
||
matchMediaResult = false; | ||
expect($mdMedia('foo')).toBe(true); | ||
expect($mdMedia('query')).toBe(true); | ||
expect($window.matchMedia.callCount).toBe(1); | ||
|
||
angular.element($window).triggerHandler('resize'); | ||
expect($mdMedia('foo')).toBe(false); | ||
|
||
expect($mdMedia('query')).toBe(false); | ||
expect($window.matchMedia.callCount).toBe(2); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters