Home Manual Reference Source Test Repository
dark themelight theme

spec-js/observables/fromEventPattern-spec.js

  1. "use strict";
  2. var chai_1 = require('chai');
  3. var sinon = require('sinon');
  4. var Rx = require('../../dist/package/Rx');
  5. var noop_1 = require('../../dist/package/util/noop');
  6. var Observable = Rx.Observable;
  7. /** @test {fromEventPattern} */
  8. describe('Observable.fromEventPattern', function () {
  9. asDiagram('fromEventPattern(addHandler, removeHandler)')('should create an observable from the handler API', function () {
  10. function addHandler(h) {
  11. Observable.timer(50, 20, rxTestScheduler)
  12. .mapTo('ev')
  13. .take(2)
  14. .concat(Observable.never())
  15. .subscribe(h);
  16. }
  17. var e1 = Observable.fromEventPattern(addHandler);
  18. var expected = '-----x-x---';
  19. expectObservable(e1).toBe(expected, { x: 'ev' });
  20. });
  21. it('should call addHandler on subscription', function () {
  22. var addHandler = sinon.spy();
  23. Observable.fromEventPattern(addHandler, noop_1.noop).subscribe(noop_1.noop);
  24. var call = addHandler.getCall(0);
  25. chai_1.expect(addHandler).calledOnce;
  26. chai_1.expect(call.args[0]).to.be.a('function');
  27. });
  28. it('should call removeHandler on unsubscription', function () {
  29. var removeHandler = sinon.spy();
  30. Observable.fromEventPattern(noop_1.noop, removeHandler).subscribe(noop_1.noop).unsubscribe();
  31. var call = removeHandler.getCall(0);
  32. chai_1.expect(removeHandler).calledOnce;
  33. chai_1.expect(call.args[0]).to.be.a('function');
  34. });
  35. it('should work without optional removeHandler', function () {
  36. var addHandler = sinon.spy();
  37. Observable.fromEventPattern(addHandler).subscribe(noop_1.noop);
  38. chai_1.expect(addHandler).calledOnce;
  39. });
  40. it('should deliver return value of addHandler to removeHandler as signal', function () {
  41. var expected = { signal: true };
  42. var addHandler = function () { return expected; };
  43. var removeHandler = sinon.spy();
  44. Observable.fromEventPattern(addHandler, removeHandler).subscribe(noop_1.noop).unsubscribe();
  45. var call = removeHandler.getCall(0);
  46. chai_1.expect(call).calledWith(sinon.match.any, expected);
  47. });
  48. it('should send errors in addHandler down the error path', function (done) {
  49. Observable.fromEventPattern(function (h) {
  50. throw 'bad';
  51. }, noop_1.noop).subscribe(function () { return done(new Error('should not be called')); }, function (err) {
  52. chai_1.expect(err).to.equal('bad');
  53. done();
  54. }, function () { return done(new Error('should not be called')); });
  55. });
  56. it('should accept a selector that maps outgoing values', function (done) {
  57. var target;
  58. var trigger = function () {
  59. var args = [];
  60. for (var _i = 0; _i < arguments.length; _i++) {
  61. args[_i - 0] = arguments[_i];
  62. }
  63. if (target) {
  64. target.apply(null, arguments);
  65. }
  66. };
  67. var addHandler = function (handler) {
  68. target = handler;
  69. };
  70. var removeHandler = function (handler) {
  71. target = null;
  72. };
  73. var selector = function (a, b) {
  74. return a + b + '!';
  75. };
  76. Observable.fromEventPattern(addHandler, removeHandler, selector).take(1)
  77. .subscribe(function (x) {
  78. chai_1.expect(x).to.equal('testme!');
  79. }, function (err) {
  80. done(new Error('should not be called'));
  81. }, function () {
  82. done();
  83. });
  84. trigger('test', 'me');
  85. });
  86. it('should send errors in the selector down the error path', function (done) {
  87. var target;
  88. var trigger = function (value) {
  89. if (target) {
  90. target(value);
  91. }
  92. };
  93. var addHandler = function (handler) {
  94. target = handler;
  95. };
  96. var removeHandler = function (handler) {
  97. target = null;
  98. };
  99. var selector = function (x) {
  100. throw 'bad';
  101. };
  102. Observable.fromEventPattern(addHandler, removeHandler, selector)
  103. .subscribe(function (x) {
  104. done(new Error('should not be called'));
  105. }, function (err) {
  106. chai_1.expect(err).to.equal('bad');
  107. done();
  108. }, function () {
  109. done(new Error('should not be called'));
  110. });
  111. trigger('test');
  112. });
  113. });
  114. //# sourceMappingURL=fromEventPattern-spec.js.map