From 2a22a3961213b6cefca495e048948a3d0e1c7e75 Mon Sep 17 00:00:00 2001 From: Hammad Jutt Date: Wed, 22 Jul 2020 19:17:35 -0600 Subject: [PATCH] createVariant defaults bug fix Fix bug in createVariant where defaults were only applied when the variant prop was passed in --- src/createVariant.ts | 4 ++-- src/test/createVariant.test.ts | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/createVariant.ts b/src/createVariant.ts index 40754bcb..af828193 100644 --- a/src/createVariant.ts +++ b/src/createVariant.ts @@ -36,7 +36,7 @@ function createVariant< >({ property = 'variant' as P, themeKey, - defaults = {}, + defaults, }: { property?: P; themeKey: K; @@ -54,7 +54,7 @@ function createVariant< variant: true, func: (props, {theme, dimensions}) => { const {expandedProps} = styleFunction.func(props, {theme, dimensions}); - if (!expandedProps) return {}; + if (!expandedProps && !defaults) return {}; return allRestyleFunctions.buildStyle( {...defaults, ...expandedProps}, { diff --git a/src/test/createVariant.test.ts b/src/test/createVariant.test.ts index e1b5a22d..49aef770 100644 --- a/src/test/createVariant.test.ts +++ b/src/test/createVariant.test.ts @@ -48,10 +48,25 @@ describe('createVariant', () => { }); }); - it('accepts default values', () => { + it('accepts defaults', () => { const variant = createVariant({ themeKey: 'textVariants', defaults: { + fontSize: 10, + opacity: 0.5, + }, + }); + expect(variant.func({}, {theme, dimensions})).toStrictEqual({ + fontSize: 10, + opacity: 0.5, + }); + }); + + it('correctly overrides default values', () => { + const variant = createVariant({ + themeKey: 'textVariants', + defaults: { + fontSize: 10, opacity: 0.5, }, });