11import path from 'path' ;
22
33import type { ElementHandle , PleasantestUtils } from 'pleasantest' ;
4- import { getAccessibilityTree , withBrowser } from 'pleasantest' ;
4+ import {
5+ getAccessibilityTree ,
6+ makeCallableJSHandle ,
7+ withBrowser ,
8+ } from 'pleasantest' ;
59
610import { loadGlobalCSS , loadTwigTemplate } from '../../../test-utils.js' ;
711
12+ import type { createSubscribe } from './subscribe.js' ;
13+
814// Helper to load the Twig template file
915const componentMarkup = ( args = { } ) =>
1016 loadTwigTemplate ( path . join ( __dirname , './subscribe.twig' ) ) ( {
@@ -25,7 +31,7 @@ const demoDestroyInitMarkup = loadTwigTemplate(
2531const expectElementToBeVisuallyHidden = async (
2632 element : ElementHandle < HTMLElement >
2733) => {
28- const { elHeight, elWidth } = await element . evaluate ( ( el : HTMLElement ) => ( {
34+ const { elHeight, elWidth } = await element . evaluate ( ( el ) => ( {
2935 elHeight : el . clientHeight ,
3036 elWidth : el . clientWidth ,
3137 } ) ) ;
@@ -41,7 +47,7 @@ const expectElementToBeVisuallyHidden = async (
4147const expectElementNotToBeVisuallyHidden = async (
4248 form : ElementHandle < HTMLElement >
4349) => {
44- const { elHeight, elWidth } = await form . evaluate ( ( el : HTMLElement ) => ( {
50+ const { elHeight, elWidth } = await form . evaluate ( ( el ) => ( {
4551 elHeight : el . clientHeight ,
4652 elWidth : el . clientWidth ,
4753 } ) ) ;
@@ -277,17 +283,24 @@ describe('Subscription component', () => {
277283 await loadGlobalCSS ( utils ) ;
278284 await utils . loadCSS ( './subscribe.scss' ) ;
279285 await utils . injectHTML ( await demoDestroyInitMarkup ( ) ) ;
280- await utils . runJS ( `
286+ const { subscribeComponent } = await utils . runJS < {
287+ subscribeComponent : NonNullable < ReturnType < typeof createSubscribe > > ;
288+ } > ( `
281289 import { createSubscribe } from './subscribe';
282- // We attach it to the window object as a workaround to have access to
283- // the subscribeComponent later in this test.
284- window.subscribeComponent = createSubscribe(
290+ export const subscribeComponent = createSubscribe(
285291 document.querySelector('.js-subscribe')
286292 );
287293 // Set it to the "destroyed" state
288- window. subscribeComponent.destroy();
294+ subscribeComponent.destroy();
289295 ` ) ;
290296
297+ const initSubscribe = makeCallableJSHandle (
298+ await subscribeComponent . getProperty ( 'init' )
299+ ) ;
300+ const destroySubscribe = makeCallableJSHandle (
301+ await subscribeComponent . getProperty ( 'destroy' )
302+ ) ;
303+
291304 // The form should be active/visible when `destroy()` is called
292305 const form = await screen . getByRole ( 'form' , {
293306 name : 'Get Weekly Digests' ,
@@ -312,9 +325,7 @@ describe('Subscription component', () => {
312325 await expectElementNotToBeVisuallyHidden ( form ) ;
313326
314327 // Initialize the Subscribe component
315- await utils . runJS ( `
316- window.subscribeComponent.init();
317- ` ) ;
328+ await initSubscribe ( ) ;
318329
319330 // The form should be visually hidden after `init()` is called
320331 await expectElementToBeVisuallyHidden ( form ) ;
@@ -355,9 +366,7 @@ describe('Subscription component', () => {
355366 // Then blur the focus
356367 await formSubmitBtn . evaluate ( ( btn ) => btn . blur ( ) ) ;
357368 // And immediately run the `destroy()`
358- await utils . runJS ( `
359- window.subscribeComponent.destroy();
360- ` ) ;
369+ await destroySubscribe ( ) ;
361370 // Wait out the Subscribe component timeout
362371 await new Promise ( ( resolve ) => {
363372 setTimeout ( resolve , 2000 ) ;
0 commit comments