mardi 28 mars 2017

Karma-Mocha - How to test method that listens to DOMContentLoaded event

I am writing tests for an internal framework that my company maintains. In it there is a method that is equivalent to jQuery's $(document).ready(handler) method. The method looks like this:

export function $ready(fn) {
    document.readyState === 'complete' ?
        $exec(fn) :
        document.addEventListener('DOMContentLoaded', function() {
            $exec(fn);
        });
}

I would like to be able to write a test that will execute the event listener callback rather than immediately executing fn, however I do not know of a way to get my test to execute before readyState is set to complete, nor do I know of a way to re-trigger the DOMContentLoaded event. Any recommendations would be appreciated.

Aucun commentaire:

Enregistrer un commentaire