|
| 1 | +/** |
| 2 | + * |
| 3 | + * Angular-Material-Mocks |
| 4 | + * |
| 5 | + * Developers interested in running their own custom unit tests WITH angular-material.js loaded... |
| 6 | + * must also include this *mocks* file. Similar to `angular-mocks.js`, `angular-material-mocks.js` |
| 7 | + * will override and disable specific Angular Material performance settings: |
| 8 | + * |
| 9 | + * - Disabled Theme CSS rule generations |
| 10 | + * - Forces $mdAria.expectWithText() to be synchronous |
| 11 | + * - Mocks $$rAF.throttle() |
| 12 | + * - Captures flush exceptions from $$rAF |
| 13 | + * |
| 14 | + */ |
| 15 | +(function(window, angular, undefined) { |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +/** |
| 20 | + * @ngdoc module |
| 21 | + * @name ngMaterial-mock |
| 22 | + * @packageName angular-material-mocks |
| 23 | + * |
| 24 | + * @description |
| 25 | + * |
| 26 | + * The `ngMaterial-mock` module provides support |
| 27 | + * |
| 28 | + */ |
| 29 | +angular.module('ngMaterial-mock', [ |
| 30 | + 'ngMock', |
| 31 | + 'ngAnimateMock', |
| 32 | + 'material.core' |
| 33 | + ]) |
| 34 | + .config(['$provide', function($provide) { |
| 35 | + |
| 36 | + $provide.factory('$material', ['$animate', '$timeout', function($animate, $timeout) { |
| 37 | + return { |
| 38 | + flushOutstandingAnimations: function() { |
| 39 | + // this code is placed in a try-catch statement |
| 40 | + // since 1.3 and 1.4 handle their animations differently |
| 41 | + // and there may be situations where follow-up animations |
| 42 | + // are run in one version and not the other |
| 43 | + try { $animate.flush(); } catch(e) {} |
| 44 | + }, |
| 45 | + flushInterimElement: function() { |
| 46 | + this.flushOutstandingAnimations(); |
| 47 | + $timeout.flush(); |
| 48 | + this.flushOutstandingAnimations(); |
| 49 | + $timeout.flush(); |
| 50 | + this.flushOutstandingAnimations(); |
| 51 | + $timeout.flush(); |
| 52 | + } |
| 53 | + }; |
| 54 | + }]); |
| 55 | + |
| 56 | + /** |
| 57 | + * Angular Material dynamically generates Style tags |
| 58 | + * based on themes and palletes; for each ng-app. |
| 59 | + * |
| 60 | + * For testing, we want to disable generation and |
| 61 | + * <style> DOM injections. So we clear the huge THEME |
| 62 | + * styles while testing... |
| 63 | + */ |
| 64 | + $provide.constant('$MD_THEME_CSS', '/**/'); |
| 65 | + |
| 66 | + /** |
| 67 | + * Intercept to make .expectWithText() to be synchronous |
| 68 | + */ |
| 69 | + $provide.decorator('$mdAria', function($delegate){ |
| 70 | + |
| 71 | + $delegate.expectWithText = function(element, attrName){ |
| 72 | + $delegate.expect(element, attrName, element.text().trim()); |
| 73 | + }; |
| 74 | + |
| 75 | + return $delegate; |
| 76 | + }); |
| 77 | + |
| 78 | + /** |
| 79 | + * Add throttle() and wrap .flush() to catch `no callbacks present` |
| 80 | + * errors |
| 81 | + */ |
| 82 | + $provide.decorator('$$rAF', function throttleInjector($delegate){ |
| 83 | + |
| 84 | + $delegate.throttle = function(cb) { |
| 85 | + return function() { |
| 86 | + cb.apply(this, arguments); |
| 87 | + }; |
| 88 | + }; |
| 89 | + |
| 90 | + var ngFlush = $delegate.flush; |
| 91 | + $delegate.flush = function() { |
| 92 | + try { ngFlush(); } |
| 93 | + catch(e) { ; } |
| 94 | + }; |
| 95 | + |
| 96 | + return $delegate; |
| 97 | + }); |
| 98 | + |
| 99 | + /** |
| 100 | + * Capture $timeout.flush() errors: "No deferred tasks to be flushed" |
| 101 | + * errors |
| 102 | + */ |
| 103 | + $provide.decorator('$timeout', function throttleInjector($delegate){ |
| 104 | + |
| 105 | + var ngFlush = $delegate.flush; |
| 106 | + $delegate.flush = function() { |
| 107 | + var args = Array.prototype.slice.call(arguments); |
| 108 | + try { ngFlush.apply($delegate, args); } |
| 109 | + catch(e) { ; } |
| 110 | + }; |
| 111 | + |
| 112 | + return $delegate; |
| 113 | + }); |
| 114 | + |
| 115 | + }]) |
| 116 | + |
| 117 | + /** |
| 118 | + * Stylesheet Mocks used by `animateCss.spec.js` |
| 119 | + */ |
| 120 | + window.createMockStyleSheet = function createMockStyleSheet(doc, wind) { |
| 121 | + doc = doc ? doc[0] : window.document; |
| 122 | + wind = wind || window; |
| 123 | + |
| 124 | + var node = doc.createElement('style'); |
| 125 | + var head = doc.getElementsByTagName('head')[0]; |
| 126 | + head.appendChild(node); |
| 127 | + |
| 128 | + var ss = doc.styleSheets[doc.styleSheets.length - 1]; |
| 129 | + |
| 130 | + return { |
| 131 | + addRule: function(selector, styles) { |
| 132 | + styles = addVendorPrefix(styles); |
| 133 | + |
| 134 | + try { |
| 135 | + ss.insertRule(selector + '{ ' + styles + '}', 0); |
| 136 | + } |
| 137 | + catch (e) { |
| 138 | + try { |
| 139 | + ss.addRule(selector, styles); |
| 140 | + } |
| 141 | + catch (e2) {} |
| 142 | + } |
| 143 | + }, |
| 144 | + |
| 145 | + destroy: function() { |
| 146 | + head.removeChild(node); |
| 147 | + } |
| 148 | + }; |
| 149 | + |
| 150 | + /** |
| 151 | + * Decompose styles, attached specific vendor prefixes |
| 152 | + * and recompose... |
| 153 | + * e.g. |
| 154 | + * 'transition:0.5s linear all; font-size:100px;' |
| 155 | + * becomes |
| 156 | + * '-webkit-transition:0.5s linear all; transition:0.5s linear all; font-size:100px;' |
| 157 | + */ |
| 158 | + function addVendorPrefix(styles) { |
| 159 | + var cache = { }; |
| 160 | + |
| 161 | + // Decompose into cache registry |
| 162 | + styles |
| 163 | + .match(/([\-A-Za-z]*)\w\:\w*([A-Za-z0-9\.\-\s]*)/gi) |
| 164 | + .forEach(function(style){ |
| 165 | + var pair = style.split(":"); |
| 166 | + var key = pair[0]; |
| 167 | + |
| 168 | + switch(key) { |
| 169 | + case 'transition': |
| 170 | + case 'transform': |
| 171 | + case 'animation': |
| 172 | + case 'transition-duration': |
| 173 | + case 'animation-duration': |
| 174 | + cache[key] = cache['-webkit-' + key] = pair[1]; |
| 175 | + break; |
| 176 | + default: |
| 177 | + cache[key] = pair[1]; |
| 178 | + } |
| 179 | + }); |
| 180 | + |
| 181 | + // Recompose full style object (as string) |
| 182 | + styles = ""; |
| 183 | + angular.forEach(cache, function(value, key) { |
| 184 | + styles = styles + key + ":" + value + "; "; |
| 185 | + }); |
| 186 | + |
| 187 | + return styles; |
| 188 | + } |
| 189 | + |
| 190 | + }; |
| 191 | + |
| 192 | +})(window, window.angular); |
0 commit comments