UNPKG

wix-storybook-utils

Version:

Utilities for automated component documentation within Storybook

76 lines 3.2 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; module.exports = { setProps: function (props) { var script = function (componentProps) { // for `browser.executeScript(fn, args)`: // * `fn` - function to run in browser context // * `args` - arguments to pass to `fn` // // unfortunately `args` can be only bool, number or string // and here we... hack a little to try and support more var parsers = [ { rule: function (value) { return value.toString() === '[object Object]'; }, parser: function (value) { return value; }, }, { rule: function (value) { return typeof value === 'string' && value.match(/^function|\(\)\s?=>/); }, /* tslint:disable */ parser: function (value) { return eval("(".concat(value, ")")); }, // eslint-disable-line no-eval /* tslint:enable */ }, { rule: function (value) { return Array.isArray(value); }, parser: function (value) { return value; }, }, { rule: function (value) { return typeof value === 'string' && !isNaN(Date.parse(value)); }, parser: function (value) { return new Date(value); }, }, { rule: function (value) { return typeof value === 'string'; }, parser: function (value) { return value; }, }, { // default rule: function () { return true; }, parser: function (value) { return JSON.parse(value); }, }, ]; var args = Object.keys(componentProps).reduce(function (allProps, key) { var parser = parsers.find(function (_a) { var rule = _a.rule; return rule(allProps[key]); }).parser; allProps[key] = parser(allProps[key]); return allProps; }, componentProps); // this is possible because: // <AutoExample ref={ref => window.autoexample = ref}/> window.autoexample.setState({ propsState: __assign(__assign({}, window.autoexample.state.propsState), args), }); }; return browser.executeScript(script, props); }, reset: function () { return browser.executeScript('window.autoexample.resetState()'); }, remount: function () { return browser.executeScript('window.story.remount()'); }, }; //# sourceMappingURL=protractor.driver.js.map