Skip to content

Commit

Permalink
Fix a bug in buildAsyncSelector, if a Promise was sent, document
Browse files Browse the repository at this point in the history
…was used (#11)
  • Loading branch information
elchininet authored Dec 4, 2023
1 parent dce2361 commit da0e04f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.0.4] - 2023-12-04

- Fix a bug in `buildAsyncSelector`, if a Promise was sent, `document` was used

## [2.0.3] - 2023-12-04

- Allow `buildAsyncSelector` method to receive a Promise
Expand Down
37 changes: 37 additions & 0 deletions cypress/e2e/async-selector.spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,27 @@ describe('ShadowDomSelector buildAsyncSelector class spec', () => {
cy.window()
.then(async (win) => {

const doc = win.document;
const buildAsyncSelector = win.ShadowDomSelector.buildAsyncSelector;

const selector = buildAsyncSelector({
retries: 100
});

const selectorFromDelayedSection = buildAsyncSelector(
new win.Promise<Element>((resolve) => {
setTimeout(() => {
resolve(
doc.querySelector('section')
);
}, 500);
}),
{
retries: 20,
delay: 50
}
);

expect(
await selector['#section'].$['.article'].$['.delayed-list-container'].$.ul['li:nth-of-type(2)'].element
).to.text('Delayed List item 2');
Expand All @@ -119,6 +134,12 @@ describe('ShadowDomSelector buildAsyncSelector class spec', () => {
(await selector.section.$.article.$['.delayed-list-container'].$['ul > li'].all).length
).to.equal(3);

expect(
await selectorFromDelayedSection.element
).to.equal(
doc.querySelector('section')
);

});

});
Expand Down Expand Up @@ -165,6 +186,18 @@ describe('ShadowDomSelector buildAsyncSelector class spec', () => {
}
);

const selectorFromDelayedSection = buildAsyncSelector(
new Promise<ShadowRoot>((resolve) => {
setTimeout(() => {
resolve(doc.querySelector('section').shadowRoot);
}, 500);
}),
{
retries: 20,
delay: 50
}
);

expect(
await selector.article.element
).to.null;
Expand All @@ -181,6 +214,10 @@ describe('ShadowDomSelector buildAsyncSelector class spec', () => {
await selectorFromSection.$.element
).to.null;

expect(
await selectorFromDelayedSection.$.element
).to.null;

expect(
(await selector.all).length
).to.equal(0);
Expand Down
2 changes: 1 addition & 1 deletion src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface AsyncParams {
}

export type AsyncSelectorBase = {
_element: Document | Element | ShadowRoot | Promise<NodeListOf<Element> | ShadowRoot | null>;
_element: Document | Element | ShadowRoot | Promise<NodeListOf<Element> | Element | ShadowRoot | null>;
asyncParams: AsyncParams;
};

Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,17 @@ export function buildAsyncSelector(
asyncParams?: AsyncParams
): AsyncSelectorProxy;
export function buildAsyncSelector(
root: Document | Element | ShadowRoot | Promise<Element | NodeListOf<Element> | ShadowRoot>,
root?: Document | Element | ShadowRoot | Promise<Element | NodeListOf<Element> | ShadowRoot>,
asyncParams?: AsyncParams
): AsyncSelectorProxy;
export function buildAsyncSelector (
firstParameter: Document | Element | ShadowRoot | Promise<Element | NodeListOf<Element> | ShadowRoot> | AsyncParams,
firstParameter?: Document | Element | ShadowRoot | Promise<Element | NodeListOf<Element> | ShadowRoot> | AsyncParams,
secondParameter?: AsyncParams
): AsyncSelectorProxy {
if (firstParameter instanceof Node) {
if (
firstParameter instanceof Node ||
firstParameter instanceof Promise
) {
const params = {
retries: DEFAULT_RETRIES,
delay: DEFAULT_DELAY,
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function getMustErrorText(
}

export function getElementPromise(
element: Document | Element | ShadowRoot | Promise<NodeListOf<Element> | ShadowRoot | null>
element: Document | Element | ShadowRoot | Promise<NodeListOf<Element> | Element | ShadowRoot | null>
): Promise<Document | Element | NodeListOf<Element> | ShadowRoot | null> {
return element instanceof Promise
? element
Expand Down

0 comments on commit da0e04f

Please sign in to comment.