From 7c80e01a1d37185933daa49885c966a452dd7b3b Mon Sep 17 00:00:00 2001 From: Kilian McMahon Date: Fri, 30 Jul 2021 12:12:01 +0200 Subject: [PATCH 1/2] Add test case and implementation for finding roles in shadowRoot --- src/__tests__/helpers/CustomButton.js | 12 ++++++++++++ src/__tests__/role.js | 28 +++++++++++++++++++++++++++ src/role-helpers.js | 7 +++---- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/__tests__/helpers/CustomButton.js diff --git a/src/__tests__/helpers/CustomButton.js b/src/__tests__/helpers/CustomButton.js new file mode 100644 index 00000000..8ff69ac2 --- /dev/null +++ b/src/__tests__/helpers/CustomButton.js @@ -0,0 +1,12 @@ +export class CustomButton extends HTMLElement { + constructor() { + super() + + this.attachShadow({mode: 'open'}) + + const button = document.createElement('button') + button.innerHTML = 'Button text' + + this.shadowRoot.append(button) + } +} diff --git a/src/__tests__/role.js b/src/__tests__/role.js index c7dc70c5..15e64d5a 100644 --- a/src/__tests__/role.js +++ b/src/__tests__/role.js @@ -1,6 +1,7 @@ import {configure, getConfig} from '../config' import {getQueriesForElement} from '../get-queries-for-element' import {render, renderIntoDocument} from './helpers/test-utils' +import {CustomButton} from './helpers/CustomButton' test('by default logs accessible roles when it fails', () => { const {getByRole} = render(`

Hi

`) @@ -572,3 +573,30 @@ test('should find the input using type property instead of attribute', () => { const {getByRole} = render('') expect(getByRole('textbox')).not.toBeNull() }) + +describe('Web Component Custom Elements', () => { + beforeAll(() => { + customElements.define('custom-button', CustomButton) + }) + + test('should find accessible roles in elements that contain a shadowRoot', () => { + const {getByRole} = render(``) + expect(() => getByRole('article')).toThrowErrorMatchingInlineSnapshot(` +"Unable to find an accessible element with the role "article" + +Here are the accessible roles: + + button: + + Name "Button text": +