Skip to content
Merged
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
60 changes: 60 additions & 0 deletions cypress/e2e/get-from-supported-selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,66 @@ describe('Get From Supported Selector', () => {
});
});

describe('filters hidden-page elements BEFORE asserting hydration', () => {
beforeEach(() => {
cy.document().then((doc) => {
doc.body.insertAdjacentHTML(
'beforeend',
`<div class="ion-page ion-page-hidden">
<div class="testing-order-bug">Not Hydrated Hidden</div>
</div>
<div class="ion-page">
<div class="testing-order-bug hydrated">Visible Hydrated</div>
</div>`,
);
});
});

it('Chainable - does not fail when a hidden element lacks .hydrated', () => {
getFromSupportedSelector(cy.get('.testing-order-bug'))
.should('have.length', 1)
.should('have.text', 'Visible Hydrated');
});

it('JQuery - does not fail when a hidden element lacks .hydrated', () => {
cy.get('.testing-order-bug').then(($els) => {
getFromSupportedSelector($els)
.should('have.length', 1)
.should('have.text', 'Visible Hydrated');
});
});

it('Chainable - returns empty set without timing out when all matches are hidden', () => {
getFromSupportedSelector(
cy.get('.testing-order-bug').filter(':not(.hydrated)'),
).should('have.length', 0);
});

it('JQuery - returns empty set without timing out when all matches are hidden', () => {
cy.get('.testing-order-bug')
.filter(':not(.hydrated)')
.then(($els) => {
getFromSupportedSelector($els).should('have.length', 0);
});
});

it('css selector - returns empty set without timing out when all matches are hidden and not hydrated', () => {
cy.document().then((doc) => {
doc.body.insertAdjacentHTML(
'beforeend',
`<div class="ion-page ion-page-hidden">
<div class="testing-order-hidden-only">Hidden Only</div>
</div>`,
);
});

getFromSupportedSelector('.testing-order-hidden-only').should(
'have.length',
0,
);
});
});

describe('waiting for components hydration', () => {
it('css selector', () => {
getFromSupportedSelector<IonRange>('.ion-range-to-test-hydration')
Expand Down
30 changes: 14 additions & 16 deletions src/helpers/get-from-supported-selector.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
import { CypressIonicReturn, SupportedSelectors } from '../interfaces';

const HIDDEN_PAGE_SELECTOR = '.ion-page-hidden, .ion-page-hidden *';

/**
* @internal
*/
export function getFromSupportedSelector<T extends Element>(
selector: SupportedSelectors<T>,
): CypressIonicReturn<T> {
if (typeof selector === 'string') {
return filterOutHiddenPage(cy.get<T>(`${selector}.hydrated`));
return filterThenAssertHydrated(cy.get<T>(selector));
}

if (isJQuery<T>(selector)) {
return filterOutHiddenPage(
cy
.wrap(selector)
.should('have.class', 'hydrated') as CypressIonicReturn<T>,
);
return filterThenAssertHydrated(cy.wrap(selector) as CypressIonicReturn<T>);
}

return filterOutHiddenPage(
(selector as unknown as CypressIonicReturn<T>).should(
'have.class',
'hydrated',
),
);
return filterThenAssertHydrated(selector as unknown as CypressIonicReturn<T>);
}

function filterOutHiddenPage<T extends Element>(
function filterThenAssertHydrated<T extends Element>(
subject: CypressIonicReturn<T>,
): CypressIonicReturn<T> {
return subject.not(
'.ion-page-hidden, .ion-page-hidden *',
) as unknown as CypressIonicReturn<T>;
return subject.then((elements) => {
const $visible = elements.not(HIDDEN_PAGE_SELECTOR) as unknown as JQuery<T>;
if ($visible.length === 0) {
return cy.wrap($visible);
Comment thread
distante marked this conversation as resolved.
}
return cy.wrap($visible).should('have.class', 'hydrated');
Comment thread
distante marked this conversation as resolved.
}) as unknown as CypressIonicReturn<T>;
}

function isJQuery<T extends Element>(
Expand Down
Loading