@@ -6,7 +6,9 @@ const renderComponent = (props) => {
66 components : { CAspectRatioBox, CBox } ,
77 template : `
88 <CAspectRatioBox maxW="400px" data-testid="aspectRatioBox" ${ inlineAttrs } >
9- <CBox as="img" src="https://bit.ly/naruto-sage" alt="naruto" objectFit="cover" data-testid="image" />
9+ <CBox data-testid="child">
10+ <CBox as="img" src="https://bit.ly/naruto-sage" alt="naruto" objectFit="cover" h="100%" w="100%" data-testid="image" />
11+ </CBox>
1012 </CAspectRatioBox>
1113 ` ,
1214 ...props
@@ -22,35 +24,50 @@ it('should render correctly', () => {
2224 const [ emotionClassName ] = [ ...screen . getByTestId ( 'aspectRatioBox' ) . classList ]
2325 const pseudoStyles = getElementStyles ( `.${ emotionClassName } :before` )
2426
25- expect ( pseudoStyles ) . toContain ( `
27+ expect ( pseudoStyles ) . toContain (
28+ `
2629 padding-bottom: 100%
27- ` . trim ( ) )
30+ ` . trim ( )
31+ )
2832} )
2933
3034it ( 'should have correct styles' , ( ) => {
3135 const inlineAttrs = ':ratio="2"'
3236 renderComponent ( { inlineAttrs } )
3337 const image = screen . getByTestId ( 'image' )
3438 const aspectRatioBox = screen . getByTestId ( 'aspectRatioBox' )
39+ const child = screen . getByTestId ( 'child' )
3540
3641 const [ emotionClassName ] = [ ...aspectRatioBox . classList ] // second className has the pseudo styles
3742 const pseudoStyles = getElementStyles ( `.${ emotionClassName } :before` )
3843
39- expect ( pseudoStyles ) . toContain ( `
44+ expect ( pseudoStyles ) . toContain (
45+ `
4046 padding-bottom: 50%
41- ` . trim ( ) )
47+ ` . trim ( )
48+ )
4249
4350 expect ( aspectRatioBox ) . toHaveStyle ( `
4451 max-width: 400px;
4552 position: relative;
4653 ` )
4754
48- expect ( image ) . toHaveStyle ( `
49- object-fit: cover;
55+ // aspectRatioBox should contain exactly 1 immediate child
56+ expect ( aspectRatioBox . childElementCount ) . toEqual ( 1 )
57+
58+ expect ( child ) . toHaveStyle ( `
5059 position: absolute;
5160 width: 100%;
5261 height: 100%;
5362 top: 0px;
5463 left: 0px;
5564 ` )
65+
66+ expect ( child ) . not . toBeEmpty ( )
67+
68+ expect ( image ) . toHaveStyle ( `
69+ object-fit: cover;
70+ width: 100%;
71+ height: 100%;
72+ ` )
5673} )
0 commit comments