Front end of the Slack clone application.

TestScheduler.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var Observable_1 = require('../Observable');
  8. var Notification_1 = require('../Notification');
  9. var ColdObservable_1 = require('./ColdObservable');
  10. var HotObservable_1 = require('./HotObservable');
  11. var SubscriptionLog_1 = require('./SubscriptionLog');
  12. var VirtualTimeScheduler_1 = require('../scheduler/VirtualTimeScheduler');
  13. var defaultMaxFrame = 750;
  14. var TestScheduler = (function (_super) {
  15. __extends(TestScheduler, _super);
  16. function TestScheduler(assertDeepEqual) {
  17. _super.call(this, VirtualTimeScheduler_1.VirtualAction, defaultMaxFrame);
  18. this.assertDeepEqual = assertDeepEqual;
  19. this.hotObservables = [];
  20. this.coldObservables = [];
  21. this.flushTests = [];
  22. }
  23. TestScheduler.prototype.createTime = function (marbles) {
  24. var indexOf = marbles.indexOf('|');
  25. if (indexOf === -1) {
  26. throw new Error('marble diagram for time should have a completion marker "|"');
  27. }
  28. return indexOf * TestScheduler.frameTimeFactor;
  29. };
  30. TestScheduler.prototype.createColdObservable = function (marbles, values, error) {
  31. if (marbles.indexOf('^') !== -1) {
  32. throw new Error('cold observable cannot have subscription offset "^"');
  33. }
  34. if (marbles.indexOf('!') !== -1) {
  35. throw new Error('cold observable cannot have unsubscription marker "!"');
  36. }
  37. var messages = TestScheduler.parseMarbles(marbles, values, error);
  38. var cold = new ColdObservable_1.ColdObservable(messages, this);
  39. this.coldObservables.push(cold);
  40. return cold;
  41. };
  42. TestScheduler.prototype.createHotObservable = function (marbles, values, error) {
  43. if (marbles.indexOf('!') !== -1) {
  44. throw new Error('hot observable cannot have unsubscription marker "!"');
  45. }
  46. var messages = TestScheduler.parseMarbles(marbles, values, error);
  47. var subject = new HotObservable_1.HotObservable(messages, this);
  48. this.hotObservables.push(subject);
  49. return subject;
  50. };
  51. TestScheduler.prototype.materializeInnerObservable = function (observable, outerFrame) {
  52. var _this = this;
  53. var messages = [];
  54. observable.subscribe(function (value) {
  55. messages.push({ frame: _this.frame - outerFrame, notification: Notification_1.Notification.createNext(value) });
  56. }, function (err) {
  57. messages.push({ frame: _this.frame - outerFrame, notification: Notification_1.Notification.createError(err) });
  58. }, function () {
  59. messages.push({ frame: _this.frame - outerFrame, notification: Notification_1.Notification.createComplete() });
  60. });
  61. return messages;
  62. };
  63. TestScheduler.prototype.expectObservable = function (observable, unsubscriptionMarbles) {
  64. var _this = this;
  65. if (unsubscriptionMarbles === void 0) { unsubscriptionMarbles = null; }
  66. var actual = [];
  67. var flushTest = { actual: actual, ready: false };
  68. var unsubscriptionFrame = TestScheduler
  69. .parseMarblesAsSubscriptions(unsubscriptionMarbles).unsubscribedFrame;
  70. var subscription;
  71. this.schedule(function () {
  72. subscription = observable.subscribe(function (x) {
  73. var value = x;
  74. // Support Observable-of-Observables
  75. if (x instanceof Observable_1.Observable) {
  76. value = _this.materializeInnerObservable(value, _this.frame);
  77. }
  78. actual.push({ frame: _this.frame, notification: Notification_1.Notification.createNext(value) });
  79. }, function (err) {
  80. actual.push({ frame: _this.frame, notification: Notification_1.Notification.createError(err) });
  81. }, function () {
  82. actual.push({ frame: _this.frame, notification: Notification_1.Notification.createComplete() });
  83. });
  84. }, 0);
  85. if (unsubscriptionFrame !== Number.POSITIVE_INFINITY) {
  86. this.schedule(function () { return subscription.unsubscribe(); }, unsubscriptionFrame);
  87. }
  88. this.flushTests.push(flushTest);
  89. return {
  90. toBe: function (marbles, values, errorValue) {
  91. flushTest.ready = true;
  92. flushTest.expected = TestScheduler.parseMarbles(marbles, values, errorValue, true);
  93. }
  94. };
  95. };
  96. TestScheduler.prototype.expectSubscriptions = function (actualSubscriptionLogs) {
  97. var flushTest = { actual: actualSubscriptionLogs, ready: false };
  98. this.flushTests.push(flushTest);
  99. return {
  100. toBe: function (marbles) {
  101. var marblesArray = (typeof marbles === 'string') ? [marbles] : marbles;
  102. flushTest.ready = true;
  103. flushTest.expected = marblesArray.map(function (marbles) {
  104. return TestScheduler.parseMarblesAsSubscriptions(marbles);
  105. });
  106. }
  107. };
  108. };
  109. TestScheduler.prototype.flush = function () {
  110. var hotObservables = this.hotObservables;
  111. while (hotObservables.length > 0) {
  112. hotObservables.shift().setup();
  113. }
  114. _super.prototype.flush.call(this);
  115. var readyFlushTests = this.flushTests.filter(function (test) { return test.ready; });
  116. while (readyFlushTests.length > 0) {
  117. var test = readyFlushTests.shift();
  118. this.assertDeepEqual(test.actual, test.expected);
  119. }
  120. };
  121. TestScheduler.parseMarblesAsSubscriptions = function (marbles) {
  122. if (typeof marbles !== 'string') {
  123. return new SubscriptionLog_1.SubscriptionLog(Number.POSITIVE_INFINITY);
  124. }
  125. var len = marbles.length;
  126. var groupStart = -1;
  127. var subscriptionFrame = Number.POSITIVE_INFINITY;
  128. var unsubscriptionFrame = Number.POSITIVE_INFINITY;
  129. for (var i = 0; i < len; i++) {
  130. var frame = i * this.frameTimeFactor;
  131. var c = marbles[i];
  132. switch (c) {
  133. case '-':
  134. case ' ':
  135. break;
  136. case '(':
  137. groupStart = frame;
  138. break;
  139. case ')':
  140. groupStart = -1;
  141. break;
  142. case '^':
  143. if (subscriptionFrame !== Number.POSITIVE_INFINITY) {
  144. throw new Error('found a second subscription point \'^\' in a ' +
  145. 'subscription marble diagram. There can only be one.');
  146. }
  147. subscriptionFrame = groupStart > -1 ? groupStart : frame;
  148. break;
  149. case '!':
  150. if (unsubscriptionFrame !== Number.POSITIVE_INFINITY) {
  151. throw new Error('found a second subscription point \'^\' in a ' +
  152. 'subscription marble diagram. There can only be one.');
  153. }
  154. unsubscriptionFrame = groupStart > -1 ? groupStart : frame;
  155. break;
  156. default:
  157. throw new Error('there can only be \'^\' and \'!\' markers in a ' +
  158. 'subscription marble diagram. Found instead \'' + c + '\'.');
  159. }
  160. }
  161. if (unsubscriptionFrame < 0) {
  162. return new SubscriptionLog_1.SubscriptionLog(subscriptionFrame);
  163. }
  164. else {
  165. return new SubscriptionLog_1.SubscriptionLog(subscriptionFrame, unsubscriptionFrame);
  166. }
  167. };
  168. TestScheduler.parseMarbles = function (marbles, values, errorValue, materializeInnerObservables) {
  169. if (materializeInnerObservables === void 0) { materializeInnerObservables = false; }
  170. if (marbles.indexOf('!') !== -1) {
  171. throw new Error('conventional marble diagrams cannot have the ' +
  172. 'unsubscription marker "!"');
  173. }
  174. var len = marbles.length;
  175. var testMessages = [];
  176. var subIndex = marbles.indexOf('^');
  177. var frameOffset = subIndex === -1 ? 0 : (subIndex * -this.frameTimeFactor);
  178. var getValue = typeof values !== 'object' ?
  179. function (x) { return x; } :
  180. function (x) {
  181. // Support Observable-of-Observables
  182. if (materializeInnerObservables && values[x] instanceof ColdObservable_1.ColdObservable) {
  183. return values[x].messages;
  184. }
  185. return values[x];
  186. };
  187. var groupStart = -1;
  188. for (var i = 0; i < len; i++) {
  189. var frame = i * this.frameTimeFactor + frameOffset;
  190. var notification = void 0;
  191. var c = marbles[i];
  192. switch (c) {
  193. case '-':
  194. case ' ':
  195. break;
  196. case '(':
  197. groupStart = frame;
  198. break;
  199. case ')':
  200. groupStart = -1;
  201. break;
  202. case '|':
  203. notification = Notification_1.Notification.createComplete();
  204. break;
  205. case '^':
  206. break;
  207. case '#':
  208. notification = Notification_1.Notification.createError(errorValue || 'error');
  209. break;
  210. default:
  211. notification = Notification_1.Notification.createNext(getValue(c));
  212. break;
  213. }
  214. if (notification) {
  215. testMessages.push({ frame: groupStart > -1 ? groupStart : frame, notification: notification });
  216. }
  217. }
  218. return testMessages;
  219. };
  220. return TestScheduler;
  221. }(VirtualTimeScheduler_1.VirtualTimeScheduler));
  222. exports.TestScheduler = TestScheduler;
  223. //# sourceMappingURL=TestScheduler.js.map