This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ngMock): add support for creating dynamic style sheets within te…
…st code
- Loading branch information
Showing
4 changed files
with
74 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function createMockStyleSheet(doc, wind) { | ||
doc = doc ? doc[0] : document; | ||
wind = wind || window; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
var node = doc.createElement('style'); | ||
var head = doc.getElementsByTagName('head')[0]; | ||
head.appendChild(node); | ||
|
||
var ss = doc.styleSheets[doc.styleSheets.length - 1]; | ||
|
||
return { | ||
addRule : function(selector, styles) { | ||
try { | ||
ss.insertRule(selector + '{ ' + styles + '}', 0); | ||
} | ||
catch(e) { | ||
try { | ||
ss.addRule(selector, styles); | ||
} | ||
catch(e) {} | ||
} | ||
}, | ||
|
||
destroy : function() { | ||
head.removeChild(node); | ||
} | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
describe('private mocks', function() { | ||
describe('createMockStyleSheet', function() { | ||
|
||
it('should allow custom styles to be created and removed when the stylesheet is destroyed', | ||
inject(function($compile, $document, $window, $rootElement, $rootScope) { | ||
|
||
var doc = $document[0]; | ||
var count = doc.styleSheets.length; | ||
var stylesheet = createMockStyleSheet($document, $window); | ||
expect(doc.styleSheets.length).toBe(count + 1); | ||
|
||
jqLite(doc.body).append($rootElement); | ||
|
||
var elm = $compile('<div class="padded">...</div>')($rootScope); | ||
$rootElement.append(elm); | ||
|
||
expect(getStyle(elm, 'paddingTop')).toBe('0px'); | ||
|
||
stylesheet.addRule('.padded', 'padding-top:2px'); | ||
|
||
expect(getStyle(elm, 'paddingTop')).toBe('2px'); | ||
|
||
stylesheet.destroy(); | ||
|
||
expect(getStyle(elm, 'paddingTop')).toBe('0px'); | ||
|
||
function getStyle(element, key) { | ||
var node = element[0]; | ||
return node.currentStyle ? | ||
node.currentStyle[key] : | ||
$window.getComputedStyle(node)[key]; | ||
}; | ||
})); | ||
|
||
}); | ||
}); |
It seems as though wind is not used after it's declared. Is it needed?