Skip to content

Commit

Permalink
Add tests and solve another edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Oct 22, 2020
1 parent ef79ffc commit 6c11e4f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
43 changes: 24 additions & 19 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,18 @@ export default function Image({

let divStyle: React.CSSProperties | undefined
let imgStyle: React.CSSProperties | undefined
let wrapperStyle: React.CSSProperties | undefined
if (typeof height === 'number' && typeof width === 'number' && !unsized) {
// <Image src="i.png" width=100 height=100 />
const quotient = height / width
const ratio = isNaN(quotient) ? 1 : quotient * 100
wrapperStyle = {
maxWidth: '100%',
width,
}
divStyle = {
position: 'relative',
paddingBottom: `${ratio}%`,
maxWidth: '100%',
width,
}
imgStyle = {
height: '100%',
Expand Down Expand Up @@ -264,23 +267,25 @@ export default function Image({
}

return (
<div style={divStyle}>
{shouldPreload
? generatePreload({
src,
widths: configSizes,
unoptimized,
sizes,
})
: ''}
<img
{...rest}
{...imgAttributes}
className={className}
sizes={sizes}
ref={thisEl}
style={imgStyle}
/>
<div style={wrapperStyle}>
<div style={divStyle}>
{shouldPreload
? generatePreload({
src,
widths: configSizes,
unoptimized,
sizes,
})
: ''}
<img
{...rest}
{...imgAttributes}
className={className}
sizes={sizes}
ref={thisEl}
style={imgStyle}
/>
</div>
</div>
)
}
Expand Down
19 changes: 19 additions & 0 deletions test/integration/image-component/default/pages/flex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react'
import Image from 'next/image'

const Page = () => {
return (
<div>
<p>Hello World</p>
<Image id="basic-image" src="/test.jpg" width={400} height={400}></Image>
<p id="stubtext">This is the index page</p>
<style jsx>{`
div {
display: flex;
}
`}</style>
</div>
)
}

export default Page
21 changes: 21 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ function runTests() {
}
}
})

it('should work when using flexbox', async () => {
let browser
try {
browser = await webdriver(appPort, '/flex')
await check(async () => {
const result = await browser.eval(
`document.getElementById('basic-image').width`
)
if (result === 0) {
throw new Error('Incorrectly loaded image')
}

return 'result-correct'
}, /result-correct/)
} finally {
if (browser) {
await browser.close()
}
}
})
}

describe('Image Component Tests', () => {
Expand Down

0 comments on commit 6c11e4f

Please sign in to comment.