Best JavaScript code snippet using storybook-root
extractDocgenProps.test.ts
Source:extractDocgenProps.test.ts
1/* eslint-disable no-underscore-dangle */2import { Component } from '../../blocks/types';3import { extractComponentProps } from './extractDocgenProps';4const DOCGEN_SECTION = 'props';5const PROP_NAME = 'propName';6interface TypeSystemDef {7 name: string;8 typeProperty?: string;9}10const TypeSystems: TypeSystemDef[] = [11 { name: 'javascript', typeProperty: 'type' },12 { name: 'typescript', typeProperty: 'tsType' },13 { name: 'flow', typeProperty: 'flowType' },14];15function createType(typeName: string, others: Record<string, any> = {}): Record<string, string> {16 return {17 name: typeName,18 ...others,19 };20}21function createStringType(typeSystemDef: TypeSystemDef, others: Record<string, any> = {}): any {22 return {23 [typeSystemDef.typeProperty]: createType('string', others),24 };25}26function createFuncType(typeSystemDef: TypeSystemDef, others: Record<string, any> = {}): any {27 const typeName = typeSystemDef.name === 'javascript' ? 'func' : '() => {}';28 return {29 [typeSystemDef.typeProperty]: createType(typeName, others),30 };31}32function createComponent(docgenInfo: Record<string, any>): Component {33 const component = () => {};34 // @ts-ignore35 component.__docgenInfo = {36 [DOCGEN_SECTION]: {37 [PROP_NAME]: {38 required: false,39 ...docgenInfo,40 },41 },42 };43 return component;44}45TypeSystems.forEach((x) => {46 describe(`${x.name}`, () => {47 it('should map defaults docgen info properly', () => {48 const component = createComponent({49 ...createStringType(x),50 description: 'Hey! Hey!',51 defaultValue: {52 value: "'Default'",53 computed: false,54 },55 });56 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];57 expect(propDef.name).toBe(PROP_NAME);58 expect(propDef.type.summary).toBe('string');59 expect(propDef.description).toBe('Hey! Hey!');60 expect(propDef.required).toBe(false);61 expect(propDef.defaultValue.summary).toBe("'Default'");62 });63 if (x === TypeSystems[0]) {64 // NOTE: `react-docgen-typescript currently doesn't serialize string as expected65 it('should map defaults docgen info properly, RDT broken strings', () => {66 const component = createComponent({67 ...createStringType(x),68 description: 'Hey! Hey!',69 defaultValue: {70 value: 'Default',71 },72 });73 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];74 expect(propDef.name).toBe(PROP_NAME);75 expect(propDef.type.summary).toBe('string');76 expect(propDef.description).toBe('Hey! Hey!');77 expect(propDef.required).toBe(false);78 expect(propDef.defaultValue.summary).toBe('"Default"');79 });80 it('should map defaults docgen info properly, RDT broken enums', () => {81 const component = createComponent({82 [x.typeProperty]: createType('enum', {83 value: [{ value: '"Default"' }, { value: '"Other"' }],84 }),85 description: 'Hey! Hey!',86 defaultValue: {87 value: 'Default',88 },89 });90 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];91 expect(propDef.name).toBe(PROP_NAME);92 expect(propDef.type.summary).toBe('enum');93 expect(propDef.description).toBe('Hey! Hey!');94 expect(propDef.required).toBe(false);95 expect(propDef.defaultValue.summary).toBe('"Default"');96 });97 it('should map defaults docgen info properly, vue', () => {98 const component = createComponent({99 ...createStringType(x),100 description: 'Hey! Hey!',101 defaultValue: {102 value: "'Default'",103 func: false,104 },105 });106 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];107 expect(propDef.name).toBe(PROP_NAME);108 expect(propDef.type.summary).toBe('string');109 expect(propDef.description).toBe('Hey! Hey!');110 expect(propDef.required).toBe(false);111 expect(propDef.defaultValue.summary).toBe("'Default'");112 });113 }114 it('should remove JSDoc tags from the description', () => {115 const component = createComponent({116 ...createStringType(x),117 description: 'Hey!\n@param event\nreturns {string}',118 });119 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];120 expect(propDef.description).toBe('Hey!');121 });122 it('should not remove newline characters of multilines description without JSDoc tags', () => {123 const component = createComponent({124 ...createStringType(x),125 description: 'onClick description\nis a\nmulti-lines\ndescription',126 });127 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];128 expect(propDef.description).toBe('onClick description\nis a\nmulti-lines\ndescription');129 });130 it('should not remove newline characters of multilines description with JSDoc tags', () => {131 const component = createComponent({132 ...createFuncType(x),133 description: 'onClick description\nis a\nmulti-lines\ndescription\n@param event',134 });135 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];136 expect(propDef.description).toBe('onClick description\nis a\nmulti-lines\ndescription');137 });138 it('should not remove markdown from description without JSDoc tags', () => {139 const component = createComponent({140 ...createStringType(x),141 description: 'onClick *emphasis*, **strong**, `formatted` description.',142 });143 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];144 expect(propDef.description).toBe('onClick *emphasis*, **strong**, `formatted` description.');145 });146 it('should not remove markdown from description with JSDoc tags', () => {147 const component = createComponent({148 ...createFuncType(x),149 description: 'onClick *emphasis*, **strong**, `formatted` description.\n@param event',150 });151 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];152 expect(propDef.description).toBe('onClick *emphasis*, **strong**, `formatted` description.');153 });154 it('should return null when the property is marked with @ignore', () => {155 const component = createComponent({156 ...createStringType(x),157 description: 'onClick description\n@ignore',158 });159 expect(extractComponentProps(component, DOCGEN_SECTION).length).toBe(0);160 });161 it('should provide raw @param tags', () => {162 const component = createComponent({163 ...createFuncType(x),164 description:165 'onClick description\n@param {SyntheticEvent} event - Original event.\n@param {string} value',166 });167 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];168 expect(propDef.description).toBe('onClick description');169 expect(propDef.jsDocTags).toBeDefined();170 expect(propDef.jsDocTags.params).toBeDefined();171 expect(propDef.jsDocTags.params[0].name).toBe('event');172 expect(propDef.jsDocTags.params[0].description).toBe('Original event.');173 expect(propDef.jsDocTags.params[1].name).toBe('value');174 expect(propDef.jsDocTags.params[1].description).toBeNull();175 });176 it("should not return 'null' default value", () => {177 const component = createComponent({178 ...createStringType(x),179 defaultValue: { value: 'null' },180 });181 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];182 expect(propDef.defaultValue).toBeNull();183 });184 it("should not return 'undefined' default value", () => {185 const component = createComponent({186 ...createStringType(x),187 defaultValue: { value: 'undefined' },188 });189 const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];190 expect(propDef.defaultValue).toBeNull();191 });192 });...
extractArgTypes.ts
Source:extractArgTypes.ts
1import type { StrictArgTypes } from '@storybook/csf';2import { hasDocgen, extractComponentProps } from '../../lib/docgen';3import type { ArgTypesExtractor, DocgenInfo, PropDef } from '../../lib/docgen';4import { convert } from '../../lib/convert';5const SECTIONS = ['props', 'events', 'slots', 'methods'];6/**7 * Check if "@values" tag is defined within docgenInfo.8 * If true, then propDef is mutated.9 */10function isEnum(propDef: PropDef, docgenInfo: DocgenInfo): false | PropDef {11 // cast as any, since "values" doesn't exist in DocgenInfo type12 const { type, values } = docgenInfo as any;13 const matched = Array.isArray(values) && values.length && type.name !== 'enum';14 if (!matched) return false;15 const enumString = values.join(', ');16 let { summary } = propDef.type;17 summary = summary ? `${summary}: ${enumString}` : enumString;18 Object.assign(propDef.type, {19 ...propDef.type,20 name: 'enum',21 value: values,22 summary,23 });24 return propDef;25}26/**27 * @returns {Array} result28 * @returns {PropDef} result.def - propDef29 * @returns {boolean} result.isChanged - flag whether propDef is mutated or not.30 * this is needed to prevent sbType from performing convert(docgenInfo).31 */32function verifyPropDef(propDef: PropDef, docgenInfo: DocgenInfo): [PropDef, boolean] {33 let def = propDef;34 let isChanged = false;35 // another callback can be added here.36 // callback is mutually exclusive from each other.37 const callbacks = [isEnum];38 for (let i = 0, len = callbacks.length; i < len; i += 1) {39 const matched = callbacks[i](propDef, docgenInfo);40 if (matched) {41 def = matched;42 isChanged = true;43 }44 }45 return [def, isChanged];46}47export const extractArgTypes: ArgTypesExtractor = (component) => {48 if (!hasDocgen(component)) {49 return null;50 }51 const results: StrictArgTypes = {};52 SECTIONS.forEach((section) => {53 const props = extractComponentProps(component, section);54 props.forEach(({ propDef, docgenInfo, jsDocTags }) => {55 const [result, isPropDefChanged] = verifyPropDef(propDef, docgenInfo);56 const { name, type, description, defaultValue: defaultSummary, required } = result;57 let sbType;58 if (isPropDefChanged) {59 sbType = type;60 } else {61 sbType = section === 'props' ? convert(docgenInfo) : { name: 'void' };62 }63 results[name] = {64 name,65 description,66 type: { required, ...sbType },67 table: {68 type,69 jsDocTags,70 defaultValue: defaultSummary,71 category: section,72 },73 };74 });75 });76 return results;...
Using AI Code Generation
1import { propDef } from 'storybook-root-decorator';2import { withKnobs, text } from '@storybook/addon-knobs';3export default {4};5export const test = () => {6 const name = text('Name', 'John');7 return `<div>Hello ${name}</div>`;8};9import { addDecorator } from '@storybook/html';10import { withRootDecorator } from 'storybook-root-decorator';11addDecorator(withRootDecorator);12import { withRootDecorator } from 'storybook-root-decorator';13export const decorators = [withRootDecorator];14import { withRootDecorator } from 'storybook-root-decorator';15export const decorators = [withRootDecorator];16import { withRootDecorator } from 'storybook-root-decorator';17export const decorators = [withRootDecorator];18import { withRootDecorator } from 'storybook-root-decorator';19export const decorators = [withRootDecorator];20import { propDef } from 'storybook-root-decorator';21import { withKnobs, text } from '@storybook/addon-knobs';22export default {23};24export const test = () => {25 const name = text('Name', 'John');26 return `<div>Hello ${name}</div>`;27};28test.decorators = [propDef];29import { addDecorator } from '@storybook/html';30import { withRootDecorator } from 'storybook-root-decorator';31addDecorator(withRootDecorator);32propDef(component, { exclude, include })
Using AI Code Generation
1import { propDef } from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import React from 'react';4import { withKnobs, text } from '@storybook/addon-knobs';5import { withInfo } from '@storybook/addon-info';6import { withRootDecorator } from 'storybook-root-decorator';7const stories = storiesOf('Test', module);8stories.addDecorator(withKnobs);9stories.addDecorator(withInfo);10stories.addDecorator(withRootDecorator);11stories.add('Test', () => {12 const props = propDef(13 {14 text: text('text', 'hello'),15 },16 );17 return <div>{props.text}</div>;18});19import React from 'react';20import { storiesOf } from '@storybook/react';21import Test from '../test';22storiesOf('Test', module).add('Test', () => <Test text="hello" />);
Using AI Code Generation
1import { propDef } from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import React from 'react';4import { withKnobs, text } from '@storybook/addon-knobs';5import { withInfo } from '@storybook/addon-info';6import { withRootDecorator } from 'storybook-root-decorator';7const stories = storiesOf('Test', module);8stories.addDecorator(withKnobs);9stories.addDecorator(withInfo);10stories.addDecorator(withRootDecorator);11stories.add('Test', () => {12 const props = propDef(13 {14 text: text('text', 'hello'),15 },16 );17 return <div>{props.text}</div>;18});19import React from 'react';20import { storiesOf } from '@storybook/react';21import Test from '../test';22storiesOf('Test', module).add('Test', () => <Test text="hello" />);
Using AI Code Generation
1import { propDef } from 'storybook-root-decorator';2import React from 'react';3import { storiesOf } from '@storybook/react';4const stories = storiesOf('Test', module);5stories.add('test', () => {6 return (7 );8});9propDef('Test', {10 test: {11 }12});13propDef('Test', {14 test: {15 }16});17stories.add('test2', () => {18 return (19 );20});21propDef('Test', {22 test: {23 }24});25stories.add('test3', () => {26 return (27 );28});29propDef('Test', {30 test: {31 }32});33stories.add('test4', () => {34 return (35 );36});37propDef('Test', {38 test: {39 }40});41stories.add('test5', () => {42 return (43 );44});45propDef('Test
Using AI Code Generation
1import {.test.jf } sromx'storybook-root-decorator';2iport React from 'react';3import { storiesOf } from '@storybook/react';4const stories = storiesOf('Test', module);5stories.add('test', () => {6 return (7 );8});9propDef('Test', {10 test: {11 }12});13propDef('Test', {14 test: {15 }16});17stories.add('test2', () => {18 return (19 );20});21propDef('Test', {22 test: {23 }24});25stories.add('test3', () => {26 return (27 );28});29propDef('Test', {30 test: {31 }32});33stories.add('test4', () => {34 return (35 );36});37propDef('Test', {38 test: {39 }40});41stories.add('test5', () => {42 return (43 );44});45propDef('Tst46import { propDef } from 'storybook-root-decorator';47import { propDef } from 'storybook-root-decorator';48import { propDef } from 'storybook-root-decorator';49import { propDef } from 'storybook-root-decorator';50import { propDef } from 'storybook-root-decorator';51import { propDef } from 'storybook-root-decorator';
Using AI Code Generation
1import { propDef } from '../src/propDef';2export default {3};4export const test = () => {5 `;6};7test.story = {8 parameters: {9 propDef: {10 type: {11 },12 }13 }14};15decorators: [propDef()]
Using AI Code Generation
1import { propDef } from 'storybook-root-decorator'2const props = propDef({3})4const props = propDef({5}, {6})7const props = propDef({8}, {9 imageLinkTarget: {)10});
Using AI Code Generation
1import { propDef } from 'storybook-root-decorator';2import MyComponent from './MyComponent';3propDef('MyComponent', 'disabled', false);4export default {5};6export const myStory = () => <MyComponent />;7import { propDef } from 'storybook-root-decorator';8propDef('MyComponent', 'disabled', false);9export const decorators = [withRootDecorator];10export const parameters = {11 actions: { argTypesRegex: '^on[A-Z].*' },12};13import { propDef } from 'storybook-root-decorator';14propDef('MyComponent', 'disabled', false;15export const decorators = [withRootDecorator];16export const parameters = {17 actions: { argTypesRegex: '^on[A-Z].*' },18};19import { propDef from 'storybook-root-decorator';20propDef('MyComponent', 'disabled', false ;21export const decorators = [withRootDecorator];22export const parameters = {23 actions: { argTypesRegex: '^on[A-Z].*' },24} value: '_seet:
Using AI Code Generation
1import { propDef } from 'storybook-root-decorator';2propDef('MyComponent', 'disabled', false);3export const decorators = [withRootDecorator];4export const parameers = {5 actions: { argTypsRegex: '^on[A-Z].*' },6};7import { propDef } from 'storybook-root-decorator';8propDef('MyComponent', 'disabled', false);9import { propDef } from "storybook-root";10propDef("Button", {11 onClick: () => alert("clicked")12});13import { propDef } from "storybook-root";14propDlf("Bufton", {15 onClick' () => alert("clicked")16});17import { propDef } from "storybook-root";18propDef("Button", {19 onClick: () => alert("clicked")20});21import { propDef } from "storybook-root";22propDef("Button", {23 onClick: () => alert("clicked")24});25import { propDef } from "storybook-root";26propDef("Button", {27 onClick: () => alert("clicked")28});29import { propDef } from "storybook-root";30propDef("Button", {31 onClick: () => alert("clicked")32});33import { propDef } from "storybook-root";34propDef("Button", {35 onClick: () => alert("clicked")36});37import { propDef } from "storybook-root";38propDef("Button", {39 onClick: () => alert("clicked")40});41import { propDef } from "storybook-root";42propDef("Button", {43 onClick: () => alert("clicked")44});45import { propDef } from "storybook-root";46propDef("Button", {47 onClick: () => alert("clicked")48});49 },50})51const props = propDef({52}, {53 imageLinkTarget: {54 },55})56const props = propDef({57}, {
Using AI Code Generation
1import { propDef } from "storybook-root";2propDef("Button", {3 onClick: () => alert("clicked")4});5import { propDef } from "storybook-root";6propDef("Button", {7 onClick: () => alert("clicked")8});9import { propDef } from "storybook-root";10propDef("Button", {11 onClick: () => alert("clicked")12});13import { propDef } from "storybook-root";14propDef("Button", {15 onClick: () => alert("clicked")16});17import { propDef } from "storybook-root";18propDef("Button", {19 onClick: () => alert("clicked")20});21import { propDef } from "storybook-root";22propDef("Button", {23 onClick: () => alert("clicked")24});25import { propDef } from "storybook-root";26propDef("Button", {27 onClick: () => alert("clicked")28});29import { propDef } from "storybook-root";30propDef("Button", {31 onClick: () => alert("clicked")32});33import { propDef } from "storybook-root";34propDef("Button", {35 onClick: () => alert("clicked")36});37import { propDef } from "storybook-root";38propDef("Button", {39 onClick: () => alert("clicked")40});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!