You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.0 KiB

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