@@ -3,6 +3,7 @@ import { render } from "@testing-library/react";
33
44import { ElementAssertion } from "../../../src/lib/ElementAssertion" ;
55
6+ import { FocusTestComponent } from "./fixtures/focusTestComponent" ;
67import { HaveClassTest } from "./fixtures/HaveClassTest" ;
78import { NestedElementsTest } from "./fixtures/NestedElementsTest" ;
89import { SimpleTest } from "./fixtures/SimpleTest" ;
@@ -257,11 +258,157 @@ describe("[Unit] ElementAssertion.test.ts", () => {
257258 const test = new ElementAssertion ( divTest ) ;
258259
259260 expect ( ( ) => test . toHaveAllClasses ( "foo" , "bar" , "baz" ) )
260- . toThrowError ( AssertionError )
261- . 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"' ) ;
262263
263264 expect ( test . not . toHaveAllClasses ( "foo" , "bar" , "baz" ) ) . toBeEqual ( test ) ;
264265 } ) ;
265266 } ) ;
266267 } ) ;
268+
269+ describe ( ".toHaveFocus" , ( ) => {
270+ context ( "when the element has focus" , ( ) => {
271+ it ( "returns the assertion instance" , ( ) => {
272+ const { getByTestId } = render ( < FocusTestComponent /> ) ;
273+ const input1 = getByTestId ( "input1" ) ;
274+ input1 . focus ( ) ;
275+ const test = new ElementAssertion ( input1 ) ;
276+
277+ expect ( test . toHaveFocus ( ) ) . toBeEqual ( test ) ;
278+
279+ expect ( ( ) => test . not . toHaveFocus ( ) )
280+ . toThrowError ( AssertionError )
281+ . toHaveMessage ( "Expected the element NOT to be focused" ) ;
282+ } ) ;
283+ } ) ;
284+
285+ context ( "when the element does not have focus" , ( ) => {
286+ it ( "throws an assertion error" , ( ) => {
287+ const { getByTestId } = render ( < FocusTestComponent /> ) ;
288+ const input1 = getByTestId ( "input1" ) ;
289+ const test = new ElementAssertion ( input1 ) ;
290+
291+ expect ( ( ) => test . toHaveFocus ( ) )
292+ . toThrowError ( AssertionError )
293+ . toHaveMessage ( "Expected the element to be focused" ) ;
294+
295+ expect ( test . not . toHaveFocus ( ) ) . toBeEqual ( test ) ;
296+ } ) ;
297+ } ) ;
298+ } ) ;
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+ } ) ;
371+
372+ describe ( ".toHaveSomeStyle" , ( ) => {
373+ context ( "when the element contains one or more expected styles" , ( ) => {
374+ it ( "returns the assertion instance" , ( ) => {
375+ const { getByTestId } = render (
376+ < div
377+ style = { { color : "blue" , maxHeight : "3rem" , width : "2rem" } }
378+ data-testid = "test-div"
379+ /> ,
380+ ) ;
381+ const divTest = getByTestId ( "test-div" ) ;
382+ const test = new ElementAssertion ( divTest ) ;
383+
384+ expect ( test . toHaveSomeStyle ( { color : "red" , display : "flex" , height : "3rem" , width : "2rem" } ) ) . toBeEqual ( test ) ;
385+
386+ expect ( ( ) => test . not . toHaveSomeStyle ( { color : "blue" } ) )
387+ . toThrowError ( AssertionError )
388+ // eslint-disable-next-line max-len
389+ . toHaveMessage ( "Expected the element NOT to match some of the following styles:\n{\n \"color\": \"rgb(0, 0, 255)\"\n}" ) ;
390+ } ) ;
391+ } ) ;
392+
393+ context ( "when the element does not contain any of the expected styles" , ( ) => {
394+ it ( "throws an assertion error" , ( ) => {
395+ const { getByTestId } = render (
396+ < div
397+ className = "foo bar test"
398+ style = { { border : "1px solid black" , color : "blue" , display : "block" } }
399+ data-testid = "test-div"
400+ /> ,
401+ ) ;
402+ const divTest = getByTestId ( "test-div" ) ;
403+ const test = new ElementAssertion ( divTest ) ;
404+
405+ expect ( ( ) => test . toHaveSomeStyle ( { color : "red" , display : "flex" } ) )
406+ . toThrowError ( AssertionError )
407+ // eslint-disable-next-line max-len
408+ . toHaveMessage ( "Expected the element to match some of the following styles:\n{\n \"color\": \"rgb(255, 0, 0)\",\n \"display\": \"flex\"\n}" ) ;
409+
410+ expect ( test . not . toHaveSomeStyle ( { border : "1px solid blue" , color : "red" , display : "flex" } ) ) . toBeEqual ( test ) ;
411+ } ) ;
412+ } ) ;
413+ } ) ;
267414} ) ;
0 commit comments