Advertisement
Guest User

Untitled

a guest
May 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. /**
  2. * Jasmine RequestAnimationFrame: a set of helpers for testing funcionality
  3. * that uses requestAnimationFrame under the Jasmine BDD framework for JavaScript.
  4. */
  5. ;(function() {
  6.  
  7. var index = 0,
  8. callbacks = {};
  9.  
  10. function MockRAF(global) {
  11. var realRAF = global.requestAnimationFrame,
  12. realCAF = global.cancelAnimationFrame;
  13.  
  14. /**
  15. * Mock for window.requestAnimationFrame
  16. */
  17. var mockRAF = function(fn) {
  18. if (typeof fn !== 'function') {
  19. throw new Error('You should pass a function to requestAnimationFrame');
  20. }
  21.  
  22. index++;
  23. callbacks[index] = fn;
  24.  
  25. return index;
  26. };
  27.  
  28. /**
  29. * Mock for window.cancelAnimationFrame
  30. */
  31. var mockCAF = function(requestID) {
  32. delete callbacks[requestID];
  33. };
  34.  
  35. /**
  36. * Install request animation frame mocks.
  37. */
  38. this.install = function() {
  39. global.requestAnimationFrame = mockRAF;
  40. global.cancelAnimationFrame = mockCAF;
  41. };
  42.  
  43. /**
  44. * Uninstall request animation frame mocks.
  45. */
  46. this.uninstall = function() {
  47. global.requestAnimationFrame = realRAF;
  48. global.cancelAnimationFrame = realCAF;
  49. };
  50.  
  51. /**
  52. * Simulate animation frame readiness.
  53. */
  54. this.tick = function(timestamp) {
  55. var fns = callbacks, fn, i;
  56.  
  57. callbacks = {};
  58.  
  59. for (i in fns) {
  60. fn = fns[i];
  61. fn.call(global, timestamp);
  62. }
  63. };
  64. }
  65.  
  66.  
  67. jasmine.RequestAnimationFrame = new MockRAF(window);
  68. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement