Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit fb3a7db

Browse files
matskomhevery
authored andcommittedAug 23, 2013
feat(ngMock): add support for creating dynamic style sheets within test code
1 parent 040aa11 commit fb3a7db

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed
 

‎angularFiles.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ angularFiles = {
7979
'src/ngTouch/swipe.js',
8080
'src/ngTouch/directive/ngClick.js',
8181
'src/ngTouch/directive/ngSwipe.js',
82-
'docs/components/angular-bootstrap/bootstrap.js'
82+
'docs/components/angular-bootstrap/bootstrap.js',
83+
'src/privateMocks.js'
8384
],
8485

8586
'angularScenario': [

‎src/privateMocks.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function createMockStyleSheet(doc, wind) {
2+
doc = doc ? doc[0] : document;
3+
wind = wind || window;
Has a conversation. Original line has a conversation.
4+
5+
var node = doc.createElement('style');
6+
var head = doc.getElementsByTagName('head')[0];
7+
head.appendChild(node);
8+
9+
var ss = doc.styleSheets[doc.styleSheets.length - 1];
10+
11+
return {
12+
addRule : function(selector, styles) {
13+
try {
14+
ss.insertRule(selector + '{ ' + styles + '}', 0);
15+
}
16+
catch(e) {
17+
try {
18+
ss.addRule(selector, styles);
19+
}
20+
catch(e) {}
21+
}
22+
},
23+
24+
destroy : function() {
25+
head.removeChild(node);
26+
}
27+
};
28+
};

‎test/ngAnimate/animateSpec.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1118,11 +1118,15 @@ describe("ngAnimate", function() {
11181118
}));
11191119

11201120
it("should properly execute CSS animations/transitions and use callbacks when using addClass / removeClass",
1121-
inject(function($animate, $rootScope, $sniffer, $rootElement, $timeout) {
1121+
inject(function($animate, $rootScope, $sniffer, $rootElement, $timeout, $window, $document) {
11221122

1123-
var transition = 'transition:11s linear all;';
1124-
var style = transition + ' ' + vendorPrefix + transition;
1125-
var parent = jqLite('<div><span style="' + style + '"></span></div>');
1123+
var ss = createMockStyleSheet($document, $window);
1124+
ss.addRule('.klass-add', 'transition:11s linear all');
1125+
ss.addRule('.klass-add', vendorPrefix + 'transition:11s linear all');
1126+
ss.addRule('.klass-remove', 'transition:11s linear all');
1127+
ss.addRule('.klass-remove', vendorPrefix + 'transition:11s linear all');
1128+
1129+
var parent = jqLite('<div><span></span></div>');
11261130
$rootElement.append(parent);
11271131
body.append($rootElement);
11281132
var element = jqLite(parent.find('span'));

‎test/privateMocksSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
describe('private mocks', function() {
2+
describe('createMockStyleSheet', function() {
3+
4+
it('should allow custom styles to be created and removed when the stylesheet is destroyed',
5+
inject(function($compile, $document, $window, $rootElement, $rootScope) {
6+
7+
var doc = $document[0];
8+
var count = doc.styleSheets.length;
9+
var stylesheet = createMockStyleSheet($document, $window);
10+
expect(doc.styleSheets.length).toBe(count + 1);
11+
12+
jqLite(doc.body).append($rootElement);
13+
14+
var elm = $compile('<div class="padded">...</div>')($rootScope);
15+
$rootElement.append(elm);
16+
17+
expect(getStyle(elm, 'paddingTop')).toBe('0px');
18+
19+
stylesheet.addRule('.padded', 'padding-top:2px');
20+
21+
expect(getStyle(elm, 'paddingTop')).toBe('2px');
22+
23+
stylesheet.destroy();
24+
25+
expect(getStyle(elm, 'paddingTop')).toBe('0px');
26+
27+
function getStyle(element, key) {
28+
var node = element[0];
29+
return node.currentStyle ?
30+
node.currentStyle[key] :
31+
$window.getComputedStyle(node)[key];
32+
};
33+
}));
34+
35+
});
36+
});

0 commit comments

Comments
 (0)
This repository has been archived.