Best JavaScript code snippet using storybook-root
ArgsStore.ts
Source:ArgsStore.ts
1import type { StoryId, Args } from '@storybook/csf';2import type { Story } from './types';3import { combineArgs, mapArgsToTypes, validateOptions, deepDiff, DEEPLY_EQUAL } from './args';4function deleteUndefined(obj: Record<string, any>) {5 // eslint-disable-next-line no-param-reassign6 Object.keys(obj).forEach((key) => obj[key] === undefined && delete obj[key]);7 return obj;8}9export class ArgsStore {10 initialArgsByStoryId: Record<StoryId, Args> = {};11 argsByStoryId: Record<StoryId, Args> = {};12 get(storyId: StoryId) {13 if (!(storyId in this.argsByStoryId)) {14 throw new Error(`No args known for ${storyId} -- has it been rendered yet?`);15 }16 return this.argsByStoryId[storyId];17 }18 setInitial(story: Story<any>) {19 if (!this.initialArgsByStoryId[story.id]) {20 this.initialArgsByStoryId[story.id] = story.initialArgs;21 this.argsByStoryId[story.id] = story.initialArgs;22 } else if (this.initialArgsByStoryId[story.id] !== story.initialArgs) {23 // When we get a new version of a story (with new initialArgs), we re-apply the same diff24 // that we had previously applied to the old version of the story25 const delta = deepDiff(this.initialArgsByStoryId[story.id], this.argsByStoryId[story.id]);26 this.initialArgsByStoryId[story.id] = story.initialArgs;27 this.argsByStoryId[story.id] = story.initialArgs;28 if (delta !== DEEPLY_EQUAL) {29 this.updateFromDelta(story, delta);30 }31 }32 }33 updateFromDelta(story: Story<any>, delta: Args) {34 // Use the argType to ensure we setting a type with defined options to something outside of that35 const validatedDelta = validateOptions(delta, story.argTypes);36 // NOTE: we use `combineArgs` here rather than `combineParameters` because changes to arg37 // array values are persisted in the URL as sparse arrays, and we have to take that into38 // account when overriding the initialArgs (e.g. we patch [,'changed'] over ['initial', 'val'])39 this.argsByStoryId[story.id] = combineArgs(this.argsByStoryId[story.id], validatedDelta);40 }41 updateFromPersisted(story: Story<any>, persisted: Args) {42 // Use the argType to ensure we aren't persisting the wrong type of value to the type.43 // For instance you could try and set a string-valued arg to a number by changing the URL44 const mappedPersisted = mapArgsToTypes(persisted, story.argTypes);45 return this.updateFromDelta(story, mappedPersisted);46 }47 update(storyId: StoryId, argsUpdate: Partial<Args>) {48 if (!(storyId in this.argsByStoryId)) {49 throw new Error(`No args known for ${storyId} -- has it been rendered yet?`);50 }51 this.argsByStoryId[storyId] = deleteUndefined({52 ...this.argsByStoryId[storyId],53 ...argsUpdate,54 });55 }...
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!!