Best JavaScript code snippet using storybook-root
GlobalsStore.ts
Source:GlobalsStore.ts
1import deprecate from 'util-deprecate';2import dedent from 'ts-dedent';3import type { Globals, GlobalTypes } from '@storybook/csf';4import { deepDiff, DEEPLY_EQUAL } from './args';5import { getValuesFromArgTypes } from './csf/getValuesFromArgTypes';6const setUndeclaredWarning = deprecate(7 () => {},8 dedent`9 Setting a global value that is undeclared (i.e. not in the user's initial set of globals10 or globalTypes) is deprecated and will have no effect in 7.0.11 `12);13export class GlobalsStore {14 allowedGlobalNames: Set<string>;15 initialGlobals: Globals;16 globals: Globals = {};17 set({ globals = {}, globalTypes = {} }: { globals?: Globals; globalTypes?: GlobalTypes }) {18 const delta = this.initialGlobals && deepDiff(this.initialGlobals, this.globals);19 this.allowedGlobalNames = new Set([...Object.keys(globals), ...Object.keys(globalTypes)]);20 const defaultGlobals: Globals = getValuesFromArgTypes(globalTypes);21 this.initialGlobals = { ...defaultGlobals, ...globals };22 this.globals = this.initialGlobals;23 if (delta && delta !== DEEPLY_EQUAL) {24 this.updateFromPersisted(delta);25 }26 }27 filterAllowedGlobals(globals: Globals) {28 return Object.entries(globals).reduce((acc, [key, value]) => {29 if (this.allowedGlobalNames.has(key)) acc[key] = value;30 return acc;31 }, {} as Globals);32 }33 updateFromPersisted(persisted: Globals) {34 const allowedUrlGlobals = this.filterAllowedGlobals(persisted);35 // Note that unlike args, we do not have the same type information for globals to allow us36 // to type check them here, so we just set them naively37 this.globals = { ...this.globals, ...allowedUrlGlobals };38 }39 get() {40 return this.globals;41 }42 update(newGlobals: Globals) {43 Object.keys(newGlobals).forEach((key) => {44 if (!this.allowedGlobalNames.has(key)) {45 setUndeclaredWarning();46 }47 });48 this.globals = { ...this.globals, ...newGlobals };49 }...
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!!