rx.testing.es6.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. declare module Rx {
  2. export interface Subscription {
  3. /**
  4. * Checks whether the given subscription is equal to the current instance.
  5. * @param other Subscription object to check for equality.
  6. * @returns {Boolean} true if both objects are equal; false otherwise.
  7. */
  8. equals(other: Subscription): boolean;
  9. /**
  10. * Returns a string representation of the current Subscription value.
  11. * @returns {String} String representation of the current Subscription value.
  12. */
  13. toString(): string;
  14. }
  15. interface SubscriptionStatic {
  16. /**
  17. * Creates a new subscription object with the given virtual subscription and unsubscription time.
  18. *
  19. * @constructor
  20. * @param {Number} subscribe Virtual time at which the subscription occurred.
  21. * @param {Number} unsubscribe Virtual time at which the unsubscription occurred.
  22. */
  23. new (subscribeAt: number, unsubscribeAt?: number): Subscription;
  24. }
  25. export var Subscription: SubscriptionStatic;
  26. export interface Recorded {
  27. /**
  28. * Checks whether the given recorded object is equal to the current instance.
  29. *
  30. * @param {Recorded} other Recorded object to check for equality.
  31. * @returns {Boolean} true if both objects are equal; false otherwise.
  32. */
  33. equals(other: Recorded): boolean;
  34. /**
  35. * Returns a string representation of the current Recorded value.
  36. *
  37. * @returns {String} String representation of the current Recorded value.
  38. */
  39. toString(): string;
  40. time: number;
  41. value: any;
  42. }
  43. interface RecordedStatic {
  44. /**
  45. * Creates a new object recording the production of the specified value at the given virtual time.
  46. *
  47. * @constructor
  48. * @param {Number} time Virtual time the value was produced on.
  49. * @param {Mixed} value Value that was produced.
  50. * @param {Function} comparer An optional comparer.
  51. */
  52. new (time: number, value: any, equalityComparer?: _Comparer<any, boolean>): Recorded;
  53. }
  54. export var Recorded: RecordedStatic;
  55. export var ReactiveTest: {
  56. /** Default virtual time used for creation of observable sequences in unit tests. */
  57. created: number;
  58. /** Default virtual time used to subscribe to observable sequences in unit tests. */
  59. subscribed: number;
  60. /** Default virtual time used to dispose subscriptions in unit tests. */
  61. disposed: number;
  62. /**
  63. * Factory method for an OnNext notification record at a given time with a given value or a predicate function.
  64. *
  65. * 1 - ReactiveTest.onNext(200, 42);
  66. * 2 - ReactiveTest.onNext(200, function (x) { return x.length == 2; });
  67. *
  68. * @param ticks Recorded virtual time the OnNext notification occurs.
  69. * @param value Recorded value stored in the OnNext notification or a predicate.
  70. * @return Recorded OnNext notification.
  71. */
  72. onNext(ticks: number, value: any): Recorded;
  73. /**
  74. * Factory method for an OnNext notification record at a given time with a given value or a predicate function.
  75. *
  76. * 1 - ReactiveTest.onNext(200, 42);
  77. * 2 - ReactiveTest.onNext(200, function (x) { return x.length == 2; });
  78. *
  79. * @param ticks Recorded virtual time the OnNext notification occurs.
  80. * @param value Recorded value stored in the OnNext notification or a predicate.
  81. * @return Recorded OnNext notification.
  82. */
  83. onNext(ticks: number, predicate: (value: any) => boolean): Recorded;
  84. /**
  85. * Factory method for an OnError notification record at a given time with a given error.
  86. *
  87. * 1 - ReactiveTest.onNext(200, new Error('error'));
  88. * 2 - ReactiveTest.onNext(200, function (e) { return e.message === 'error'; });
  89. *
  90. * @param ticks Recorded virtual time the OnError notification occurs.
  91. * @param exception Recorded exception stored in the OnError notification.
  92. * @return Recorded OnError notification.
  93. */
  94. onError(ticks: number, exception: any): Recorded;
  95. /**
  96. * Factory method for an OnError notification record at a given time with a given error.
  97. *
  98. * 1 - ReactiveTest.onNext(200, new Error('error'));
  99. * 2 - ReactiveTest.onNext(200, function (e) { return e.message === 'error'; });
  100. *
  101. * @param ticks Recorded virtual time the OnError notification occurs.
  102. * @param exception Recorded exception stored in the OnError notification.
  103. * @return Recorded OnError notification.
  104. */
  105. onError(ticks: number, predicate: (exception: any) => boolean): Recorded;
  106. /**
  107. * Factory method for an OnCompleted notification record at a given time.
  108. *
  109. * @param ticks Recorded virtual time the OnCompleted notification occurs.
  110. * @return Recorded OnCompleted notification.
  111. */
  112. onCompleted(ticks: number): Recorded;
  113. /**
  114. * Factory method for a subscription record based on a given subscription and disposal time.
  115. *
  116. * @param start Virtual time indicating when the subscription was created.
  117. * @param end Virtual time indicating when the subscription was disposed.
  118. * @return Subscription object.
  119. */
  120. subscribe(subscribeAt: number, unsubscribeAt?: number): Subscription;
  121. }
  122. export interface MockObserver<T> extends Observer<T> {
  123. messages: Recorded[];
  124. }
  125. interface MockObserverStatic extends ObserverStatic {
  126. new <T>(scheduler: IScheduler): MockObserver<T>;
  127. }
  128. export var MockObserver: MockObserverStatic;
  129. export interface TestScheduler extends VirtualTimeScheduler<number, number> {
  130. /**
  131. * Creates a cold observable using the specified timestamped notification messages either as an array or arguments.
  132. * @param messages Notifications to surface through the created sequence at their specified virtual time offsets from the sequence subscription time.
  133. * @return Cold observable sequence that can be used to assert the timing of subscriptions and notifications.
  134. */
  135. createColdObservable<T>(...records: Recorded[]): Observable<T>;
  136. /**
  137. * Creates a hot observable using the specified timestamped notification messages either as an array or arguments.
  138. * @param messages Notifications to surface through the created sequence at their specified absolute virtual times.
  139. * @return Hot observable sequence that can be used to assert the timing of subscriptions and notifications.
  140. */
  141. createHotObservable<T>(...records: Recorded[]): Observable<T>;
  142. /**
  143. * Creates an observer that records received notification messages and timestamps those.
  144. * @return Observer that can be used to assert the timing of received notifications.
  145. */
  146. createObserver<T>(): MockObserver<T>;
  147. /**
  148. * Creates a resolved promise with the given value and ticks
  149. * @param {Number} ticks The absolute time of the resolution.
  150. * @param {Any} value The value to yield at the given tick.
  151. * @returns {MockPromise} A mock Promise which fulfills with the given value.
  152. */
  153. createResolvedPromise<T>(ticks: number, value: T): IPromise<T>;
  154. /**
  155. * Creates a rejected promise with the given reason and ticks
  156. * @param {Number} ticks The absolute time of the resolution.
  157. * @param {Any} reason The reason for rejection to yield at the given tick.
  158. * @returns {MockPromise} A mock Promise which rejects with the given reason.
  159. */
  160. createRejectedPromise<T>(ticks: number, value: T): IPromise<T>;
  161. /**
  162. * Starts the test scheduler and uses the specified virtual times to invoke the factory function, subscribe to the resulting sequence, and dispose the subscription.
  163. *
  164. * @param create Factory method to create an observable sequence.
  165. * @param created Virtual time at which to invoke the factory to create an observable sequence.
  166. * @param subscribed Virtual time at which to subscribe to the created observable sequence.
  167. * @param disposed Virtual time at which to dispose the subscription.
  168. * @return Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.
  169. */
  170. startWithTiming<T>(create: () => Observable<T>, createdAt: number, subscribedAt: number, disposedAt: number): MockObserver<T>;
  171. /**
  172. * Starts the test scheduler and uses the specified virtual time to dispose the subscription to the sequence obtained through the factory function.
  173. * Default virtual times are used for factory invocation and sequence subscription.
  174. *
  175. * @param create Factory method to create an observable sequence.
  176. * @param disposed Virtual time at which to dispose the subscription.
  177. * @return Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.
  178. */
  179. startWithDispose<T>(create: () => Observable<T>, disposedAt: number): MockObserver<T>;
  180. /**
  181. * Starts the test scheduler and uses default virtual times to invoke the factory function, to subscribe to the resulting sequence, and to dispose the subscription.
  182. *
  183. * @param create Factory method to create an observable sequence.
  184. * @return Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.
  185. */
  186. startWithCreate<T>(create: () => Observable<T>): MockObserver<T>;
  187. }
  188. export var TestScheduler: {
  189. new (): TestScheduler;
  190. }
  191. }
  192. declare module "rx.testing" { export = Rx; }