-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from trurl-master/visibility-checks
Add isSubtreeInaccessible pass-through
- Loading branch information
Showing
11 changed files
with
217 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { render } from '@testing-library/react'; | ||
import { byRole } from '../helpers/by-role'; | ||
import { configToolkit } from '../config'; | ||
|
||
describe('inaccessible', () => { | ||
it('skips subtrees with hidden property', () => { | ||
const { container } = render( | ||
<div> | ||
<div hidden> | ||
<p>Hidden paragraph</p> | ||
</div> | ||
<p>Visible paragraph</p> | ||
</div> | ||
); | ||
|
||
expect(container.firstChild).toHaveA11yTree( | ||
byRole('generic', [byRole('paragraph', ['Visible paragraph'])]) | ||
); | ||
}); | ||
|
||
it('skips subtrees with aria-hidden property', () => { | ||
const { container } = render( | ||
<div> | ||
<div aria-hidden> | ||
<p>Hidden paragraph</p> | ||
</div> | ||
<p>Visible paragraph</p> | ||
</div> | ||
); | ||
|
||
expect(container.firstChild).toHaveA11yTree( | ||
byRole('generic', [byRole('paragraph', ['Visible paragraph'])]) | ||
); | ||
}); | ||
|
||
it("doesn't skipp subtrees with aria-hidden property set to false", () => { | ||
const { container } = render( | ||
<div> | ||
<div aria-hidden="false"> | ||
<p>Not hidden paragraph</p> | ||
</div> | ||
<p>Visible paragraph</p> | ||
</div> | ||
); | ||
|
||
expect(container.firstChild).toHaveA11yTree( | ||
byRole('generic', [ | ||
byRole('paragraph', ['Not hidden paragraph']), | ||
byRole('paragraph', ['Visible paragraph']), | ||
]) | ||
); | ||
}); | ||
|
||
it('skips subtrees with visibility: hidden', () => { | ||
const { container } = render( | ||
<div> | ||
<div style={{ visibility: 'hidden' }}> | ||
<p>Invisible paragraph</p> | ||
</div> | ||
<p>Visible paragraph</p> | ||
</div> | ||
); | ||
|
||
expect(container.firstChild).toHaveA11yTree( | ||
byRole('generic', [byRole('paragraph', ['Visible paragraph'])]) | ||
); | ||
}); | ||
|
||
it('skips subtrees with display: none', () => { | ||
const { container } = render( | ||
<div> | ||
<div style={{ display: 'none' }}> | ||
<p>Hidden paragraph</p> | ||
</div> | ||
<p>Visible paragraph</p> | ||
</div> | ||
); | ||
|
||
expect(container.firstChild).toHaveA11yTree( | ||
byRole('generic', [byRole('paragraph', ['Visible paragraph'])]) | ||
); | ||
}); | ||
|
||
it('skips subtrees with custom isSubtreeInaccessible function', () => { | ||
const { container } = render( | ||
<div> | ||
<div className="hidden"> | ||
<p>Hidden paragraph</p> | ||
</div> | ||
<div className="invisible"> | ||
<p>Invisible paragraph</p> | ||
</div> | ||
<p>Visible paragraph</p> | ||
</div> | ||
); | ||
|
||
expect(container.firstChild).toHaveA11yTree( | ||
byRole('generic', [byRole('paragraph', ['Visible paragraph'])]), | ||
{ | ||
isInaccessibleOptions: { | ||
isSubtreeInaccessible: (element) => | ||
// tailwindcss classes: hidden, invisible | ||
element.classList.contains('hidden') || | ||
element.classList.contains('invisible'), | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it('skips subtrees with custom isSubtreeInaccessible function set globally', () => { | ||
configToolkit({ | ||
isInaccessibleOptions: { | ||
isSubtreeInaccessible: (element) => | ||
// tailwindcss classes: hidden, invisible | ||
element.classList.contains('hidden') || | ||
element.classList.contains('invisible'), | ||
}, | ||
}); | ||
|
||
const { container } = render( | ||
<div> | ||
<div className="hidden"> | ||
<p>Hidden paragraph</p> | ||
</div> | ||
<div className="invisible"> | ||
<p>Invisible paragraph</p> | ||
</div> | ||
<p>Visible paragraph</p> | ||
</div> | ||
); | ||
|
||
expect(container.firstChild).toHaveA11yTree( | ||
byRole('generic', [byRole('paragraph', ['Visible paragraph'])]) | ||
); | ||
|
||
configToolkit({ | ||
isInaccessibleOptions: { | ||
isSubtreeInaccessible: undefined, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { IsInaccessibleOptions } from 'dom-accessibility-api'; | ||
|
||
type Config = { | ||
isInaccessibleOptions?: IsInaccessibleOptions; | ||
}; | ||
|
||
const config: Config = { | ||
isInaccessibleOptions: undefined, | ||
}; | ||
|
||
export const getConfig = (): typeof config => config; | ||
export const configToolkit = (options: Partial<Config>): void => { | ||
Object.assign(config, options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters