Skip to content

Commit

Permalink
fix(): karma waiting
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Sep 30, 2019
1 parent 86bdd30 commit 5bc76a7
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions test/karma/test-app/util.ts
Expand Up @@ -79,6 +79,38 @@ export function setupDomTests(document: Document) {

return new Promise<HTMLElement>((resolve, reject) => {
try {
const waitFrame = () => {
return new Promise(resolve => {
requestAnimationFrame(resolve);
});
};

const allReady = () => {
const promises: Promise<any>[] = [];
const waitForDidLoad = (promises: Promise<any>[], elm: Element) => {
if (elm != null && elm.nodeType === 1) {
for (let i = 0; i < elm.children.length; i++) {
const childElm = elm.children[i];
if (childElm.tagName.includes('-') && typeof (childElm as any).componentOnReady === 'function') {
promises.push((childElm as any).componentOnReady());
}
waitForDidLoad(promises, childElm);
}
}
};

waitForDidLoad(promises, window.document.documentElement);

return Promise.all(promises)
.catch((e) => console.error(e));
};

const stencilReady = () => {
return allReady()
.then(() => waitFrame())
.then(() => allReady());
};

const indexLoaded = function(this: XMLHttpRequest) {
if (this.status !== 200) {
reject(`404: ${url}`);
Expand All @@ -92,9 +124,9 @@ export function setupDomTests(document: Document) {

function appLoad() {
window.removeEventListener('stencil_appload', appLoad);
setTimeout(() => {
stencilReady().then(() => {
resolve(app);
}, 400);
});
}

window.addEventListener('stencil_appload', appLoad);
Expand Down

0 comments on commit 5bc76a7

Please sign in to comment.