Skip to content

Commit

Permalink
test: add ref forwarding test
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasin committed Apr 5, 2021
1 parent 9d49d15 commit 5859bb7
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/react-div-100vh/src/jsdom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @jest-environment jsdom
*/

import React, { useState } from 'react'
import React, { useRef, useState } from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
import Div100vh from '.'
import { act } from 'react-dom/test-utils'
Expand Down Expand Up @@ -59,4 +59,37 @@ describe('Div100vh component', () => {
)
expect(resizeListenerUnmountCalls.length).toBe(1)
})

it('forwards ref', () => {
let focus: () => void
const TestApp = () => {
const ref = useRef<HTMLDivElement>(null)
focus = () => {
ref.current?.focus()
}
return (
<Div100vh
ref={ref}
// so we look up the target div by different means (not via the ref)
data-test
// https://github.com/jsdom/jsdom/issues/2586#issuecomment-561871527
tabIndex={1}
>
hello
</Div100vh>
)
}
act(() => {
render(<TestApp />, container)
})

const divElement = container?.querySelector('[data-test]')
expect(document.activeElement === divElement).toBe(false)

act(() => {
focus()
})

expect(document.activeElement === divElement).toBe(true)
})
})

1 comment on commit 5859bb7

@vercel
Copy link

@vercel vercel bot commented on 5859bb7 Apr 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.