diff --git a/src/getComponentAndPattern.test.ts b/src/getComponentAndPattern.test.ts index ac33144..ff0b0da 100644 --- a/src/getComponentAndPattern.test.ts +++ b/src/getComponentAndPattern.test.ts @@ -96,6 +96,26 @@ describe('getComponentAndPattern', () => { expect(pattern).toEqual(undefined); }); + test('return errors when pattern value is not array', () => { + const context: StoryContext = { + id: 'id', + name: 'name', + kind: 'kind', + parameters: { + component: ComponentMock, + matrix: { + pattern: { + color: 'string', + }, + }, + }, + }; + const { errors, component, pattern } = getComponentAndPattern(context); + expect(errors).toEqual(['Parameter pattern value must be Array']); + expect(component).toEqual(undefined); + expect(pattern).toEqual(undefined); + }); + test('return errors when component and pattern are not provided', () => { const context: StoryContext = { id: 'id', diff --git a/src/getComponentAndPattern.ts b/src/getComponentAndPattern.ts index f86fe5a..c87d7d9 100644 --- a/src/getComponentAndPattern.ts +++ b/src/getComponentAndPattern.ts @@ -9,6 +9,8 @@ type CoponentAndPattern = { pattern?: MatrixProps['propsPattern']; }; +const isObject = (arg: any): boolean => arg && typeof arg === 'object'; + export function getComponentAndPattern({ parameters }: StoryContext): CoponentAndPattern { const { component, @@ -24,9 +26,16 @@ export function getComponentAndPattern({ parameters }: StoryContext): CoponentAn if (pattern && typeof pattern !== 'object') { errors.push('Parameter pattern must be Object'); } - if (pattern && typeof pattern === 'object' && Object.keys(pattern).length === 0) { + if (isObject(pattern) && Object.keys(pattern).length === 0) { errors.push('Parameter pattern must not be empty'); } + if ( + isObject(pattern) && + Object.keys(pattern).length !== 0 && + !Array.isArray(Object.entries(pattern)[0][1]) + ) { + errors.push('Parameter pattern value must be Array'); + } if (errors.length !== 0) { return { errors }; }