Skip to content

Commit

Permalink
Merge pull request #99 from ShellyDCMS/fix-shadow-slot-exception
Browse files Browse the repository at this point in the history
fix exception when trying to get shadow slot parent
  • Loading branch information
ShellyDCMS committed May 29, 2024
2 parents 7bdb48a + 77629a2 commit 250f984
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
10 changes: 4 additions & 6 deletions src/cypress-helper.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,11 @@ describe("cypress helper tests", () => {
});
fetch("https://example.com/api/endpoint");
});

it("should not include a specific item", () => {
then(get.responseBody("apiCall")).shouldNotInclude({
data: {
items: [
{ id: 4, name: "Durian" }
]
items: [{ id: 4, name: "Durian" }]
}
});
});
Expand Down Expand Up @@ -390,8 +388,8 @@ describe("cypress helper tests", () => {
then(get.elementByText("My first paragraph")).shouldExist();
});

it("Non existance slot", () => {
then(get.elementByTestId("non-existance")).shouldNotExist();
it("Non existing slot", () => {
then(get.elementByTestId("non-existing-slot")).shouldNotExist();
});

it("Test non-existing element", () => {
Expand Down
33 changes: 21 additions & 12 deletions src/cypress-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,26 @@ export class CypressHelper {
return checkFunction();
};

private getShadowSlotElement = (dataTestID: string, index: number | undefined) =>
this.get.nthBySelector(dataTestID, index).then(slot =>
cy.wrap(
Cypress.$(slot as JQuery<HTMLSlotElement>)
.get(0)
.assignedNodes()[0].parentElement!
)
);
private getShadowSlotElement = (
dataTestID: string,
index: number | undefined
) =>
this.get.nthBySelector(dataTestID, index).then(slot => {
const shadowSlot = Cypress.$(slot as JQuery<HTMLSlotElement>).get(0);
const shadowSlotParent = this.getShadowSlotParent(shadowSlot);
return cy.wrap(shadowSlotParent ? shadowSlotParent : shadowSlot);
});

private getShadowSlotParent = (shadowSlot: HTMLSlotElement) => {
let shadowSlotParent;
try {
shadowSlotParent = shadowSlot.assignedNodes()[0].parentElement;
} catch {}
return shadowSlotParent;
};
private shouldHandleShadowDomSlot = (dataTestID: string) =>
this.options.handleSlotShadowDOM && dataTestID.endsWith(`-${this.options.shadowSlotSuffix}`)
this.options.handleSlotShadowDOM &&
dataTestID.endsWith(`-${this.options.shadowSlotSuffix}`);

beforeAndAfter = () => {
before(() => {
Expand Down Expand Up @@ -827,14 +836,14 @@ export class CypressHelper {
elementByTestId: (dataTestID: string, index?: number) =>
this.get
.nthBySelector(dataTestID, index)
.should(_ => { })
.should(_ => {})
.then(elements => {
if (elements.length === 0) {
return this.get.nthBySelector(dataTestID, index)
return this.get.nthBySelector(dataTestID, index);
}
return this.shouldHandleShadowDomSlot(dataTestID)
? this.getShadowSlotElement(dataTestID, index)
: this.get.nthBySelector(dataTestID, index)
: this.get.nthBySelector(dataTestID, index);
}),
/**
* Get the element currently focused in the document.
Expand Down

0 comments on commit 250f984

Please sign in to comment.