Best JavaScript code snippet using storybook-root
componentDecoratorParser.ts
Source:componentDecoratorParser.ts
1import * as vscode from 'vscode';2import * as ts from 'typescript';3import * as fs from 'fs';4import * as path from 'path';5import { ComponentDecoratorData, componentDecoratorName, IDecoratorMatchingStrategy, StyleParseResult, stylesPropertyName, styleUrlsPropertyName } from './common';6export class ComponentDecoratorParser {7 private sourceFile: ts.SourceFile | null;8 constructor(private doc: vscode.TextDocument) {9 this.sourceFile = this.decompileFile();10 }11 public getStylesFromDecorator(matchingStrategy: IDecoratorMatchingStrategy): StyleParseResult | null {12 try {13 if (this.sourceFile?.statements == null) {14 return null;15 }16 const [styleUrls, inlineStyles] = this.extractStylesFromDecorators(matchingStrategy);17 const dir = path.dirname(this.doc.uri.fsPath);18 const urls = styleUrls.map(x => path.join(dir, x));19 return [urls, inlineStyles];20 }21 catch {22 return null;23 }24 }25 private extractStylesFromDecorators(matchingStrategy: IDecoratorMatchingStrategy): StyleParseResult {26 const componentDecorator = this.findDecorator(matchingStrategy);27 if (componentDecorator == null) {28 return [[], []];29 }30 else {31 const styleUrls: string[] = [];32 const inlineStyles: string[] = [];33 const foundStyleUrls = componentDecorator[styleUrlsPropertyName] as string[];34 if (foundStyleUrls != null && foundStyleUrls.length > 0) {35 foundStyleUrls.forEach(x => styleUrls.push(x));36 }37 const foundStyles = componentDecorator[stylesPropertyName] as string[];38 if (foundStyles != null && foundStyles.length > 0) {39 foundStyles.forEach(x => inlineStyles.push(x));40 }41 return [styleUrls, inlineStyles];42 }43 }44 private findDecorator(matchingStrategy: IDecoratorMatchingStrategy): ComponentDecoratorData | null {45 for (const statement of this.sourceFile!.statements) {46 if (statement.decorators == null) {47 continue;48 }49 for (const decorator of statement.decorators) {50 const expression = decorator.expression as ts.CallExpression;51 if (expression.expression.getText() !== componentDecoratorName) {52 continue;53 }54 const componentDecorator = this.parseDecoratorFromExpression(expression);55 if (matchingStrategy.matches(this.sourceFile!, decorator, componentDecorator)) {56 return componentDecorator;57 }58 }59 }60 return null;61 }62 private parseDecoratorFromExpression(decoratorExpression: ts.CallExpression) {63 const componentDecorator: ComponentDecoratorData = {};64 const decoratorArgument = decoratorExpression.arguments[0] as ts.ObjectLiteralExpression;65 for (const prop of decoratorArgument.properties) {66 const propExpr = prop as ts.PropertyAssignment;67 const name = propExpr.name?.getText();68 if (name === styleUrlsPropertyName || name === stylesPropertyName) {69 ComponentDecoratorParser.parseDecoratorStringArrayValues(propExpr, componentDecorator, name);70 }71 else {72 const value = propExpr.initializer.getText();73 componentDecorator[name] = value;74 }75 }76 return componentDecorator;77 }78 private static parseDecoratorStringArrayValues(propExpr: ts.PropertyAssignment, componentDecorator: ComponentDecoratorData, name: string) {79 const styles: string[] = [];80 const valExpr = propExpr.initializer as ts.ArrayLiteralExpression;81 for (const element of valExpr.elements) {82 const text = element.getText();83 //element is surrounded with ' ' that needs to be removed84 styles.push(text.substring(1, text.length - 1));85 }86 componentDecorator[name] = styles;87 }88 private decompileFile() {89 try {90 const normalizedName = path.normalize(this.doc.uri.fsPath).replace(/\\/g, '/');91 const compilerOptions = {92 allowJs: true,93 noResolve: true,94 target: ts.ScriptTarget.ES5,95 };96 const compilerHost: ts.CompilerHost = {97 fileExists: () => true,98 getCanonicalFileName: (filename: string) => filename,99 getCurrentDirectory: () => "",100 getDefaultLibFileName: () => "lib.d.ts",101 getDirectories: () => [],102 getNewLine: () => this.doc.eol === 1 ? '\n' : '\r\n',103 getSourceFile: (filenameToGet: string) => {104 const source = this.doc.getText();105 const target = compilerOptions.target == null ? ts.ScriptTarget.ES5 : compilerOptions.target;106 return ts.createSourceFile(filenameToGet, source, target, true);107 },108 readFile: (x: string) => x,109 useCaseSensitiveFileNames: () => true,110 writeFile: (x: string) => x,111 };112 const program = ts.createProgram([normalizedName], compilerOptions, compilerHost);113 return program.getSourceFile(normalizedName) ?? null;114 }115 catch (e) {116 console.error(`Error parsing ts file: ${this.doc.uri.fsPath}: ${e}`);117 return null;118 }119 }...
index.d.ts
Source:index.d.ts
1declare module 'recompose' {2 import { ComponentLifecycle, ComponentClass, StatelessComponent, ValidationMap } from 'react';3 interface ComponentDecorator<TOriginalProps, TOwnProps> {4 (component: ComponentClass<TOriginalProps> | StatelessComponent<TOriginalProps>)5 : ComponentClass<TOwnProps>;6 }7 /**8 * Decorator that infers the type from the original component9 *10 * Can't use the above decorator because it would default the type to {}11 */12 export interface InferableComponentDecorator {13 <P, TComponentConstruct extends (ComponentClass<P> | StatelessComponent<P>)>14 (component: TComponentConstruct): TComponentConstruct;15 }16 export function withContext<ContextProps, ComponentOwnProps>(17 childContextTypes: ValidationMap<ContextProps>,18 getChildContext: (props: ComponentOwnProps) => any19 ): InferableComponentDecorator;20 export function mapProps<TNeededProps, TMappedProps, TChildProps>(21 propsMapper: (props: TNeededProps) => TMappedProps22 ): ComponentDecorator<TChildProps, TNeededProps>;23 export function withProps<TOriginalProps, TOwnProps>(24 createProps: (props: TOriginalProps) => TOriginalProps & TOwnProps | TOwnProps25 ): ComponentDecorator<TOriginalProps, TOriginalProps & TOwnProps>;26 export function pure<P, TComponentConstruct extends (ComponentClass<P> | StatelessComponent<P>)>27 (component: TComponentConstruct): TComponentConstruct;28 export function onlyUpdateForKeys(propKeys: Array<string>): InferableComponentDecorator;29 export function onlyUpdateForPropTypes(): InferableComponentDecorator;30 export function setPropTypes<TOwnProps>(31 propTypes: ValidationMap<TOwnProps>32 ): ComponentDecorator<{}, TOwnProps>;33 export function getContext<TOriginalProps, TContextProps>(34 contextTypes: ValidationMap<TContextProps>35 ): ComponentDecorator<TOriginalProps, TOriginalProps & TContextProps>;36 export function componentFromProp<TOriginalProps>(propName: string)37 : ComponentClass<TOriginalProps>;38 export function defaultProps(props: {}): InferableComponentDecorator;39 export function withState<TOriginalProps, TOwnProps>(40 stateName: string,41 stateUpdaterName: string,42 initialState: (props: Object) => any | any43 ): ComponentDecorator<TOriginalProps, TOwnProps>;44 type HandlerCreators = { [handlerName: string]: (props: Object) => Function }45 export function withHandlers<TOriginalProps, TNextProps>(46 handlerCreators: HandlerCreators47 ): ComponentDecorator<TOriginalProps, TNextProps>;48 export function lifecycle<P, S>(spec: ComponentLifecycle<P, S>): InferableComponentDecorator;49 export function compose<TOriginalProps, TNextProps>(50 ...functions: Array<Function>51 ): ComponentDecorator<TOriginalProps, TNextProps>;52 /**53 * Higher-order component version of shouldComponentUpdate(). The test54 * function accepts both the current props and the next props.55 */56 export function shouldUpdate<TProps>(57 test: (props: TProps, nextProps: TProps) => boolean58 ): ComponentDecorator<TProps, TProps>;59 export function renameProp<TOldProps, TNewProps>(60 oldName: string,61 newName: string62 ): ComponentDecorator<TOldProps, TNewProps>;63 export function renameProps<TOldProps, TNewProps>(64 nameMap: { [key: string]: string }65 ): ComponentDecorator<TOldProps, TNewProps>;...
index.js
Source:index.js
1import React from "react";2import Linkify from "react-linkify";3const componentDecorator = (href, text, key) => (4 <a href={href} key={key} target="_blank" rel="noopener noreferrer">5 {text}6 </a>7);8const LinkifyWithTargetBlank = ({ children }) => {9 return <Linkify componentDecorator={componentDecorator}>{children}</Linkify>;10};...
Using AI Code Generation
1import { componentDecorator } from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import { withKnobs } from '@storybook/addon-knobs';4import React from 'react';5import { withInfo } from '@storybook/addon-info';6import { action } from '@storybook/addon-actions';7import { withA11y } from '@storybook/addon-a11y';8import { withReadme } from 'storybook-readme';9import { withTests } from '@storybook/addon-jest';10import readme from './README.md';11import results from './test-results.json';12import MyComponent from '../MyComponent';13import './style.scss';14import '../test';15storiesOf('MyComponent', module)16 .addDecorator(withKnobs)17 .addDecorator(componentDecorator())18 .addDecorator(withInfo)19 .addDecorator(withA11y)20 .addDecorator(withReadme(readme))21 .addDecorator(withTests({ results }) )22 .add('MyComponent', () => <MyComponent onClick={action('clicked')} />);23import { componentDecorator } from 'storybook-root-decorator';24import { storiesOf } from '@storybook/react';25import { withKnobs } from '@storybook/addon-knobs';26import React from 'react';27import { withInfo } from '@storybook/addon-info';28import { action } from '@storybook/addon-actions';29import { withA11y } from '@storybook/addon-a11y';30import { withReadme } from 'storybook-readme';31import { withTests } from '@storybook/addon-jest';32import readme from './README.md';33import results from './test-results.json';34import MyComponent from '../MyComponent';35import './style.scss';36import '../test';37storiesOf('MyComponent', module)38 .addDecorator(withKnobs)39 .addDecorator(componentDecorator())40 .addDecorator(withInfo)41 .addDecorator(withA11y)42 .addDecorator(withReadme(readme))43 .addDecorator(withTests({ results }) )44 .add('MyComponent',
Using AI Code Generation
1import { componentDecorator } from 'storybook-root-decorator';2import { addDecorator } from '@storybook/react';3addDecorator(componentDecorator());4import { componentDecorator } from 'storybook-root-decorator';5import { addDecorator } from '@storybook/react';6addDecorator(componentDecorator());7import { componentDecorator } from 'storybook-root-decorator';8import { addDecorator } from '@storybook/react';9addDecorator(componentDecorator());10import { componentDecorator } from 'storybook-root-decorator';11import { addDecorator } from '@storybook/react';12addDecorator(componentDecorator());13import { componentDecorator } from 'storybook-root-decorator';14import { addDecorator } from '@storybook/react';15addDecorator(componentDecorator());16import { componentDecorator } from 'storybook-root-decorator';17import { addDecorator } from '@storybook/react';18addDecorator(componentDecorator());19import { componentDecorator } from 'storybook-root-decorator';20import { addDecorator } from '@storybook/react';21addDecorator(componentDecorator());22import { componentDecorator } from 'storybook-root-decorator';23import { addDecorator } from '@storybook/react';24addDecorator(componentDecorator());25import { componentDecorator } from 'storybook-root-decorator';26import { addDecorator } from '@storybook/react';27addDecorator(componentDecorator());28import { componentDecorator } from 'storybook-root-decorator';29import { addDecorator } from '@storybook/react';30addDecorator(componentDecorator());31import { componentDecorator } from 'storybook-root-decorator';32import { addDecorator } from '@storybook/react';33addDecorator(componentDecorator());34import { componentDecorator } from 'storybook-root-decorator';35import { addDecorator } from '@storybook/react';
Using AI Code Generation
1import { componentDecorator } from 'storybook-root-decorator'2import { storiesOf } from '@storybook/react'3storiesOf('SomeComponent', module)4 .addDecorator(componentDecorator)5 .add('some story', () => <SomeComponent />)6 .add('some other story', () => <SomeComponent />)7import { configure } from '@storybook/react'8import { addDecorator } from '@storybook/react'9import { componentDecorator } from 'storybook-root-decorator'10addDecorator(componentDecorator)11configure(() => require('../test.js'), module)12module.exports = (storybookBaseConfig, configType) => {13 storybookBaseConfig.module.rules.push({14 require.resolve('style-loader'),15 require.resolve('css-loader'),16 require.resolve('sass-loader'),17 })18}19import 'storybook-root-decorator/register'
Using AI Code Generation
1import { componentDecorator } from 'storybook-root-decorator';2storiesOf('component', module)3 .addDecorator(componentDecorator())4 .add('default', () => <Component />);5import { addDecorator } from '@storybook/react';6import { componentDecorator } from 'storybook-root-decorator';7addDecorator(componentDecorator());8import { addDecorator } from '@storybook/react';9import { componentDecorator } from 'storybook-root-decorator';10addDecorator(componentDecorator());11import { addDecorator } from '@storybook/react';12import { componentDecorator } from 'storybook-root-decorator';13addDecorator(componentDecorator());14import { addDecorator } from '@storybook/react';15import { componentDecorator } from 'storybook-root-decorator';16addDecorator(componentDecorator());17import { addDecorator } from '@storybook/react';18import { componentDecorator } from 'storybook-root-decorator';19addDecorator(componentDecorator());20import { addDecorator } from '@storybook/react';21import { componentDecorator } from 'storybook-root-decorator';22addDecorator(componentDecorator());23import { addDecorator } from '@storybook/react';24import { componentDecorator } from 'storybook-root-decorator';25addDecorator(componentDecorator());26import { addDecorator } from '@storybook/react';27import { componentDecorator } from 'storybook-root-decorator';28addDecorator(componentDecorator());29import { addDecorator } from '@storybook/react';30import { componentDecorator } from 'storybook-root-decorator';31addDecorator(componentDecorator());32import { addDecorator } from '@storybook/react';33import { componentDecorator } from 'storybook-root-decorator';34addDecorator(componentDecorator());35import {
Using AI Code Generation
1import { componentDecorator } from 'storybook-react-router';2import { addDecorator } from '@storybook/react';3addDecorator(componentDecorator);4import React from 'react';5import { storiesOf } from '@storybook/react';6import { withInfo } from '@storybook/addon-info';7import { withKnobs } from '@storybook/addon-knobs';8import { withRootDecorator } from 'storybook-root-decorator';9import { withRootDecorator } from 'storybook-root-decorator';10import { BrowserRouter as Router } from 'react-router-dom';11import { withRootDecorator } from 'storybook-root-decorator';12import { BrowserRouter as Router } from 'react-router-dom';13import { withRootDecorator } from 'storybook-root-decorator';14import { BrowserRouter as Router } from 'react-router-dom';15import { withRootDecorator } from 'storybook-root-decorator';16import { BrowserRouter as Router } from 'react-router-dom';17import { withRootDecorator } from 'storybook-root-decorator';18import { BrowserRouter as Router } from 'react-router-dom';19import { withRootDecorator } from 'storybook-root-decorator';20import { BrowserRouter as Router } from 'react-router-dom';21import { withRootDecorator } from 'storybook-root-decorator';22import { BrowserRouter as Router } from 'react-router-dom';23import { withRootDecorator } from 'storybook-root-decorator';24import { BrowserRouter as Router } from 'react-router-dom';25import { withRootDecorator } from 'storybook-root-decorator';26import { BrowserRouter as Router } from 'react-router-dom';27import { withRootDecorator } from 'storybook-root-decorator';28import { BrowserRouter as Router } from 'react-router-dom';29import { withRootDecorator } from 'storybook-root-decorator';30import { BrowserRouter as Router } from 'react-router-dom';31import { withRootDecorator } from 'storybook-root-decorator';32import { BrowserRouter as Router } from 'react-router-dom';33import { withRootDecorator } from 'storybook-root-decorator';34import { BrowserRouter as Router } from 'react-router-dom';35storiesOf('Button', module)36 .addDecorator(withRootDecorator)37 .addDecorator(withKnobs)38 .addDecorator(withInfo)39 .addDecorator(componentDecorator)40 .add('with text', () => <Button>Hello Button</Button>
Using AI Code Generation
1import { componentDecorator } from 'storybook-root-decorator';2import MyComponent from './MyComponent';3export default {4 decorators: [componentDecorator(MyComponent)],5};6export const Example1 = () => ({7});8export const Example2 = () => ({9});10import { componentDecorator } from 'storybook-root-decorator';11import MyComponent from './MyComponent';12export default {13 decorators: [componentDecorator(MyComponent)],14};15export const Example1 = () => ({16});17export const Example2 = () => ({18});19import { componentDecorator } from 'storybook-root-decorator';20import MyComponent from './MyComponent';21export default {22 decorators: [componentDecorator(MyComponent)],23};24export const Example1 = () => ({25});26export const Example2 = () => ({27});28import { componentDecorator } from 'storybook-root-decorator';29import MyComponent from './MyComponent';30export default {31 decorators: [componentDecorator(MyComponent)],
Using AI Code Generation
1import { componentDecorator } from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import { withKnobs } from '@storybook/addon-knobs';4import { withA11y } from '@storybook/addon-a11y';5import { withInfo } from '@storybook/addon-info';6import { withTests } from '@storybook/addon-jest';7import { withConsole } from '@storybook/addon-console';8import { withOptions } from '@storybook/addon-options';9import { withViewport } from '@storybook/addon-viewport';10import { withBackgrounds } from '@storybook/addon-backgrounds';11import { withLinks } from '@storybook/addon-links';12import { withNotes } from '@storybook/addon-notes';13import { withPerformance } from 'storybook-addon-performance';14import { withStorysource } from '@storybook/addon-storysource';15import decorator from './decorator';16import results from '../.jest-test-results.json';17storiesOf('Button', module)18 .addDecorator(componentDecorator(decorator))19 .addDecorator(withKnobs)20 .addDecorator(withA11y)21 .addDecorator(withInfo)22 .addDecorator(withTests({ results }))23 .addDecorator(24 withConsole({25 })26 .addDecorator(27 withOptions({28storiesOf('component', module)29 .addDecorator(componentDecorator())30 .add('default', () => <Component />);31import { addDecorator } from '@storybook/react';32import { componentDecorator } from 'storybook-root-decorator';33addDecorator(componentDecorator());34import { addDecorator } from '@storybook/react';35import { componentDecorator } from 'storybook-root-decorator';36addDecorator(componentDecorator());37import { addDecorator } from '@storybook/react';38import { componentDecorator } from 'storybook-root-decorator';39addDecorator(componentDecorator());40import { addDecorator } from '@storybook/react';41import { componentDecorator } from 'storybook-root-decorator';42addDecorator(componentDecorator());43import { addDecorator } from '@storybook/react';44import { componentDecorator } from 'storybook-root-decorator';45addDecorator(componentDecorator());46import { addDecorator } from '@storybook/react';47import { componentDecorator } from 'storybook-root-decorator';48addDecorator(componentDecorator());49import { addDecorator } from '@storybook/react';50import { componentDecorator } from 'storybook-root-decorator';51addDecorator(componentDecorator());52import { addDecorator } from '@storybook/react';53import { componentDecorator } from 'storybook-root-decorator';54addDecorator(componentDecorator());55import { addDecorator } from '@storybook/react';56import { componentDecorator } from 'storybook-root-decorator';57addDecorator(componentDecorator());58import { addDecorator } from '@storybook/react';59import { componentDecorator } from 'storybook-root-decorator';60addDecorator(componentDecorator());61import {om './README.md';62import results from './test-results.json';63import MyComponent from '../MyComponent';64import './style.scss';65import '../test';66storiesOf('MyComponent', module)67 .addDecorator(withKnobs)68 .addDecorator(componentDecorator())69 .addDecorator(withInfo)70 .addDecorator(withA11y)71 .addDecorator(withReadme(readme))72 .addDecorator(withTests({ results }) )73 .add('MyComponent', () => <MyComponent onClick={action('clicked')} />);74import { componentDecorator } from 'storybook-root-decorator';75import { storiesOf } from '@storybook/react';76import { withKnobs } from '@storybook/addon-knobs';77import React from 'react';78import { withInfo } from '@storybook/addon-info';79import { action } from '@storybook/addon-actions';80import { withA11y } from '@storybook/addon-a11y';81import { withReadme } from 'storybook-readme';82import { withTests } from '@storybook/addon-jest';83import readme from './README.md';84import results from './test-results.json';85import MyComponent from '../MyComponent';86import './style.scss';87import '../test';88storiesOf('MyComponent', module)89 .addDecorator(withKnobs)90 .addDecorator(componentDecorator())91 .addDecorator(withInfo)92 .addDecorator(withA11y)93 .addDecorator(withReadme(readme))94 .addDecorator(withTests({ results }) )95 .add('MyComponent',
Using AI Code Generation
1import { componentDecorator } from 'storybook-root-decorator'2import { storiesOf } from '@storybook/react'3storiesOf('SomeComponent', module)4 .addDecorator(componentDecorator)5 .add('some story', () => <SomeComponent />)6 .add('some other story', () => <SomeComponent />)7import { configure } from '@storybook/react'8import { addDecorator } from '@storybook/react'9import { componentDecorator } from 'storybook-root-decorator'10addDecorator(componentDecorator)11configure(() => require('../test.js'), module)12module.exports = (storybookBaseConfig, configType) => {13 storybookBaseConfig.module.rules.push({14 require.resolve('style-loader'),15 require.resolve('css-loader'),16 require.resolve('sass-loader'),17 })18}19import 'storybook-root-decorator/register'
Using AI Code Generation
1import { componentDecorator } from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import { withKnobs } from '@storybook/addon-knobs';4import { withA11y } from '@storybook/addon-a11y';5import { withInfo } from '@storybook/addon-info';6import { withTests } from '@storybook/addon-jest';7import { withConsole } from '@storybook/addon-console';8import { withOptions } from '@storybook/addon-options';9import { withViewport } from '@storybook/addon-viewport';10import { withBackgrounds } from '@storybook/addon-backgrounds';11import { withLinks } from '@storybook/addon-links';12import { withNotes } from '@storybook/addon-notes';13import { withPerformance } from 'storybook-addon-performance';14import { withStorysource } from '@storybook/addon-storysource';15import decorator from './decorator';16import results from '../.jest-test-results.json';17storiesOf('Button', module)18 .addDecorator(componentDecorator(decorator))19 .addDecorator(withKnobs)20 .addDecorator(withA11y)21 .addDecorator(withInfo)22 .addDecorator(withTests({ results }))23 .addDecorator(24 withConsole({25 })26 .addDecorator(27 withOptions({
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!!