Best JavaScript code snippet using storybook-root
Comparison.ts
Source:Comparison.ts
1export interface DiffType<T> {2 Action: Action3 NewData: T4 PreviousData: T5}6export enum Action {7 Create,8 Remove,9 Update,10 None,11}12export function RetrieveIdByPath<T>(item: T, pathToID: string): string {13 return pathToID.split('.').reduce((prevValue, currPathToApply) => {14 return prevValue[currPathToApply]15 }, item)16}17export function NormalizeArrayToObject<T>(list: T[], pathToID: string): any {18 return list.reduce((prev, curr) => {19 const ID = RetrieveIdByPath(curr, pathToID)20 return Object.assign({}, prev, { [ID]: curr })21 }, {})22}23export function CompareLists<T>(24 newList: T[],25 previousList: T[],26 areTheSameFunction: (elemA: T, elemB: T) => boolean,27 pathToID: string,28 shouldReturnNones: boolean = false29): DiffType<T>[] {30 const normalizedNewList = NormalizeArrayToObject(newList, pathToID)31 const normalizedPreviousList = NormalizeArrayToObject(previousList, pathToID)32 const addAndUpdateItems = newList33 .map((itemInNewList) => {34 const itemInPreviousList =35 normalizedPreviousList[RetrieveIdByPath(itemInNewList, pathToID)]36 if (itemInPreviousList) {37 if (areTheSameFunction(itemInNewList, itemInPreviousList)) {38 return {39 Action: Action.None,40 NewData: itemInNewList,41 PreviousData: itemInPreviousList,42 }43 } else {44 return {45 Action: Action.Update,46 NewData: itemInNewList,47 PreviousData: itemInPreviousList,48 }49 }50 } else {51 return {52 Action: Action.Create,53 NewData: itemInNewList,54 PreviousData: itemInPreviousList,55 }56 }57 })58 .filter((i) => shouldReturnNones || i.Action !== Action.None)59 const RemoveItems = previousList60 .map((itemInPreviousList) => {61 const itemInNewList =62 normalizedNewList[RetrieveIdByPath(itemInPreviousList, pathToID)]63 if (itemInNewList) {64 if (areTheSameFunction(itemInNewList, itemInPreviousList)) {65 return {66 Action: Action.None,67 NewData: itemInNewList,68 PreviousData: itemInPreviousList,69 }70 } else {71 return {72 Action: Action.None,73 NewData: itemInNewList,74 PreviousData: itemInPreviousList,75 }76 }77 } else {78 return {79 Action: Action.Remove,80 NewData: itemInPreviousList,81 PreviousData: itemInPreviousList,82 }83 }84 })85 .filter((i) => i.Action !== Action.None)86 return addAndUpdateItems.concat(RemoveItems)...
pathToid.test.ts
Source:pathToid.test.ts
1import pathToId from './pathToId';2describe('pathToId', () => {3 describe('pathToId', () => {4 it('errors on invalid path', () => {5 expect(() => pathToId('/something/random')).toThrow(/Invalid/);6 });7 it('errors empty path', () => {8 expect(() => pathToId(null)).toThrow(/Invalid/);9 });10 it('succeeds on a valid path', () => {11 expect(pathToId('/story/some--id')).toBe('some--id');12 });13 });...
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!!