wix-storybook-utils
Version:
Utilities for automated component documentation within Storybook
64 lines • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.r = exports.Driver = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var enzyme_1 = require("enzyme");
var ErrorSpy = /** @class */ (function (_super) {
tslib_1.__extends(ErrorSpy, _super);
function ErrorSpy(props) {
var _this = _super.call(this, props) || this;
_this.state = { hasError: false };
return _this;
}
ErrorSpy.prototype.componentDidCatch = function (err, info) {
this.props.spy(err, info);
};
ErrorSpy.getDerivedStateFromError = function (err) {
return { hasError: true };
};
ErrorSpy.prototype.render = function () {
return this.state.hasError ? null : this.props.children;
};
return ErrorSpy;
}(React.Component));
var Driver = /** @class */ (function () {
function Driver(Component, hookPrefix) {
var _this = this;
this.selectRoot = function () { return _this.component.childAt(0); };
this.find = function (selector) { return _this.component.find(selector); };
this.select = function (hook) {
return _this.component.find("[data-hook=\"auto-testkit-".concat(_this.hookPrefix).concat(hook, "\"]"));
};
this.has = function (hook) {
return _this.component.exists("[data-hook=\"auto-testkit-".concat(_this.hookPrefix).concat(hook, "\"]"));
};
this.create = function (props, spy) {
if (spy === void 0) { spy = function (err) {
console.log(err.stack);
}; }
var Component = _this.Component;
var mounted = (0, enzyme_1.mount)(React.createElement(ErrorSpy, { spy: spy },
React.createElement(Component, tslib_1.__assign({}, props))));
return _this.reuse(mounted.childAt(0));
};
this.reuse = function (component) {
_this.component = component;
return _this;
};
this.reset = function () {
_this.component = undefined;
};
this.Component = Component;
this.hookPrefix = hookPrefix;
this.reset();
}
return Driver;
}());
exports.Driver = Driver;
var r = function (driver) {
afterEach(function () { return driver.reset(); });
return driver;
};
exports.r = r;
//# sourceMappingURL=driver.js.map