Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
fix(react): fix jsdom react testing issues
Browse files Browse the repository at this point in the history
Fixes #5985
- Add isNode check to property syncing in cdscontrol.

Fixes #6525:
- Initialize role properties in connectedCallback.
- Remove isNode check for aria-reflect. Fixes

Signed-off-by: Ashley Ryan <[email protected]>
  • Loading branch information
Ashley Ryan committed Jan 31, 2022
1 parent e560047 commit d9537b6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/breadcrumb/breadcrumb.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export class CdsBreadcrumb extends LitElement {

@querySlot('[slot="cds-separator"]') private customSeparator: HTMLElement;

role = 'navigation';
connectedCallback() {
super.connectedCallback();
this.role = 'navigation';
}

render() {
return html`
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/card/card.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ import styles from './card.element.scss';
* @cssprop --cds-card-remove-margin
*/
export class CdsCard extends CdsInternalPanel {
role = 'region';

@globalStyle() globalStyles = css`
[cds-card-remove-margin] {
margin-left: calc(-1 * var(--card-remove-margin));
Expand All @@ -61,4 +59,9 @@ export class CdsCard extends CdsInternalPanel {
static get styles() {
return [...super.styles, styles];
}

connectedCallback() {
super.connectedCallback();
this.role = 'region';
}
}
8 changes: 6 additions & 2 deletions packages/core/src/internal/utils/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
* Copyright (c) 2016-2022 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
Expand All @@ -9,6 +9,10 @@ export function stopEvent(event: Event) {
event.stopPropagation();
}

function isNode() {
return (globalThis as any)?.process?.env?.JEST_WORKER_ID !== undefined; // Jest and JSDom breaks defining a new property
}

export const getElementUpdates = (
element: HTMLElement,
propertyKey: string,
Expand All @@ -29,7 +33,7 @@ export const getElementUpdates = (

const updatedProp = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(element), propertyKey) as any;

if (updatedProp) {
if (updatedProp && !isNode()) {
Object.defineProperty(element, propertyKey, {
get: updatedProp.get,
set: val => {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/navigation/navigation-item.element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
* Copyright (c) 2016-2022 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
Expand Down Expand Up @@ -71,10 +71,9 @@ export class CdsNavigationItem extends LitElement implements FocusableItem {
@querySlotAll('[cds-navigation-sr-text]')
itemText: NodeListOf<HTMLSpanElement>;

role = 'listitem';

connectedCallback() {
super.connectedCallback();
this.role = 'listitem';
if (!this.id) {
this.id = createId();
}
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/navigation/navigation.element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
* Copyright (c) 2016-2022 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
Expand Down Expand Up @@ -173,8 +173,6 @@ export class CdsNavigation extends LitElement {
@querySlotAll('cds-navigation-group')
protected navigationGroupRefs: NodeListOf<CdsNavigationGroup>;

role = 'list';

private toggle() {
this.expandedChange.emit(!this.expanded);
}
Expand Down Expand Up @@ -364,6 +362,7 @@ export class CdsNavigation extends LitElement {

connectedCallback() {
super.connectedCallback();
this.role = 'list';
this.rootNavigationStart?.addEventListener('focus', this.focusRootStart.bind(this));
this.rootNavigationStart?.addEventListener('blur', this.blurRootStart.bind(this));
this.rootNavigationStart?.addEventListener('keydown', this.handleRootStartKeys.bind(this));
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/polyfills/aria-reflect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
* Copyright (c) 2016-2022 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
Expand Down Expand Up @@ -59,19 +59,15 @@ declare global {
let roleRegistered = false;
let ariaRegistered = false;

function isNode() {
return (globalThis as any)?.process?.env?.JEST_WORKER_ID !== undefined; // Jest and JSDom breaks on property reflection
}

// eslint-disable-next-line
if (!roleRegistered && !Element.prototype.hasOwnProperty('role') && !isNode()) {
if (!roleRegistered && !Element.prototype.hasOwnProperty('role')) {
reflect(Element.prototype, 'role', 'role');
roleRegistered = true;
}

// https://www.w3.org/TR/wai-aria-1.0/states_and_properties
// eslint-disable-next-line
if (!ariaRegistered && !Element.prototype.hasOwnProperty('ariaLabel') && !isNode()) {
if (!ariaRegistered && !Element.prototype.hasOwnProperty('ariaLabel')) {
ariaRegistered = true;
[
'ActiveDescendant',
Expand Down

0 comments on commit d9537b6

Please sign in to comment.