angular-material-mocks.js 4.9 KB

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