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
mdMedia improvements #978
Closed
Closed
mdMedia improvements #978
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gkalpak - think this is dangerous. Perhaps:
is safer and allows subsequent calls to
cache.put()
to work properly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subsequent calls to
put()
will not work correctly anyway, because the originalput()
will fail after having calleddestroy()
. (put()
is not supposed to be called afterdestroy()
.)So, having
keys = null
helps GC (instead of having to keep a reference to an empty object around). But, it's impact should be minimal (even more so consideringdestroy()
is rarely used currently).