|
| 1 | +/** @jsx React.DOM */ |
| 2 | + |
| 3 | +var React = require('react/addons'); |
| 4 | +var Utils = React.addons.TestUtils; |
| 5 | +Bacon = require('baconjs'); |
| 6 | +var BaconMixin = require('react-bacon').BaconMixin; |
| 7 | + |
| 8 | +describe('BaconMixin', function() { |
| 9 | + describe('#eventStream', function() { |
| 10 | + it('returns the stream for that defined name', function() { |
| 11 | + var stream; |
| 12 | + var Component = React.createClass({ |
| 13 | + mixins: [BaconMixin], |
| 14 | + componentWillMount: function() { |
| 15 | + stream = this.eventStream('clicks'); |
| 16 | + }, |
| 17 | + render: function() { |
| 18 | + return <div /> |
| 19 | + } |
| 20 | + }); |
| 21 | + var component = Utils.renderIntoDocument(<Component />); |
| 22 | + expect(stream instanceof Bacon.EventStream).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns always the same event stream', function() { |
| 26 | + var reference, anotherReference; |
| 27 | + var Component = React.createClass({ |
| 28 | + mixins: [BaconMixin], |
| 29 | + componentWillMount: function() { |
| 30 | + reference = this.eventStream('clicks'); |
| 31 | + anotherReference = this.eventStream('clicks'); |
| 32 | + }, |
| 33 | + render: function() { |
| 34 | + return <div /> |
| 35 | + } |
| 36 | + }); |
| 37 | + var component = Utils.renderIntoDocument(<Component />); |
| 38 | + expect(reference).toBe(anotherReference); |
| 39 | + }); |
| 40 | + |
| 41 | + it('defines a function property with the name of the stream', function() { |
| 42 | + var Component = React.createClass({ |
| 43 | + mixins: [BaconMixin], |
| 44 | + componentWillMount: function() { |
| 45 | + this.eventStream('clicks'); |
| 46 | + }, |
| 47 | + render: function() { |
| 48 | + return <div /> |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + var component = Utils.renderIntoDocument(<Component />); |
| 53 | + expect(component.clicks.constructor).toBe(Function); |
| 54 | + }); |
| 55 | + |
| 56 | + it('that defined function can be used as event handler, and pushes to the stream', function() { |
| 57 | + var clickedDiv; |
| 58 | + var Component = React.createClass({ |
| 59 | + mixins: [BaconMixin], |
| 60 | + componentWillMount: function() { |
| 61 | + this.eventStream('clicks').onValue(function() { |
| 62 | + clickedDiv = true; |
| 63 | + }); |
| 64 | + }, |
| 65 | + render: function() { |
| 66 | + return <div onClick={this.clicks}/> |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + var component = Utils.renderIntoDocument(<Component />); |
| 71 | + var div = Utils.findRenderedDOMComponentWithTag(component, 'div'); |
| 72 | + Utils.Simulate.click(div); |
| 73 | + |
| 74 | + expect(clickedDiv).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it('removes subscribers when the component unmounts', function() { |
| 78 | + var stream; |
| 79 | + var Component = React.createClass({ |
| 80 | + mixins: [BaconMixin], |
| 81 | + componentWillMount: function() { |
| 82 | + stream = this.eventStream('clicks'); |
| 83 | + stream.onValue(function() { |
| 84 | + // Does something |
| 85 | + }); |
| 86 | + }, |
| 87 | + render: function() { |
| 88 | + return <div onClick={this.clicks}/> |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + var component = Utils.renderIntoDocument(<Component />); |
| 93 | + |
| 94 | + expect(stream.hasSubscribers()).toBe(true); |
| 95 | + |
| 96 | + var unmounted = React.unmountComponentAtNode(component.getDOMNode().parentNode); |
| 97 | + expect(unmounted).toBe(true); |
| 98 | + |
| 99 | + expect(stream.hasSubscribers()).toBe(false); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments