Skip to content

Commit

Permalink
test(image): image with new src should be re-lazyloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Apr 4, 2022
1 parent 96ae758 commit f3443bd
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/integration/image-component/default/pages/lazy-src-change.js
@@ -0,0 +1,21 @@
import React from 'react'
import Image from 'next/image'

const Page = () => {
const [src, setSrc] = React.useState('/test.jpg')
return (
<div>
<p>Home Page</p>
<div id="spacer" style={{ height: '150vh' }} />
<Image id="basic-image" src={src} width="400" height="400"></Image>
<button
id="button-change-image-src"
onClick={() => setSrc('/test.jpg?new')}
>
Change Image
</button>
</div>
)
}

export default Page
81 changes: 81 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Expand Up @@ -1173,6 +1173,87 @@ function runTests(mode) {
}
})

it('should re-lazyload images after src changes', async () => {
let browser
try {
browser = await webdriver(appPort, '/lazy-src-change')

// image should not be loaded as it is out of viewport
await check(async () => {
const result = await browser.eval(
`document.getElementById('basic-image').naturalWidth`
)

if (result >= 400) {
throw new Error('Incorrectly loaded image')
}

return 'result-correct'
}, /result-correct/)

// Move image into viewport
await browser.eval(
'document.getElementById("spacer").style.display = "none"'
)

// image should be loaded by now
await check(async () => {
const result = await browser.eval(
`document.getElementById('basic-image').naturalWidth`
)

if (result < 400) {
throw new Error('Incorrectly loaded image')
}

return 'result-correct'
}, /result-correct/)

// Make image out of viewport again
await browser.eval(
'document.getElementById("spacer").style.display = "block"'
)
// Toggle image's src
await browser.eval(
'document.getElementById("button-change-image-src").click()'
)

// "new" image should be lazy loaded
await check(async () => {
const result = await browser.eval(
`document.getElementById('basic-image').naturalWidth`
)

if (result >= 400) {
throw new Error('Incorrectly loaded image')
}

return 'result-correct'
}, /result-correct/)

// Move image into viewport again
await browser.eval(
'document.getElementById("spacer").style.display = "none"'
)
// "new" image should be loaded by now
await check(async () => {
const result = await browser.eval(
`document.getElementById('basic-image').naturalWidth`
)

if (result >= 400) {
throw new Error('Incorrectly loaded image')
}

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

it('should initially load only two of four images using lazyroot', async () => {
let browser
try {
Expand Down

0 comments on commit f3443bd

Please sign in to comment.