Skip to content

Commit

Permalink
Improve hook for running code before initialisation in `renderAndInit…
Browse files Browse the repository at this point in the history
…ialise`

- Clarify naming as it's actually code that runs before initialisation rather than does any kind of initialisation of the component itself
- Update how the hook is run to be directly called by `page.evaluate`.
  Having it run as part of the function that did the initialisation didn't run reliably for updating the `govuk-frontend-supported` class,
  or even other simpler scenarios like logging or throwing. Calling it directly with `page.evaluate` seems to run more reliably.
  • Loading branch information
romaricpascal committed Aug 1, 2023
1 parent d5456e0 commit 2e57cd6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ describe('Character count', () => {
// Override maxlength to 10
maxlength: 10
},
initialiser ($module) {
beforeInitialisation ($module) {
// Set locale to Welsh, which expects translations for 'one', 'two',
// 'few' 'many' and 'other' forms – with the default English strings
// provided we only have translations for 'one' and 'other'.
Expand Down
18 changes: 9 additions & 9 deletions shared/helpers/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ async function axe (page, overrides = {}) {
* @param {object} options - Render and initialise options
* @param {object} options.params - Nunjucks macro params
* @param {object} [options.config] - Component instantiation config
* @param {($module: Element) => void} [options.initialiser] - A function that'll run in the
* browser to execute arbitrary initialisation
* @param {($module: Element) => void} [options.beforeInitialisation] - A function that'll run in the browser
* via `page.evaluate`
* @returns {Promise<import('puppeteer').Page>} Puppeteer page object
*/
async function renderAndInitialise (page, componentName, options) {
Expand All @@ -91,18 +91,18 @@ async function renderAndInitialise (page, componentName, options) {
slot.innerHTML = htmlForSlot
}, html)

// Run a script to init the JavaScript component
await page.evaluate(async (exportName, options) => {
const $module = document.querySelector('[data-module]')
const componentRootHandle = await page.$('[data-module]')

if (options.initialiser) {
options.initialiser($module)
}
if (options.beforeInitialisation) {
await componentRootHandle.evaluate(options.beforeInitialisation)
}

// Run a script to init the JavaScript component
await componentRootHandle.evaluate(async ($module, exportName, options) => {
const namespace = await import('govuk-frontend')
/* eslint-disable-next-line no-new */
new namespace[exportName]($module, options.config)
}, componentNameToClassName(componentName), options)
}, componentNameToClassName(componentName), options, options.beforeInitialisation)

return page
}
Expand Down

0 comments on commit 2e57cd6

Please sign in to comment.