@@ -2,6 +2,8 @@ const AssertionError = require('assertion-error');
22const fs = require ( 'fs' ) ;
33const path = require ( 'path' ) ;
44
5+ const { TMP_TEST_NAME , writeTmpTestFile, removeTmpTestFile} = require ( '../../src/tmp_file.js' ) ;
6+
57class NightwatchMountError extends AssertionError {
68 constructor ( message ) {
79 super ( message ) ;
@@ -46,18 +48,21 @@ module.exports = class Command {
4648 let beforeMountError ;
4749 let afterMountError ;
4850
51+ await Command . _createEntryScriptFile ( componentOrName , props , isJSX ) ;
52+
4953 // mount component
5054 await this . api
51- . execute ( function ( innerHTML ) {
55+ . executeAsyncScript ( function ( fileName , done ) {
5256 function onReady ( fn ) { if ( document . readyState === 'complete' || document . readyState === 'interactive' ) { setTimeout ( fn ) ; } else { document . addEventListener ( 'DOMContentLoaded' , fn ) } }
5357 onReady ( function ( ) {
5458 var scriptTag = Object . assign ( document . createElement ( 'script' ) , {
59+ src : `/${ fileName } ?t=${ Math . random ( ) . toString ( 36 ) } ` ,
5560 type : 'module' ,
56- innerHTML
61+ onload : function ( ) { done ( ) ; }
5762 } ) ;
5863 document . body . appendChild ( scriptTag ) ;
5964 } ) ;
60- } , [ Command . _buildScript ( componentOrName , props , isJSX ) ] )
65+ } , [ TMP_TEST_NAME ] )
6166
6267 // FIXME: writing the waitUntil outside of the perform using await, breaks the chain
6368
@@ -207,6 +212,8 @@ module.exports = class Command {
207212 done ( browserResult ) ;
208213 } , 100 ) ;
209214 } , [ Command . rootElementId ] ) ;
215+
216+ await removeTmpTestFile ( ) ;
210217
211218 if ( ! result || beforeMountError || afterMountError ) {
212219 const err = this . getError ( 'Could not mount the component.' ) ;
@@ -331,6 +338,9 @@ module.exports = class Command {
331338
332339 static _buildScript ( Component , props , isJSX ) {
333340 return `
341+ // THIS IS GENERATED FILE. DO NOT EDIT IT!
342+ // THIS FILE SHOULD NOT BE ADDED TO THE VERSION CONTROL SYSTEM.
343+
334344 ${ Command . _getReactImports ( ) }
335345 ${ Command . _addDescribeMocks ( isJSX ) }
336346
@@ -365,4 +375,20 @@ module.exports = class Command {
365375 window.__$$PlayFnDone = false;
366376 ` ;
367377 }
378+
379+ /**
380+ * Creates a content for the script that will load component and mount
381+ * it into the renderer's canvas. For that purpose the file will be created
382+ * in the current working directory. That file has to be deleted after a testing is done.
383+ *
384+ * @param {string|ComponentDescription|(() => JSX.Element) } componentOrName
385+ * @param {unknown } props
386+ * @param {boolean } isJSX
387+ * @returns {Promise<void> }
388+ */
389+ static async _createEntryScriptFile ( componentOrName , props , isJSX ) {
390+ const content = Command . _buildScript ( componentOrName , props , isJSX ) ;
391+
392+ await writeTmpTestFile ( content ) ;
393+ }
368394} ;
0 commit comments