es6.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const is = require('is-type-of');
  3. const sleep = require('ko-sleep');
  4. const mm = require('./mm');
  5. const mockDatas = mm.datas;
  6. // support generator
  7. mm.datas = function(mod, method, datas, timeout) {
  8. if (!is.generatorFunction(mod[method])) {
  9. return mockDatas.call(mm, mod, method, datas, timeout);
  10. }
  11. if (timeout) {
  12. timeout = parseInt(timeout, 10);
  13. }
  14. timeout = timeout || 0;
  15. mm(mod, method, function* () {
  16. yield sleep(timeout);
  17. return datas;
  18. });
  19. return this;
  20. };
  21. const mockData = mm.data;
  22. mm.data = function(mod, method, data, timeout) {
  23. if (!is.generatorFunction(mod[method])) {
  24. return mockData.call(mm, mod, method, data, timeout);
  25. }
  26. return mm.datas(mod, method, data, timeout);
  27. };
  28. const mockError = mm.error;
  29. mm.error = function(mod, method, error, props, timeout) {
  30. if (!is.generatorFunction(mod[method])) {
  31. return mockError.call(mm, mod, method, error, props, timeout);
  32. }
  33. error = mm._createError(error, props);
  34. if (timeout) {
  35. timeout = parseInt(timeout, 10);
  36. }
  37. timeout = timeout || 0;
  38. mm(mod, method, function* () {
  39. yield sleep(timeout);
  40. throw error;
  41. });
  42. return this;
  43. };
  44. const mockErrorOnce = mm.errorOnce;
  45. mm.errorOnce = function(mod, method, error, props, timeout) {
  46. if (!is.generatorFunction(mod[method])) {
  47. return mockErrorOnce.call(mm, mod, method, error, props, timeout);
  48. }
  49. error = mm._createError(error, props);
  50. if (timeout) {
  51. timeout = parseInt(timeout, 10);
  52. }
  53. timeout = timeout || 0;
  54. mm(mod, method, function* () {
  55. yield sleep(timeout);
  56. mm.restore();
  57. throw error;
  58. });
  59. return this;
  60. };