Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose close functionality #697

Merged
merged 4 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createElement, useEffect } from 'react'
import React, { createElement, useEffect, useRef } from 'react'
import { render } from '@testing-library/react'

import { Disclosure } from './disclosure'
Expand Down Expand Up @@ -115,6 +115,127 @@ describe('Rendering', () => {

assertDisclosureButton({ state: DisclosureState.InvisibleUnmounted })
})

it(
'should expose a close function that closes the disclosure',
suppressConsoleLogs(async () => {
render(
<Disclosure>
{({ close }) => (
<>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
<button onClick={() => close()}>Close me</button>
</Disclosure.Panel>
</>
)}
</Disclosure>
)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the Disclosure.Button got the restored focus
assertActiveElement(getByText('Trigger'))
})
)

it(
'should expose a close function that closes the disclosure and restores to a specific element',
suppressConsoleLogs(async () => {
render(
<>
<button id="test">restoreable</button>
<Disclosure>
{({ close }) => (
<>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
<button onClick={() => close(document.getElementById('test')!)}>
Close me
</button>
</Disclosure.Panel>
</>
)}
</Disclosure>
</>
)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the restoreable button got the restored focus
assertActiveElement(getByText('restoreable'))
})
)

it(
'should expose a close function that closes the disclosure and restores to a ref',
suppressConsoleLogs(async () => {
function Example() {
let elementRef = useRef(null)
return (
<>
<button ref={elementRef}>restoreable</button>
<Disclosure>
{({ close }) => (
<>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
<button onClick={() => close(elementRef)}>Close me</button>
</Disclosure.Panel>
</>
)}
</Disclosure>
</>
)
}

render(<Example />)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the restoreable button got the restored focus
assertActiveElement(getByText('restoreable'))
})
)
})

describe('Disclosure.Button', () => {
Expand Down Expand Up @@ -242,6 +363,115 @@ describe('Rendering', () => {
assertDisclosureButton({ state: DisclosureState.InvisibleHidden })
assertDisclosurePanel({ state: DisclosureState.InvisibleHidden })
})

it(
'should expose a close function that closes the disclosure',
suppressConsoleLogs(async () => {
render(
<Disclosure>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
{({ close }) => <button onClick={() => close()}>Close me</button>}
</Disclosure.Panel>
</Disclosure>
)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the Disclosure.Button got the restored focus
assertActiveElement(getByText('Trigger'))
})
)

it(
'should expose a close function that closes the disclosure and restores to a specific element',
suppressConsoleLogs(async () => {
render(
<>
<button id="test">restoreable</button>
<Disclosure>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
{({ close }) => (
<button onClick={() => close(document.getElementById('test')!)}>Close me</button>
)}
</Disclosure.Panel>
</Disclosure>
</>
)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the restoreable button got the restored focus
assertActiveElement(getByText('restoreable'))
})
)

it(
'should expose a close function that closes the disclosure and restores to a ref',
suppressConsoleLogs(async () => {
function Example() {
let elementRef = useRef(null)
return (
<>
<button ref={elementRef}>restoreable</button>
<Disclosure>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
{({ close }) => <button onClick={() => close(elementRef)}>Close me</button>}
</Disclosure.Panel>
</Disclosure>
</>
)
}

render(<Example />)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the restoreable button got the restored focus
assertActiveElement(getByText('restoreable'))
})
)
})
})

Expand Down
Loading