Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
test(mdMedia): add more tests and move .spec file to correct location
Browse files Browse the repository at this point in the history
In commit 6397040 `mdMedia` was moved out of the `sidenav` component and into
`core`. The .spec file remained in the original folder though.

This commit moves the 'media.spec.js` file to the correct location and
adds more tests to more thoroughly cover `mdMedia`'s functionality.
  • Loading branch information
gkalpak committed Dec 19, 2014
1 parent b044558 commit 0ec65a1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 25 deletions.
25 changes: 0 additions & 25 deletions src/components/sidenav/media.spec.js

This file was deleted.

81 changes: 81 additions & 0 deletions src/core/util/media.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
describe('$mdMedia', function() {
var matchMediaResult;
var queriesCache;
var resultsCache;


beforeEach(module('material.core'));

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};
});
}));

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 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('query')).toBe(true);
expect($window.matchMedia.callCount).toBe(1);

expect($mdMedia('query')).toBe(true);
expect($window.matchMedia.callCount).toBe(1);

matchMediaResult = false;
expect($mdMedia('query')).toBe(true);
expect($window.matchMedia.callCount).toBe(1);

angular.element($window).triggerHandler('resize');

expect($mdMedia('query')).toBe(false);
expect($window.matchMedia.callCount).toBe(2);
}));
});

0 comments on commit 0ec65a1

Please sign in to comment.