1- import { Paragraph , H2 , H3 } from './Text' ;
1+ import { Paragraph , H2 , H3 , Heading } from './Text' ;
22import renderer from 'react-test-renderer' ;
3+ import { render } from '@testing-library/react' ;
4+ import React from 'react' ;
35
46describe ( 'Text' , ( ) => {
57 it ( 'matches body snapshot' , ( ) => {
@@ -24,3 +26,41 @@ describe('Text', () => {
2426 } ) ;
2527
2628} ) ;
29+
30+ describe ( 'Heading' , ( ) => {
31+ it ( 'defaults variant to match the semantic level' , ( ) => {
32+ const tree = renderer . create (
33+ < Heading level = { 2 } > This is a heading</ Heading >
34+ ) . toJSON ( ) ;
35+ expect ( tree ) . toMatchObject ( { type : 'h2' , props : { className : 'text-h2' } } ) ;
36+ } ) ;
37+
38+ it ( 'renders the element for the given level' , ( ) => {
39+ const tree = renderer . create (
40+ < Heading level = { 4 } > This is a heading</ Heading >
41+ ) . toJSON ( ) ;
42+ expect ( ( tree as unknown as { type : string } ) . type ) . toBe ( 'h4' ) ;
43+ } ) ;
44+
45+ it ( 'decouples visual style from semantic level' , ( ) => {
46+ // an <h2> element styled like an h3
47+ const tree = renderer . create (
48+ < Heading level = { 2 } variant = "h3" > This is a heading</ Heading >
49+ ) . toJSON ( ) ;
50+ expect ( tree ) . toMatchObject ( { type : 'h2' , props : { className : 'text-h3' } } ) ;
51+ } ) ;
52+
53+ it ( 'merges a consumer className with the variant class' , ( ) => {
54+ const tree = renderer . create (
55+ < Heading level = { 2 } className = "custom" > This is a heading</ Heading >
56+ ) . toJSON ( ) ;
57+ expect ( ( tree as unknown as { props : { className : string } } ) . props . className ) . toBe ( 'text-h2 custom' ) ;
58+ } ) ;
59+
60+ it ( 'forwards a ref to the underlying element' , ( ) => {
61+ const ref = React . createRef < HTMLHeadingElement > ( ) ;
62+ render ( < Heading level = { 2 } ref = { ref } > This is a heading</ Heading > ) ;
63+ expect ( ref . current ) . not . toBeNull ( ) ;
64+ expect ( ref . current ?. tagName ) . toBe ( 'H2' ) ;
65+ } ) ;
66+ } ) ;
0 commit comments