mercredi 20 mars 2019

How to test dynamic changes to the DOM via JavaScript with Jest or some other testing library?

I am dynamically adding a <script> tag to the document <head> on page load based on the environment.

The Function:

export const loadScript = () => { // load script tag into head const HEAD = document.getElementsByTagName('head') const SCRIPT_TAG = document.createElement('script') SCRIPT_TAG.setAttribute('src', process.env.SCRIPT_SRC) SCRIPT_TAG.setAttribute('async', true) HEAD[0].append(SCRIPT_TAG) }

I want to write a test that checks if once the loadScript() function is run that the <script> tag made it into the head. Our environment is set up with Jest, and I haven't found a satisfactory example that demonstrates how to do it, or works.

I am new to testing, and would appreciate any solutions, or hints offered.

Aucun commentaire:

Enregistrer un commentaire