Skip to content
Open
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
4 changes: 3 additions & 1 deletion packages/dom/src/lib/ElementAssertion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Assertion, AssertionError } from "@assertive-ts/core";
import equal from "fast-deep-equal";

import { getExpectedAndReceivedStyles, getAccessibleDescription, isElementEmpty } from "./helpers/helpers";
import { getAccessibleDescription } from "./helpers/accessibility";
import { isElementEmpty } from "./helpers/dom";
import { getExpectedAndReceivedStyles } from "./helpers/styles";

export class ElementAssertion<T extends Element> extends Assertion<T> {

Expand Down
30 changes: 30 additions & 0 deletions packages/dom/src/lib/helpers/accessibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function normalizeText(text: string): string {
return text.replace(/\s+/g, " ").trim();
}

export function getAccessibleDescription(actual: Element): string {
const ariaDescribedBy = actual.getAttribute("aria-describedby");

if (!ariaDescribedBy) {
return "";
}

const descriptionIds = ariaDescribedBy.split(/\s+/).filter(Boolean);

const getElementText = (id: string): string | null => {
const element = actual.ownerDocument.getElementById(id);

if (!element || !element.textContent) {
return null;
}

return element.textContent;
};

const combinedText = descriptionIds
.map(getElementText)
.filter((text): text is string => text !== null)
.join(" ");

return normalizeText(combinedText);
}
6 changes: 6 additions & 0 deletions packages/dom/src/lib/helpers/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const COMMENT_NODE_TYPE = 8;

export function isElementEmpty (element: Element): boolean {
const nonCommentChildNodes = [...element.childNodes].filter(child => child.nodeType !== COMMENT_NODE_TYPE);
return nonCommentChildNodes.length === 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ interface StyleDeclaration extends Record<string, string> {
value: string;
}

const COMMENT_NODE_TYPE = 8;

function normalizeStyles(css: Partial<CSSStyleDeclaration>): StyleDeclaration {
const normalizer = document.createElement("div");
document.body.appendChild(normalizer);
Expand Down Expand Up @@ -75,39 +73,3 @@ export function getExpectedAndReceivedStyles
elementProcessedStyle,
];
}

export function isElementEmpty (element: Element): boolean {
const nonCommentChildNodes = [...element.childNodes].filter(child => child.nodeType !== COMMENT_NODE_TYPE);
return nonCommentChildNodes.length === 0;
}

function normalizeText(text: string): string {
return text.replace(/\s+/g, " ").trim();
}

export function getAccessibleDescription(actual: Element): string {
const ariaDescribedBy = actual.getAttribute("aria-describedby");

if (!ariaDescribedBy) {
return "";
}

const descriptionIds = ariaDescribedBy.split(/\s+/).filter(Boolean);

const getElementText = (id: string): string | null => {
const element = actual.ownerDocument.getElementById(id);

if (!element || !element.textContent) {
return null;
}

return element.textContent;
};

const combinedText = descriptionIds
.map(getElementText)
.filter((text): text is string => text !== null)
.join(" ");

return normalizeText(combinedText);
}
Loading