UNPKG

wix-storybook-utils

Version:

Utilities for automated component documentation within Storybook

66 lines 2.87 kB
"use strict"; var tslib_1 = require("tslib"); 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: tslib_1.__assign(tslib_1.__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