@@ -258,8 +258,8 @@ describe("[Unit] ElementAssertion.test.ts", () => {
258258 const test = new ElementAssertion ( divTest ) ;
259259
260260 expect ( ( ) => test . toHaveAllClasses ( "foo" , "bar" , "baz" ) )
261- . toThrowError ( AssertionError )
262- . toHaveMessage ( 'Expected the element to have all of these classes: "foo bar baz"' ) ;
261+ . toThrowError ( AssertionError )
262+ . toHaveMessage ( 'Expected the element to have all of these classes: "foo bar baz"' ) ;
263263
264264 expect ( test . not . toHaveAllClasses ( "foo" , "bar" , "baz" ) ) . toBeEqual ( test ) ;
265265 } ) ;
@@ -296,4 +296,76 @@ describe("[Unit] ElementAssertion.test.ts", () => {
296296 } ) ;
297297 } ) ;
298298 } ) ;
299+ describe ( ".toHaveStyle" , ( ) => {
300+ context ( "when the element has the expected style" , ( ) => {
301+ it ( "returns the assertion instance" , ( ) => {
302+ const { getByTestId } = render (
303+ < div
304+ className = "foo bar test"
305+ style = { { border : "1px solid black" , color : "red" , display : "flex" } }
306+ data-testid = "test-div"
307+ /> ) ;
308+ const divTest = getByTestId ( "test-div" ) ;
309+ const test = new ElementAssertion ( divTest ) ;
310+
311+ expect ( test . toHaveStyle ( { border : "1px solid black" , color : "red" , display : "flex" } ) ) . toBeEqual ( test ) ;
312+
313+ expect ( ( ) => test . not . toHaveStyle ( { border : "1px solid black" , color : "red" , display : "flex" } ) )
314+ . toThrowError ( AssertionError )
315+ . toHaveMessage (
316+ // eslint-disable-next-line max-len
317+ 'Expected the element to NOT match the following style:\n{\n "border": "1px solid black",\n "color": "rgb(255, 0, 0)",\n "display": "flex"\n}' ,
318+ ) ;
319+ } ) ;
320+ } ) ;
321+
322+ context ( "when the element does not have the expected style" , ( ) => {
323+ it ( "throws an assertion error" , ( ) => {
324+ const { getByTestId } = render (
325+ < div
326+ className = "foo bar test"
327+ style = { { color : "blue" , display : "block" } }
328+ data-testid = "test-div"
329+ /> ,
330+ ) ;
331+
332+ const divTest = getByTestId ( "test-div" ) ;
333+ const test = new ElementAssertion ( divTest ) ;
334+
335+ expect ( ( ) => test . toHaveStyle ( ( { border : "1px solid black" , color : "red" , display : "flex" } ) ) )
336+ . toThrowError ( AssertionError )
337+ . toHaveMessage (
338+ // eslint-disable-next-line max-len
339+ 'Expected the element to match the following style:\n{\n "border": "1px solid black",\n "color": "rgb(255, 0, 0)",\n "display": "flex"\n}' ,
340+ ) ;
341+
342+ expect ( test . not . toHaveStyle ( { border : "1px solid black" , color : "red" , display : "flex" } ) ) . toBeEqual ( test ) ;
343+
344+ } ) ;
345+ } ) ;
346+ context ( "when the element partially match the style" , ( ) => {
347+ it ( "throws an assertion error" , ( ) => {
348+ const { getByTestId } = render (
349+ < div
350+ className = "foo bar test"
351+ style = { { border : "1px solid black" , color : "blue" , display : "block" } }
352+ data-testid = "test-div"
353+ /> ,
354+ ) ;
355+
356+ const divTest = getByTestId ( "test-div" ) ;
357+ const test = new ElementAssertion ( divTest ) ;
358+
359+ expect ( ( ) => test . toHaveStyle ( ( { color : "red" , display : "flex" } ) ) )
360+ . toThrowError ( AssertionError )
361+ . toHaveMessage (
362+ // eslint-disable-next-line max-len
363+ 'Expected the element to match the following style:\n{\n "color": "rgb(255, 0, 0)",\n "display": "flex"\n}' ,
364+ ) ;
365+
366+ expect ( test . not . toHaveStyle ( { border : "1px solid black" , color : "red" , display : "flex" } ) ) . toBeEqual ( test ) ;
367+
368+ } ) ;
369+ } ) ;
370+ } ) ;
299371} ) ;
0 commit comments