Best JavaScript code snippet using storybook-root
enhanceSource.test.ts
Source:enhanceSource.test.ts
1import { StoryContext } from '@storybook/addons';2import { enhanceSource } from './enhanceSource';3const emptyContext: StoryContext = {4 id: 'foo--bar',5 kind: 'foo',6 name: 'bar',7 args: {},8 argTypes: {},9 globals: {},10 parameters: {},11};12describe('addon-docs enhanceSource', () => {13 describe('no source loaded', () => {14 const baseContext = emptyContext;15 it('no transformSource', () => {16 expect(enhanceSource(baseContext)).toBeNull();17 });18 it('transformSource', () => {19 const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');20 const parameters = { ...baseContext.parameters, docs: { transformSource } };21 expect(enhanceSource({ ...baseContext, parameters })).toBeNull();22 });23 });24 describe('custom/mdx source loaded', () => {25 const source = 'storySource.source';26 const baseContext = {27 ...emptyContext,28 parameters: { storySource: { source } },29 };30 it('no transformSource', () => {31 expect(enhanceSource(baseContext)).toEqual({32 docs: { source: { code: 'storySource.source' } },33 });34 });35 it('transformSource', () => {36 const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');37 const parameters = { ...baseContext.parameters, docs: { transformSource } };38 expect(enhanceSource({ ...baseContext, parameters }).docs.source).toEqual({39 code: 'formatted: storySource.source',40 });41 });42 it('receives context as 2nd argument', () => {43 const transformSource = jest.fn();44 const parameters = { ...baseContext.parameters, docs: { transformSource } };45 const context = { ...baseContext, parameters };46 enhanceSource(context);47 expect(transformSource).toHaveBeenCalledWith(source, context);48 });49 });50 describe('storysource source loaded w/ locationsMap', () => {51 const baseContext = {52 ...emptyContext,53 parameters: {54 storySource: {55 source: 'storySource.source',56 locationsMap: {57 bar: { startBody: { line: 1, col: 5 }, endBody: { line: 1, col: 11 } },58 },59 },60 },61 };62 it('no transformSource', () => {63 expect(enhanceSource(baseContext)).toEqual({ docs: { source: { code: 'Source' } } });64 });65 it('transformSource', () => {66 const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');67 const parameters = { ...baseContext.parameters, docs: { transformSource } };68 expect(enhanceSource({ ...baseContext, parameters }).docs.source).toEqual({69 code: 'formatted: Source',70 });71 });72 it('receives context as 2nd argument', () => {73 const transformSource = jest.fn();74 const parameters = { ...baseContext.parameters, docs: { transformSource } };75 const context = { ...baseContext, parameters };76 enhanceSource(context);77 expect(transformSource).toHaveBeenCalledWith('Source', context);78 });79 });80 describe('custom docs.source provided', () => {81 const baseContext = {82 ...emptyContext,83 parameters: {84 storySource: { source: 'storySource.source' },85 docs: { source: { code: 'docs.source.code' } },86 },87 };88 it('no transformSource', () => {89 expect(enhanceSource(baseContext)).toBeNull();90 });91 it('transformSource', () => {92 const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');93 const { source } = baseContext.parameters.docs;94 const parameters = { ...baseContext.parameters, docs: { source, transformSource } };95 expect(enhanceSource({ ...baseContext, parameters })).toBeNull();96 });97 });...
index.js
Source:index.js
1const Path = require('path')2const { Duplex } = require('stream')3const { CsvTransform } = require('./csv-transform')4const { JsonTransform } = require('./json-transform')5const { XlsxTransform } = require('./xlsx-transform')6/**7 * @param {string} transformSource8 * @return {Promise<Duplex> | Duplex}9 */10const TransformProvider = transformSource => {11 if (typeof transformSource !== 'string') {12 throw new TypeError('Argument "transformSource" must be a string')13 }14 if (!transformSource.trim()) {15 throw Object.assign(16 new Error('Argument "transformSource" cannot be blank'),17 { name: 'ArgumentError', argName: 'transformSource', argValue: transformSource }18 )19 }20 switch (transformSource) {21 case 'json': return new JsonTransform()22 case 'csv': return new CsvTransform()23 case 'xlsx': return new XlsxTransform()24 // case 'xml':25 default:26 switch (Path.extname(transformSource)) {27 case '':28 case '.js':29 case '.mjs':30 return loadTransformFromModule(transformSource)31 default:32 throw Object.assign(33 new Error(`Argument "transformSource" invalid: ${transformSource}`),34 { name: 'ArgumentError', argName: 'transformSource', argValue: transformSource }35 )36 }37 }38}39const loadTransformFromModule = async moduleId => {40 let id = moduleId41 if (id.startsWith('.')) {42 id = Path.resolve(id)43 }44 const factory = require(id)45 if (typeof factory !== 'function') {46 throw Object.assign(47 new Error('Transform module must have a default export that is a function'),48 { name: 'InvalidModuleError', moduleId }49 )50 }51 try {52 const transform = await factory()53 if (!(transform instanceof Duplex)) {54 throw Object.assign(55 new Error('Transform module factory function must return a Duplex stream'),56 { name: 'InvalidTransformError' }57 )58 }59 return transform60 } catch (error) {61 if (error.name === 'InvalidTransformError') {62 throw error63 } else {64 throw Object.assign(65 new Error('Transform error encountered : ' + error),66 { name: 'TransformError', moduleId, error }67 )68 }69 }70}...
esm-loader.d.ts
Source:esm-loader.d.ts
1declare global {2 namespace EsmLoader {3 type Format =4 | 'builtin'5 | 'commonjs'6 | 'dynamic'7 | 'json'8 | 'module'9 | 'wasm'10 type Source = string | Buffer11 const hooks: {12 getFormat: Hooks.GetFormat13 resolve: Hooks.Resolve14 transformSource: Hooks.TransformSource15 }16 namespace Hooks {17 type GetFormat = (18 url: string,19 context: HookContext.GetFormat,20 defaultGetFormat: GetFormat21 ) => Promise<HookResult.GetFormat>22 type Resolve = (23 specifier: string,24 context: HookContext.Resolve,25 defaultResolve: Resolve26 ) => Promise<HookResult.Resolve>27 type TransformSource = (28 source: Source,29 context: HookContext.TransformSource,30 defaultTransformSource: TransformSource31 ) => Promise<HookResult.TransformSource>32 }33 namespace HookContext {34 type GetFormat = Record<never, never>35 type Resolve = { parentURL: string }36 type TransformSource = { format: Format; url: string }37 }38 namespace HookResult {39 type GetFormat = { format: Format }40 type Resolve = { url: string }41 type TransformSource = { source: Source }42 }43 }44}...
Using AI Code Generation
1const { transformSource } = require('storybook-root-configuration');2module.exports = {3 webpackFinal: async config => {4 return transformSource(config);5 },6};7const path = require('path');8const { addBabelPlugin } = require('customize-cra');9module.exports = {10 transformSource: config => {11 config.module.rules[0].include.push(path.resolve(__dirname, '../src'));12 config = addBabelPlugin('babel-plugin-styled-components', config);13 return config;14 },15};
Using AI Code Generation
1const { transformSource } = require('storybook-root-alias');2module.exports = {3 webpackFinal: async (config, { configType }) => {4 config.module.rules.push({5 test: /\.(stories|story)\.jsx?$/,6 loader: require.resolve('@storybook/source-loader'),7 options: { parser: 'javascript', injectStoryParameters: false },8 });9 config.module.rules.push({10 test: /\.(stories|story)\.jsx?$/,11 loader: require.resolve('babel-loader'),12 options: {13 presets: [['react-app', { flow: false, typescript: true }]],14 },15 });16 return config;17 },18};19module.exports = {20 stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],21};22import { addParameters } from '@storybook/react';23import { DocsContainer, DocsPage } from '@storybook/addon-docs/blocks';24import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';25import { withRootAlias } from 'storybook-root-alias';26addParameters({27 docs: {28 },29 viewport: {30 },31});32export const decorators = [withRootAlias];33import { addons } from '@storybook/addons';34import { create } from '@storybook/theming';35addons.setConfig({36 theme: create({37 }),38});39/>;40 document.body.style.backgroundColor = 'red';
Using AI Code Generation
1const { transformSource } = require('storybook-root-cause');2module.exports = function({ config }) {3 config.module.rules.push({4 test: /\.(stories|story)\.jsx?$/,5 {6 options: {7 ['@babel/plugin-transform-react-jsx', { pragma: 'h' }],8 {9 },10 },11 },12 include: [path.resolve(__dirname, '../src')],13 });14 return config;15};16import { transformSource } from 'storybook-root-cause';17export const parameters = {18};19import { h } from 'preact';20export default {21};22export const Text = () => <button>Hello Button</button>;23Text.story = {24};25export const Emoji = () => (26);27Emoji.story = {28};29import { h } from 'preact';30export default {31};32export const toStorybook = () => <h1>Hello World</h1>;33toStorybook.story = {34};
Using AI Code Generation
1module.exports = require('storybook-root-configuration').transformSource(2 require('path').resolve(__dirname, 'src/test.js')3);4import React from 'react';5import PropTypes from 'prop-types';6const Test = ({ name }) => <div>Hello {name}</div>;7Test.propTypes = {8};9export default Test;
Using AI Code Generation
1import { transformSource } from 'storybook-root-cause';2import { storiesOf } from '@storybook/react';3import { withKnobs } from '@storybook/addon-knobs';4import { withInfo } from '@storybook/addon-info';5import { withScreenshot } from 'storycap';6import { withRootCause } from 'storybook-root-cause';7import { withTests } from '@storybook/addon-jest';8import { withA11y } from '@storybook/addon-a11y';9import { withConsole } from '@storybook/addon-console';10import { withPerformance } from 'storybook-addon-performance';11import { withWebComponentsKnobs } from '@open-wc/demoing-storybook';12import { withActions } from '@storybook/addon-actions';13import { withCssResources } from '@storybook/addon-cssresources';14import { withViewport } from '@storybook/addon-viewport';15import { withBackgrounds } from '@storybook/addon-backgrounds';16import { withLinks } from '@storybook/addon-links';17import { withPercy } from '@percy-io/storybook';18import { withTests as withTests2 } from '@storybook/addon-jest';19import { withTests as withTests3 } from '@storybook/addon-jest';20import { withTests as withTests4 } from '@storybook/addon-jest';21import { withTests as withTests5 } from '@storybook/addon-jest';22import { withTests as withTests6 } from '@storybook/addon-jest';23import { withTests as withTests7 } from '@storybook/addon-jest';24import { withTests as withTests8 } from '@storybook/addon-jest';25import { withTests as withTests9 } from '@storybook/addon-jest';26import { withTests as withTests10 } from '@storybook/addon-jest';27import { withTests as withTests11 } from '@storybook/addon-jest';28import { withTests as withTests12 } from '@storybook/addon-jest';29import { withTests as withTests13 } from '@storybook/addon-jest';30import { withTests as withTests14 } from '@storybook/addon-jest';31import { withTests as withTests15 } from '@storybook/addon-jest';32import
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!!