From db5ccb35d392a1de88c5f6e14f14a0cf36138398 Mon Sep 17 00:00:00 2001 From: Bruno Castro Date: Sun, 26 Jul 2020 14:04:29 -0300 Subject: [PATCH] feat(toHaveStyle): handle when the prop style is undefined (#29) --- src/__tests__/to-have-style.js | 12 ++++++++++++ src/to-have-style.js | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/__tests__/to-have-style.js b/src/__tests__/to-have-style.js index 639f171..a179f94 100644 --- a/src/__tests__/to-have-style.js +++ b/src/__tests__/to-have-style.js @@ -36,4 +36,16 @@ describe('.toHaveStyle', () => { expect(() => expect(container).toHaveStyle({ fontWeight: 'bold' })).toThrowError(); expect(() => expect(container).not.toHaveStyle({ color: 'black' })).toThrowError(); }); + + test('handles when the style prop is undefined', () => { + const { getByTestId } = render( + + Hello World + , + ); + + const container = getByTestId('container'); + + expect(container).not.toHaveStyle({ fontWeight: 'bold' }); + }); }); diff --git a/src/to-have-style.js b/src/to-have-style.js index f865365..4cd3e5b 100644 --- a/src/to-have-style.js +++ b/src/to-have-style.js @@ -38,7 +38,7 @@ function expectedDiff(expected, elementStyles) { export function toHaveStyle(element, style) { checkReactElement(element, toHaveStyle, this); - const elementStyle = element.props.style; + const elementStyle = element.props.style ?? {}; const expected = Array.isArray(style) ? mergeAllStyles(style) : style; const received = Array.isArray(elementStyle) ? mergeAllStyles(elementStyle) : elementStyle;