@@ -6,7 +6,9 @@ const renderComponent = (props) => {
6
6
components : { CAspectRatioBox, CBox } ,
7
7
template : `
8
8
<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>
10
12
</CAspectRatioBox>
11
13
` ,
12
14
...props
@@ -22,35 +24,50 @@ it('should render correctly', () => {
22
24
const [ emotionClassName ] = [ ...screen . getByTestId ( 'aspectRatioBox' ) . classList ]
23
25
const pseudoStyles = getElementStyles ( `.${ emotionClassName } :before` )
24
26
25
- expect ( pseudoStyles ) . toContain ( `
27
+ expect ( pseudoStyles ) . toContain (
28
+ `
26
29
padding-bottom: 100%
27
- ` . trim ( ) )
30
+ ` . trim ( )
31
+ )
28
32
} )
29
33
30
34
it ( 'should have correct styles' , ( ) => {
31
35
const inlineAttrs = ':ratio="2"'
32
36
renderComponent ( { inlineAttrs } )
33
37
const image = screen . getByTestId ( 'image' )
34
38
const aspectRatioBox = screen . getByTestId ( 'aspectRatioBox' )
39
+ const child = screen . getByTestId ( 'child' )
35
40
36
41
const [ emotionClassName ] = [ ...aspectRatioBox . classList ] // second className has the pseudo styles
37
42
const pseudoStyles = getElementStyles ( `.${ emotionClassName } :before` )
38
43
39
- expect ( pseudoStyles ) . toContain ( `
44
+ expect ( pseudoStyles ) . toContain (
45
+ `
40
46
padding-bottom: 50%
41
- ` . trim ( ) )
47
+ ` . trim ( )
48
+ )
42
49
43
50
expect ( aspectRatioBox ) . toHaveStyle ( `
44
51
max-width: 400px;
45
52
position: relative;
46
53
` )
47
54
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 ( `
50
59
position: absolute;
51
60
width: 100%;
52
61
height: 100%;
53
62
top: 0px;
54
63
left: 0px;
55
64
` )
65
+
66
+ expect ( child ) . not . toBeEmpty ( )
67
+
68
+ expect ( image ) . toHaveStyle ( `
69
+ object-fit: cover;
70
+ width: 100%;
71
+ height: 100%;
72
+ ` )
56
73
} )
0 commit comments