Repository URL to install this package:
|
Version:
1.2.13 ▾
|
"use strict";
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _events = require("events");
var _index = require("../index");
var _jsxFileName = "_forks/react-broadcast/__tests__/integration-test.js";
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
it("works", done => {
const steps = [];
const execNextStep = () => steps.shift()();
const div = document.createElement("div"); // so we can trigger rerenders in ComponentWithStateForDescendants
const emitter = new _events.EventEmitter(); // A component has some state it wants to make available to descendants
// 1. We create our Broadcast and Subscriber components
const CheeseBroadcast = ({
cheese,
children
}) => _react.default.createElement(_index.Broadcast, {
channel: "cheese",
value: cheese,
children: children,
__source: {
fileName: _jsxFileName,
lineNumber: 17
},
__self: void 0
});
const CheeseSubscriber = ({
children
}) => _react.default.createElement(_index.Subscriber, {
channel: "cheese",
children: children,
__source: {
fileName: _jsxFileName,
lineNumber: 21
},
__self: void 0
});
class ComponentWithStateForDescendants extends _react.default.Component {
constructor() {
super();
Object.defineProperty(this, "componentDidMount", {
configurable: true,
enumerable: true,
writable: true,
value: execNextStep
});
Object.defineProperty(this, "componentDidUpdate", {
configurable: true,
enumerable: true,
writable: true,
value: execNextStep
});
this.state = {
cheese: "cheddar"
};
emitter.on("CHEESE", cheese => {
this.setState({
cheese
});
});
}
render() {
// 2. render the Broadcast in the component w/ state and pass
// it the value we want accessible through context as a prop
// by the same name as when the Broadcast was created
return _react.default.createElement(CheeseBroadcast, {
cheese: this.state.cheese,
__source: {
fileName: _jsxFileName,
lineNumber: 42
},
__self: this
}, this.props.children);
}
}
let actualCheese = null;
steps.push(() => {
expect(actualCheese).toBe("cheddar");
emitter.emit("CHEESE", "gouda");
}, () => {
expect(actualCheese).toBe("gouda");
done();
}); // 3. Render a <Subscriber> that calls back when the Broadcast
// gets a new value in its prop
_reactDom.default.render(_react.default.createElement(ComponentWithStateForDescendants, {
__source: {
fileName: _jsxFileName,
lineNumber: 65
},
__self: void 0
}, _react.default.createElement(CheeseSubscriber, {
__source: {
fileName: _jsxFileName,
lineNumber: 66
},
__self: void 0
}, cheese => {
actualCheese = cheese;
return null;
})), div);
});