首页 手册 参考 源码 测试 关于中文版Repository
暗色主题亮色主题

spec-js/observables/combineLatest-spec.js

  1. "use strict";
  2. var chai_1 = require('chai');
  3. var Rx = require('../../dist/cjs/Rx');
  4. var Observable = Rx.Observable;
  5. var queueScheduler = Rx.Scheduler.queue;
  6. /** @test {combineLatest} */
  7. describe('Observable.combineLatest', function () {
  8. it('should combineLatest the provided observables', function () {
  9. var firstSource = hot('----a----b----c----|');
  10. var secondSource = hot('--d--e--f--g--|');
  11. var expected = '----uv--wx-y--z----|';
  12. var combined = Observable.combineLatest(firstSource, secondSource, function (a, b) { return '' + a + b; });
  13. expectObservable(combined).toBe(expected, { u: 'ad', v: 'ae', w: 'af', x: 'bf', y: 'bg', z: 'cg' });
  14. });
  15. it('should combine an immediately-scheduled source with an immediately-scheduled second', function (done) {
  16. var a = Observable.of(1, 2, 3, queueScheduler);
  17. var b = Observable.of(4, 5, 6, 7, 8, queueScheduler);
  18. var r = [[1, 4], [2, 4], [2, 5], [3, 5], [3, 6], [3, 7], [3, 8]];
  19. //type definition need to be updated
  20. Observable.combineLatest(a, b, queueScheduler).subscribe(function (vals) {
  21. chai_1.expect(vals).to.deep.equal(r.shift());
  22. }, function (x) {
  23. done(new Error('should not be called'));
  24. }, function () {
  25. chai_1.expect(r.length).to.equal(0);
  26. done();
  27. });
  28. });
  29. it('should accept array of observables', function () {
  30. var firstSource = hot('----a----b----c----|');
  31. var secondSource = hot('--d--e--f--g--|');
  32. var expected = '----uv--wx-y--z----|';
  33. var combined = Observable.combineLatest([firstSource, secondSource], function (a, b) { return '' + a + b; });
  34. expectObservable(combined).toBe(expected, { u: 'ad', v: 'ae', w: 'af', x: 'bf', y: 'bg', z: 'cg' });
  35. });
  36. it('should work with two nevers', function () {
  37. var e1 = cold('-');
  38. var e1subs = '^';
  39. var e2 = cold('-');
  40. var e2subs = '^';
  41. var expected = '-';
  42. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  43. expectObservable(result).toBe(expected);
  44. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  45. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  46. });
  47. it('should work with never and empty', function () {
  48. var e1 = cold('-');
  49. var e1subs = '^';
  50. var e2 = cold('|');
  51. var e2subs = '(^!)';
  52. var expected = '-';
  53. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  54. expectObservable(result).toBe(expected);
  55. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  56. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  57. });
  58. it('should work with empty and never', function () {
  59. var e1 = cold('|');
  60. var e1subs = '(^!)';
  61. var e2 = cold('-');
  62. var e2subs = '^';
  63. var expected = '-';
  64. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  65. expectObservable(result).toBe(expected);
  66. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  67. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  68. });
  69. it('should work with empty and empty', function () {
  70. var e1 = cold('|');
  71. var e1subs = '(^!)';
  72. var e2 = cold('|');
  73. var e2subs = '(^!)';
  74. var expected = '|';
  75. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  76. expectObservable(result).toBe(expected);
  77. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  78. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  79. });
  80. it('should work with hot-empty and hot-single', function () {
  81. var values = {
  82. a: 1,
  83. b: 2,
  84. c: 3,
  85. r: 1 + 3 //a + c
  86. };
  87. var e1 = hot('-a-^-|', values);
  88. var e1subs = '^ !';
  89. var e2 = hot('-b-^-c-|', values);
  90. var e2subs = '^ !';
  91. var expected = '----|';
  92. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  93. expectObservable(result).toBe(expected, values);
  94. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  95. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  96. });
  97. it('should work with hot-single and hot-empty', function () {
  98. var values = {
  99. a: 1, b: 2, c: 3
  100. };
  101. var e1 = hot('-a-^-|', values);
  102. var e1subs = '^ !';
  103. var e2 = hot('-b-^-c-|', values);
  104. var e2subs = '^ !';
  105. var expected = '----|';
  106. var result = Observable.combineLatest(e2, e1, function (x, y) { return x + y; });
  107. expectObservable(result).toBe(expected, values);
  108. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  109. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  110. });
  111. it('should work with hot-single and never', function () {
  112. var values = {
  113. a: 1
  114. };
  115. var e1 = hot('-a-^-|', values);
  116. var e1subs = '^ !';
  117. var e2 = hot('------', values); //never
  118. var e2subs = '^ ';
  119. var expected = '-'; //never
  120. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  121. expectObservable(result).toBe(expected, values);
  122. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  123. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  124. });
  125. it('should work with never and hot-single', function () {
  126. var values = {
  127. a: 1, b: 2
  128. };
  129. var e1 = hot('--------', values); //never
  130. var e1subs = '^ ';
  131. var e2 = hot('-a-^-b-|', values);
  132. var e2subs = '^ !';
  133. var expected = '-----'; //never
  134. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  135. expectObservable(result).toBe(expected, values);
  136. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  137. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  138. });
  139. it('should work with hot and hot', function () {
  140. var e1 = hot('--a--^--b--c--|', { a: 'a', b: 'b', c: 'c' });
  141. var e1subs = '^ !';
  142. var e2 = hot('---e-^---f--g--|', { e: 'e', f: 'f', g: 'g' });
  143. var e2subs = '^ !';
  144. var expected = '----x-yz--|';
  145. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  146. expectObservable(result).toBe(expected, { x: 'bf', y: 'cf', z: 'cg' });
  147. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  148. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  149. });
  150. it('should work with empty and error', function () {
  151. var e1 = hot('----------|'); //empty
  152. var e1subs = '^ !';
  153. var e2 = hot('------#', null, 'shazbot!'); //error
  154. var e2subs = '^ !';
  155. var expected = '------#';
  156. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  157. expectObservable(result).toBe(expected, null, 'shazbot!');
  158. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  159. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  160. });
  161. it('should work with error and empty', function () {
  162. var e1 = hot('--^---#', null, 'too bad, honk'); //error
  163. var e1subs = '^ !';
  164. var e2 = hot('--^--------|'); //empty
  165. var e2subs = '^ !';
  166. var expected = '----#';
  167. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  168. expectObservable(result).toBe(expected, null, 'too bad, honk');
  169. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  170. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  171. });
  172. it('should work with hot and throw', function () {
  173. var e1 = hot('-a-^--b--c--|', { a: 1, b: 2, c: 3 });
  174. var e1subs = '^ !';
  175. var e2 = hot('---^-#', null, 'bazinga');
  176. var e2subs = '^ !';
  177. var expected = '--#';
  178. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  179. expectObservable(result).toBe(expected, null, 'bazinga');
  180. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  181. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  182. });
  183. it('should work with throw and hot', function () {
  184. var e1 = hot('---^-#', null, 'bazinga');
  185. var e1subs = '^ !';
  186. var e2 = hot('-a-^--b--c--|', { a: 1, b: 2, c: 3 });
  187. var e2subs = '^ !';
  188. var expected = '--#';
  189. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  190. expectObservable(result).toBe(expected, null, 'bazinga');
  191. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  192. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  193. });
  194. it('should work with throw and throw', function () {
  195. var e1 = hot('---^----#', null, 'jenga');
  196. var e1subs = '^ !';
  197. var e2 = hot('---^-#', null, 'bazinga');
  198. var e2subs = '^ !';
  199. var expected = '--#';
  200. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  201. expectObservable(result).toBe(expected, null, 'bazinga');
  202. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  203. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  204. });
  205. it('should work with error and throw', function () {
  206. var e1 = hot('-a-^--b--#', { a: 1, b: 2 }, 'wokka wokka');
  207. var e1subs = '^ !';
  208. var e2 = hot('---^-#', null, 'flurp');
  209. var e2subs = '^ !';
  210. var expected = '--#';
  211. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  212. expectObservable(result).toBe(expected, null, 'flurp');
  213. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  214. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  215. });
  216. it('should work with throw and error', function () {
  217. var e1 = hot('---^-#', null, 'flurp');
  218. var e1subs = '^ !';
  219. var e2 = hot('-a-^--b--#', { a: 1, b: 2 }, 'wokka wokka');
  220. var e2subs = '^ !';
  221. var expected = '--#';
  222. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  223. expectObservable(result).toBe(expected, null, 'flurp');
  224. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  225. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  226. });
  227. it('should work with never and throw', function () {
  228. var e1 = hot('---^-----------');
  229. var e1subs = '^ !';
  230. var e2 = hot('---^-----#', null, 'wokka wokka');
  231. var e2subs = '^ !';
  232. var expected = '------#';
  233. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  234. expectObservable(result).toBe(expected, null, 'wokka wokka');
  235. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  236. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  237. });
  238. it('should work with throw and never', function () {
  239. var e1 = hot('---^----#', null, 'wokka wokka');
  240. var e1subs = '^ !';
  241. var e2 = hot('---^-----------');
  242. var e2subs = '^ !';
  243. var expected = '-----#';
  244. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  245. expectObservable(result).toBe(expected, null, 'wokka wokka');
  246. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  247. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  248. });
  249. it('should work with some and throw', function () {
  250. var e1 = hot('---^----a---b--|', { a: 1, b: 2 });
  251. var e1subs = '^ !';
  252. var e2 = hot('---^--#', null, 'wokka wokka');
  253. var e2subs = '^ !';
  254. var expected = '---#';
  255. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  256. expectObservable(result).toBe(expected, { a: 1, b: 2 }, 'wokka wokka');
  257. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  258. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  259. });
  260. it('should work with throw and some', function () {
  261. var e1 = hot('---^--#', null, 'wokka wokka');
  262. var e1subs = '^ !';
  263. var e2 = hot('---^----a---b--|', { a: 1, b: 2 });
  264. var e2subs = '^ !';
  265. var expected = '---#';
  266. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  267. expectObservable(result).toBe(expected, { a: 1, b: 2 }, 'wokka wokka');
  268. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  269. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  270. });
  271. it('should handle throw after complete left', function () {
  272. var left = hot('--a--^--b---|', { a: 1, b: 2 });
  273. var leftSubs = '^ !';
  274. var right = hot('-----^--------#', null, 'bad things');
  275. var rightSubs = '^ !';
  276. var expected = '---------#';
  277. var result = Observable.combineLatest(left, right, function (x, y) { return x + y; });
  278. expectObservable(result).toBe(expected, null, 'bad things');
  279. expectSubscriptions(left.subscriptions).toBe(leftSubs);
  280. expectSubscriptions(right.subscriptions).toBe(rightSubs);
  281. });
  282. it('should handle throw after complete right', function () {
  283. var left = hot('-----^--------#', null, 'bad things');
  284. var leftSubs = '^ !';
  285. var right = hot('--a--^--b---|', { a: 1, b: 2 });
  286. var rightSubs = '^ !';
  287. var expected = '---------#';
  288. var result = Observable.combineLatest(left, right, function (x, y) { return x + y; });
  289. expectObservable(result).toBe(expected, null, 'bad things');
  290. expectSubscriptions(left.subscriptions).toBe(leftSubs);
  291. expectSubscriptions(right.subscriptions).toBe(rightSubs);
  292. });
  293. it('should handle interleaved with tail', function () {
  294. var e1 = hot('-a--^--b---c---|', { a: 'a', b: 'b', c: 'c' });
  295. var e1subs = '^ !';
  296. var e2 = hot('--d-^----e---f--|', { d: 'd', e: 'e', f: 'f' });
  297. var e2subs = '^ !';
  298. var expected = '-----x-y-z--|';
  299. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  300. expectObservable(result).toBe(expected, { x: 'be', y: 'ce', z: 'cf' });
  301. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  302. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  303. });
  304. it('should handle two consecutive hot observables', function () {
  305. var e1 = hot('--a--^--b--c--|', { a: 'a', b: 'b', c: 'c' });
  306. var e1subs = '^ !';
  307. var e2 = hot('-----^----------d--e--f--|', { d: 'd', e: 'e', f: 'f' });
  308. var e2subs = '^ !';
  309. var expected = '-----------x--y--z--|';
  310. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  311. expectObservable(result).toBe(expected, { x: 'cd', y: 'ce', z: 'cf' });
  312. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  313. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  314. });
  315. it('should handle two consecutive hot observables with error left', function () {
  316. var left = hot('--a--^--b--c--#', { a: 'a', b: 'b', c: 'c' }, 'jenga');
  317. var leftSubs = '^ !';
  318. var right = hot('-----^----------d--e--f--|', { d: 'd', e: 'e', f: 'f' });
  319. var rightSubs = '^ !';
  320. var expected = '---------#';
  321. var result = Observable.combineLatest(left, right, function (x, y) { return x + y; });
  322. expectObservable(result).toBe(expected, null, 'jenga');
  323. expectSubscriptions(left.subscriptions).toBe(leftSubs);
  324. expectSubscriptions(right.subscriptions).toBe(rightSubs);
  325. });
  326. it('should handle two consecutive hot observables with error right', function () {
  327. var left = hot('--a--^--b--c--|', { a: 'a', b: 'b', c: 'c' });
  328. var leftSubs = '^ !';
  329. var right = hot('-----^----------d--e--f--#', { d: 'd', e: 'e', f: 'f' }, 'dun dun dun');
  330. var rightSubs = '^ !';
  331. var expected = '-----------x--y--z--#';
  332. var result = Observable.combineLatest(left, right, function (x, y) { return x + y; });
  333. expectObservable(result).toBe(expected, { x: 'cd', y: 'ce', z: 'cf' }, 'dun dun dun');
  334. expectSubscriptions(left.subscriptions).toBe(leftSubs);
  335. expectSubscriptions(right.subscriptions).toBe(rightSubs);
  336. });
  337. it('should handle selector throwing', function () {
  338. var e1 = hot('--a--^--b--|', { a: 1, b: 2 });
  339. var e1subs = '^ !';
  340. var e2 = hot('--c--^--d--|', { c: 3, d: 4 });
  341. var e2subs = '^ !';
  342. var expected = '---#';
  343. var result = Observable.combineLatest(e1, e2, function (x, y) { throw 'ha ha ' + x + ', ' + y; });
  344. expectObservable(result).toBe(expected, null, 'ha ha 2, 4');
  345. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  346. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  347. });
  348. it('should allow unsubscribing early and explicitly', function () {
  349. var e1 = hot('--a--^--b--c---d-| ');
  350. var e1subs = '^ ! ';
  351. var e2 = hot('---e-^---f--g---h-|');
  352. var e2subs = '^ ! ';
  353. var expected = '----x-yz-- ';
  354. var unsub = ' ! ';
  355. var values = { x: 'bf', y: 'cf', z: 'cg' };
  356. var result = Observable.combineLatest(e1, e2, function (x, y) { return x + y; });
  357. expectObservable(result, unsub).toBe(expected, values);
  358. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  359. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  360. });
  361. it('should not break unsubscription chains when unsubscribed explicitly', function () {
  362. var e1 = hot('--a--^--b--c---d-| ');
  363. var e1subs = '^ ! ';
  364. var e2 = hot('---e-^---f--g---h-|');
  365. var e2subs = '^ ! ';
  366. var expected = '----x-yz-- ';
  367. var unsub = ' ! ';
  368. var values = { x: 'bf', y: 'cf', z: 'cg' };
  369. var result = Observable.combineLatest(e1.mergeMap(function (x) { return Observable.of(x); }), e2.mergeMap(function (x) { return Observable.of(x); }), function (x, y) { return x + y; }).mergeMap(function (x) { return Observable.of(x); });
  370. expectObservable(result, unsub).toBe(expected, values);
  371. expectSubscriptions(e1.subscriptions).toBe(e1subs);
  372. expectSubscriptions(e2.subscriptions).toBe(e2subs);
  373. });
  374. it('should support promises', function () {
  375. type(function () {
  376. /* tslint:disable:no-unused-variable */
  377. var a;
  378. var b;
  379. var c;
  380. var o1 = Observable.combineLatest(a, b, c);
  381. var o2 = Observable.combineLatest(a, b, c, function (aa, bb, cc) { return !!aa && !!bb && cc; });
  382. /* tslint:enable:no-unused-variable */
  383. });
  384. });
  385. it('should support observables', function () {
  386. type(function () {
  387. /* tslint:disable:no-unused-variable */
  388. var a;
  389. var b;
  390. var c;
  391. var o1 = Observable.combineLatest(a, b, c);
  392. var o2 = Observable.combineLatest(a, b, c, function (aa, bb, cc) { return !!aa && !!bb && cc; });
  393. /* tslint:enable:no-unused-variable */
  394. });
  395. });
  396. it('should support mixed observables and promises', function () {
  397. type(function () {
  398. /* tslint:disable:no-unused-variable */
  399. var a;
  400. var b;
  401. var c;
  402. var d;
  403. var o1 = Observable.combineLatest(a, b, c, d);
  404. var o2 = Observable.combineLatest(a, b, c, d, function (aa, bb, cc, dd) { return !!aa && !!bb && cc && !!dd.length; });
  405. /* tslint:enable:no-unused-variable */
  406. });
  407. });
  408. it('should support arrays of promises', function () {
  409. type(function () {
  410. /* tslint:disable:no-unused-variable */
  411. var a;
  412. var o1 = Observable.combineLatest(a);
  413. var o2 = Observable.combineLatest.apply(Observable, a);
  414. var o3 = Observable.combineLatest(a, function () {
  415. var x = [];
  416. for (var _i = 0; _i < arguments.length; _i++) {
  417. x[_i - 0] = arguments[_i];
  418. }
  419. return x.length;
  420. });
  421. /* tslint:enable:no-unused-variable */
  422. });
  423. });
  424. it('should support arrays of observables', function () {
  425. type(function () {
  426. /* tslint:disable:no-unused-variable */
  427. var a;
  428. var o1 = Observable.combineLatest(a);
  429. var o2 = Observable.combineLatest.apply(Observable, a);
  430. var o3 = Observable.combineLatest(a, function () {
  431. var x = [];
  432. for (var _i = 0; _i < arguments.length; _i++) {
  433. x[_i - 0] = arguments[_i];
  434. }
  435. return x.length;
  436. });
  437. /* tslint:enable:no-unused-variable */
  438. });
  439. });
  440. it('should return Array<T> when given a single promise', function () {
  441. type(function () {
  442. /* tslint:disable:no-unused-variable */
  443. var a;
  444. var o1 = Observable.combineLatest(a);
  445. /* tslint:enable:no-unused-variable */
  446. });
  447. });
  448. it('should return Array<T> when given a single observable', function () {
  449. type(function () {
  450. /* tslint:disable:no-unused-variable */
  451. var a;
  452. var o1 = Observable.combineLatest(a);
  453. /* tslint:enable:no-unused-variable */
  454. });
  455. });
  456. });
  457. //# sourceMappingURL=combineLatest-spec.js.map