1+ import '@testing-library/jest-dom' ;
12import { render , screen } from '@testing-library/react' ;
23import Hero from './hero' ;
34
45describe ( 'Hero' , ( ) => {
5- it ( 'Renders all elements: heading, subheading, image' , ( ) => {
6+ it ( 'renders heading, subheading, and image with DS classes ' , ( ) => {
67 const heading = 'heading' ;
78 const subheading = 'subheading' ;
89 const image = '../../assets/images/credit-card-hero.png' ;
@@ -17,69 +18,117 @@ describe('Hero', () => {
1718 /> ,
1819 ) ;
1920
20- expect ( screen . getByText ( heading ) . className ) . toMatch ( / m - h e r o _ _ h e a d i n g / g) ;
21- expect ( screen . getByText ( subheading ) . className ) . toMatch ( / m - h e r o _ _ s u b h e a d / g) ;
22- expect ( screen . getByRole ( 'img' , { name : imageText } ) . className ) . toMatch (
23- / m - h e r o _ _ i m a g e / g,
21+ const headingElement = screen . getByRole ( 'heading' , { level : 1 } ) ;
22+ expect ( headingElement ) . toHaveClass ( 'm-hero__heading' ) ;
23+ expect ( headingElement ) . toHaveTextContent ( heading ) ;
24+
25+ const subheadingElement = screen . getByText ( subheading ) ;
26+ expect ( subheadingElement . tagName ) . toBe ( 'P' ) ;
27+ expect ( subheadingElement ) . toHaveClass ( 'm-hero__subhead' ) ;
28+
29+ expect ( screen . getByRole ( 'img' , { name : imageText } ) ) . toHaveClass (
30+ 'm-hero__image' ,
2431 ) ;
2532 } ) ;
2633
27- it ( 'Derives CSS classes for component variations' , ( ) => {
34+ it ( 'derives CSS classes for component variations' , ( ) => {
2835 const isKnockout = 'm-hero--knockout' ;
2936 const imageIsPhoto = 'm-hero--overlay' ;
3037
3138 render ( < Hero isKnockout data-testid = { isKnockout } /> ) ;
32- expect ( screen . getByTestId ( isKnockout ) . className ) . toMatch ( isKnockout ) ;
39+ expect ( screen . getByTestId ( isKnockout ) ) . toHaveClass ( isKnockout ) ;
3340
34- render ( < Hero imageIsPhoto data-testid = { imageIsPhoto } /> ) ;
35- expect ( screen . getByTestId ( imageIsPhoto ) . className ) . toMatch ( imageIsPhoto ) ;
41+ render (
42+ < Hero
43+ imageIsPhoto
44+ mobileImage = '/mobile-photo.jpg'
45+ data-testid = { imageIsPhoto }
46+ /> ,
47+ ) ;
48+ expect ( screen . getByTestId ( imageIsPhoto ) ) . toHaveClass ( imageIsPhoto ) ;
3649 } ) ;
3750
38- it ( 'Applies direct color settings' , ( ) => {
39- const textColor = 'orange' ;
40- const backgroundColor = 'purple' ;
41- const wrapperSelector = 'wrapper' ;
42- const textId = 'hero-text' ;
51+ it ( 'applies background color on the section for standard heroes' , ( ) => {
52+ render (
53+ < Hero backgroundColor = '#800080' heading = 'test' data-testid = 'hero' /> ,
54+ ) ;
55+
56+ expect ( screen . getByTestId ( 'hero' ) ) . toHaveStyle ( {
57+ backgroundColor : 'rgb(128, 0, 128)' ,
58+ } ) ;
59+ expect ( screen . getByTestId ( 'hero-wrapper' ) ) . not . toHaveAttribute ( 'style' ) ;
60+ } ) ;
4361
62+ it ( 'applies background color on the section for knockout heroes' , ( ) => {
4463 render (
4564 < Hero
46- data-testid = 'wrapper'
47- textColor = { textColor }
48- backgroundColor = { backgroundColor }
65+ isKnockout
66+ backgroundColor = '#207676'
4967 heading = 'test'
68+ data-testid = 'hero'
5069 /> ,
5170 ) ;
5271
53- // Background color
54- const wrapper = screen . getByTestId ( wrapperSelector ) ;
55- expect ( wrapper . style . backgroundColor ) . toMatch ( backgroundColor ) ;
72+ expect ( screen . getByTestId ( 'hero' ) ) . toHaveStyle ( {
73+ backgroundColor : '#207676' ,
74+ } ) ;
75+ } ) ;
5676
57- // Text color
58- const text = screen . getByTestId ( textId ) ;
59- expect ( text . style . color ) . toMatch ( textColor ) ;
77+ it ( 'does not set inline text color (knockout uses DS styles)' , ( ) => {
78+ render (
79+ < Hero isKnockout heading = 'test' subheading = 'sub' data-testid = 'hero' /> ,
80+ ) ;
81+
82+ expect ( screen . getByTestId ( 'hero-text' ) ) . not . toHaveAttribute ( 'style' ) ;
6083 } ) ;
6184
62- it ( 'Applies heading levels' , ( ) => {
63- const headingLevel = 2 ;
64- const headingText = 'Heading text' ;
65- const subheadingLevel = 3 ;
66- const subheadingText = 'Subheading text' ;
85+ it ( 'uses the mobile image in the image slot for photo heroes' , ( ) => {
86+ render (
87+ < Hero
88+ imageIsPhoto
89+ image = '/desktop-photo.png'
90+ mobileImage = '/mobile-photo.jpg'
91+ imageAltText = 'photo hero'
92+ /> ,
93+ ) ;
94+
95+ const heroImage = screen . getByRole ( 'img' , { name : 'photo hero' } ) ;
96+ expect ( heroImage . style . getPropertyValue ( '--m-hero-image' ) ) . toBe (
97+ 'url("/desktop-photo.png")' ,
98+ ) ;
99+ expect ( heroImage . style . getPropertyValue ( '--m-hero-mobile-image' ) ) . toBe (
100+ 'url("/mobile-photo.jpg")' ,
101+ ) ;
102+ } ) ;
67103
104+ it ( 'supports a mobile image in the image slot for non-photo heroes' , ( ) => {
68105 render (
69106 < Hero
70- heading = 'Heading text'
71- headingLevel = { `h${ headingLevel } ` }
72- subheading = 'Subheading text'
73- subheadingLevel = { `h${ subheadingLevel } ` }
107+ image = '/illustration.png'
108+ mobileImage = '/mobile-photo.jpg'
109+ imageAltText = 'illustration hero'
74110 /> ,
75111 ) ;
76112
77- // Heading
78- const header = screen . getByRole ( 'heading' , { level : headingLevel } ) ;
79- expect ( header ) . toHaveProperty ( 'textContent' , headingText ) ;
113+ const heroImage = screen . getByRole ( 'img' , { name : 'illustration hero' } ) ;
114+ expect ( heroImage . style . getPropertyValue ( '--m-hero-image' ) ) . toBe (
115+ 'url("/illustration.png")' ,
116+ ) ;
117+ expect ( heroImage . style . getPropertyValue ( '--m-hero-mobile-image' ) ) . toBe (
118+ 'url("/mobile-photo.jpg")' ,
119+ ) ;
120+ } ) ;
80121
81- // Subheading
82- const subheading = screen . getByRole ( 'heading' , { level : subheadingLevel } ) ;
83- expect ( subheading ) . toHaveProperty ( 'textContent' , subheadingText ) ;
122+ it ( 'requires a mobile image for photo heroes' , ( ) => {
123+ expect ( ( ) =>
124+ render (
125+ // @ts -expect-error mobileImage is required when imageIsPhoto is true.
126+ < Hero
127+ imageIsPhoto
128+ image = '/desktop-photo.png'
129+ imageAltText = 'photo hero'
130+ /> ,
131+ ) ,
132+ ) . toThrow ( 'Hero requires mobileImage when imageIsPhoto is true.' ) ;
84133 } ) ;
85134} ) ;
0 commit comments