Skip to content

Commit

Permalink
test: callback ref cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce committed Jul 25, 2024
1 parent 94b99d2 commit 3b10ad9
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/integration/link-ref/pages/child-ref-func-cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react'
import Link from 'next/link'
import { useCallback, useRef, useEffect, useState } from 'react'
import { flushSync } from 'react-dom'

export default function Page() {
const [isVisible, setIsVisible] = useState(true)

const statusRef = useRef({ wasInitialized: false, wasCleanedUp: false })

const refWithCleanup = useCallback((el) => {
if (!el) {
console.error(
'callback refs that returned a cleanup should never be called with null'
)
return
}

statusRef.current.wasInitialized = true
return () => {
statusRef.current.wasCleanedUp = true
}
}, [])

useEffect(() => {
const timeout = setTimeout(
() => {
flushSync(() => {
setIsVisible(false)
})
if (!statusRef.current.wasInitialized) {
console.error('callback ref was not initialized')
}
if (!statusRef.current.wasCleanedUp) {
console.error('callback ref was not cleaned up')
}
},
100 // if we hide the Link too quickly, the prefetch won't fire, failing a test
)
return () => clearTimeout(timeout)
}, [])

if (!isVisible) {
return null
}

return (
<Link href="/" ref={refWithCleanup}>
Click me
</Link>
)
}
8 changes: 8 additions & 0 deletions test/integration/link-ref/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ describe('Invalid hrefs', () => {
it('should handle child ref that is a function', async () => {
await noError('/child-ref-func')
})

it('should handle child ref that is a function that returns a cleanup function', async () => {
await noError('/child-ref-func-cleanup')
})
}
)
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
Expand All @@ -109,6 +113,10 @@ describe('Invalid hrefs', () => {
it('should preload with child ref with function', async () => {
await didPrefetch('/child-ref-func')
})

it('should preload with child ref with function that returns a cleanup function', async () => {
await didPrefetch('/child-ref-func-cleanup')
})
}
)
})
43 changes: 43 additions & 0 deletions test/integration/next-image-new/app-dir/app/ref-cleanup/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use client'
import Image from 'next/image'
import { useCallback, useEffect, useState } from 'react'

export default function Home() {
const [displayImage, setDisplayImage] = useState(true)

const refWithCleanup = useCallback((el) => {
if (!el) {
throw new Error(
'callback refs that returned a cleanup should never be called with null'
)
}

return () => {
console.log('callback ref was cleaned up')
}
}, [])

useEffect(() => {
setDisplayImage(false)
}, [])

return (
<main>
<h1>Should call ref cleanup on unmount</h1>
<section>
{displayImage ? (
<div style={{ position: 'relative', width: 10, height: 10 }}>
<Image
ref={refWithCleanup}
priority
fill
src="/test.jpg"
alt="alt"
sizes="10px"
/>
</div>
) : null}
</section>
</main>
)
}
14 changes: 14 additions & 0 deletions test/integration/next-image-new/app-dir/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
nextBuild,
nextStart,
renderViaHTTP,
retry,
waitFor,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
Expand Down Expand Up @@ -1574,6 +1575,19 @@ function runTests(mode) {
}
}
})

it('should call callback ref cleanups when unmounting', async () => {
const browser = await webdriver(appPort, '/ref-cleanup')
const getLogs = async () => (await browser.log()).map((log) => log.message)

await retry(async () => {
expect(await getLogs()).toContain('callback ref was cleaned up')
})

expect(await getLogs()).not.toContain(
'callback refs that returned a cleanup should never be called with null'
)
})
}

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

0 comments on commit 3b10ad9

Please sign in to comment.