@@ -76,9 +76,8 @@ async function axe (page, overrides = {}) {
76
76
* @param {object } options - Render and initialise options
77
77
* @param {object } options.params - Nunjucks macro params
78
78
* @param {object } [options.config] - Component instantiation config
79
- * @param {(context: { config?: object, namespace: object }) => void } [options.initialiser] - A function that'll run in the
80
- * browser to execute arbitrary initialisation. Receives an object with the
81
- * passed configuration as `config` and the GOVUKFrontend global as `namespace`
79
+ * @param {($module: Element) => void } [options.initialiser] - A function that'll run in the
80
+ * browser to execute arbitrary initialisation
82
81
* @returns {Promise<import('puppeteer').Page> } Puppeteer page object
83
82
*/
84
83
async function renderAndInitialise ( page , componentName , options ) {
@@ -92,22 +91,15 @@ async function renderAndInitialise (page, componentName, options) {
92
91
} , html )
93
92
94
93
// Run a script to init the JavaScript component
95
- await page . evaluate ( ( componentClassName , options ) => {
96
- const $component = document . querySelector ( '[data-module]' )
97
-
98
- // Check for window global
99
- if ( ! ( 'GOVUKFrontend' in window ) || ! window . GOVUKFrontend [ componentClassName ] ) {
100
- throw new Error ( `Global 'window.GOVUKFrontend.${ componentClassName } ' not found` )
101
- }
94
+ await page . evaluate ( async ( exportName , options ) => {
95
+ const $module = document . querySelector ( '[data-module]' )
102
96
103
97
if ( options . initialiser ) {
104
- return options . initialiser ( {
105
- config : options . config ,
106
- namespace : window . GOVUKFrontend
107
- } )
98
+ options . initialiser ( $module )
108
99
}
109
100
110
- new window . GOVUKFrontend [ componentClassName ] ( $component , options . config ) . init ( )
101
+ const namespace = await import ( 'govuk-frontend' )
102
+ new namespace [ exportName ] ( $module , options . config ) . init ( )
111
103
} , componentNameToClassName ( componentName ) , options )
112
104
113
105
return page
0 commit comments