UNPKG

reblend-testing-library

Version:

Simple and complete Reblendjs testing utilities that encourage good testing practices.

69 lines (68 loc) 1.99 kB
"use strict"; var _reblendjs = require("reblendjs"); var _ = require("../"); class StopWatch extends _reblendjs.Reblend { initState() { this.state = { lapse: 0, running: false }; } handleRunClick() { this.setState(state => { if (state.running) { clearInterval(this.timer); } else { const startTime = Date.now() - this.state.lapse; this.timer = setInterval(() => { this.setState({ lapse: Date.now() - startTime }); }); } return { running: !state.running }; }); } handleClearClick() { clearInterval(this.timer); this.setState({ lapse: 0, running: false }); } componentWillUnmount() { clearInterval(this.timer); } html() { const { lapse, running } = this.state; return _reblendjs.Reblend.construct.bind(this)("div", null, _reblendjs.Reblend.construct.bind(this)("span", null, lapse, "ms"), _reblendjs.Reblend.construct.bind(this)("button", { onClick: this.handleRunClick }, running ? "Stop" : "Start"), _reblendjs.Reblend.construct.bind(this)("button", { onClick: this.handleClearClick }, "Clear")); } } const sleep = t => new Promise(resolve => setTimeout(resolve, t)); test("unmounts a component", async () => { const { unmount, container, debug } = await (0, _.render)(_reblendjs.Reblend.construct.bind(this)(StopWatch, null)); //debug(container); _.fireEvent.click(_.screen.getByText("Start")); await unmount(); // hey there reader! You don't need to have an assertion like this one // this is just me making sure that the unmount function works. // You don't need to do this in your apps. Just rely on the fact that this works. expect(container).toBeEmptyDOMElement(); // just wait to see if the interval is cleared or not // if it's not, then we'll call setState on an unmounted component // and get an error. await sleep(5); });