Skip to content

Commit

Permalink
fix(controls): use correct assertion for color type
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Jul 11, 2021
1 parent 1bd9907 commit 2ac6d8a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
56 changes: 33 additions & 23 deletions lib/client-api/src/inferControls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ describe('inferControls', () => {
warnSpy.mockRestore();
});

it('should return color type when matching color', () => {
it('should return color type when using color matcher', () => {
// passing a string, should return control type color
const inferredControls = inferControls(
getStoryContext({
argTypes: {
background: {
type: {
name: 'string',
value: 'red',
},
name: 'background',
},
Expand All @@ -55,31 +54,42 @@ describe('inferControls', () => {
expect(inferredControls.background.control.type).toEqual('color');
});

it('should return inferred type when matches color but arg is not a string', () => {
// passing an object which is unsupported, should infer the type to object
const inferredControls = inferControls(
getStoryContext({
argTypes: {
background: {
type: {
name: 'object',
value: {
rgb: [255, 255, 0],
},
},
name: 'background',
it('should return inferred type when using color matcher but arg passed is not a string', () => {
const sampleTypes = [
{
name: 'object',
value: {
rgb: {
name: 'number',
},
},
controls: {
matchers: {
color: /background/,
},
{ name: 'number' },
{ name: 'boolean' },
];

sampleTypes.forEach((type) => {
const inferredControls = inferControls(
getStoryContext({
argTypes: {
background: {
// passing an object which is unsupported
// should ignore color control and infer the type instead
type,
name: 'background',
},
},
},
})
);
controls: {
matchers: {
color: /background/,
},
},
})
);

expect(warnSpy).toHaveBeenCalled();
expect(inferredControls.background.control.type).toEqual('object');
expect(warnSpy).toHaveBeenCalled();
expect(inferredControls.background.control.type).toEqual(type.name);
});
});
});

Expand Down
3 changes: 2 additions & 1 deletion lib/client-api/src/inferControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const inferControl = (argType: ArgType, name: string, matchers: ControlsMatchers

// args that end with background or color e.g. iconColor
if (matchers.color && matchers.color.test(name)) {
const controlType = typeof argType.type.value;
const controlType = argType.type.name;

if (controlType === 'string') {
return { control: { type: 'color' } };
}
Expand Down

0 comments on commit 2ac6d8a

Please sign in to comment.