Skip to content

Instantly share code, notes, and snippets.

@nicohsieh
Last active July 17, 2018 23:38
Show Gist options
  • Save nicohsieh/9ba5571076e3f579250b5a3a5d7d7cdb to your computer and use it in GitHub Desktop.
Save nicohsieh/9ba5571076e3f579250b5a3a5d7d7cdb to your computer and use it in GitHub Desktop.
Js content to append to Mochawesome app.js asset in post-process to auto expand contexts

Procedually clicking on context items to expand them after dom mutation is over to enable auto expand behavier which Mochawesome doesn't plan to support(adamgruber/mochawesome#217)

!(function() {
 function postExpand() {
    const contextEls = document.querySelectorAll('.with-context');

    [...contextEls].forEach(item => {
      // expand test items with context
      item.querySelector('header').click();
    });
  }
  
  // wait till the dom mutation is over
  const targetNode = document.getElementById('report');
  const config = {
    childList: true,
    subtree: true
  };
  const maxWait = 5000;
  const waitTimeSpan = 1000;
  let isDone = false;
  let timerRef;

  function timer() {
    return setTimeout(done, waitTimeSpan);
  }

  function done() {
    observer.disconnect();
    postExpand();
    isDone = true;
  }

  const observer = new MutationObserver(mutationsList => {
    if (timerRef) {
      clearTimeout(timerRef);
    }
    timerRef = timer();
  });

  setTimeout(() => {
    if (isDone) {
      return;
    }
    if (timerRef) {
      clearTimeout(timerRef);
    }
    done();
  }, maxWait);
  observer.observe(targetNode, config);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment