@@ -213,4 +213,79 @@ test.describe("_hyperscript bootstrapping", () => {
213213 } ) ;
214214 expect ( hasState ) . toBe ( false ) ;
215215 } ) ;
216+
217+ test ( "sets data-hyperscript-powered on initialized elements" , async ( { html, find} ) => {
218+ await html ( "<div _='on click add .foo'></div>" ) ;
219+ await expect ( find ( 'div' ) ) . toHaveAttribute ( 'data-hyperscript-powered' , 'true' ) ;
220+ } ) ;
221+
222+ test ( "cleanup removes data-hyperscript-powered" , async ( { html, find, evaluate} ) => {
223+ await html ( "<div _='on click add .foo'></div>" ) ;
224+ await expect ( find ( 'div' ) ) . toHaveAttribute ( 'data-hyperscript-powered' , 'true' ) ;
225+ await evaluate ( ( ) => {
226+ _hyperscript . cleanup ( document . querySelector ( '#work-area div' ) ) ;
227+ } ) ;
228+ expect ( await find ( 'div' ) . getAttribute ( 'data-hyperscript-powered' ) ) . toBeNull ( ) ;
229+ } ) ;
230+
231+ test ( "fires hyperscript:before:init and hyperscript:after:init" , async ( { html, find, evaluate} ) => {
232+ var events = await evaluate ( ( ) => {
233+ var events = [ ] ;
234+ var wa = document . getElementById ( 'work-area' ) ;
235+ wa . addEventListener ( 'hyperscript:before:init' , ( ) => events . push ( 'before:init' ) ) ;
236+ wa . addEventListener ( 'hyperscript:after:init' , ( ) => events . push ( 'after:init' ) ) ;
237+ wa . innerHTML = "<div _='on click add .foo'></div>" ;
238+ _hyperscript . processNode ( wa ) ;
239+ return events ;
240+ } ) ;
241+ expect ( events ) . toEqual ( [ 'before:init' , 'after:init' ] ) ;
242+ } ) ;
243+
244+ test ( "hyperscript:before:init can cancel initialization" , async ( { html, find, evaluate} ) => {
245+ var result = await evaluate ( ( ) => {
246+ var wa = document . getElementById ( 'work-area' ) ;
247+ wa . addEventListener ( 'hyperscript:before:init' , ( e ) => e . preventDefault ( ) , { once : true } ) ;
248+ wa . innerHTML = "<div _='on click add .foo'></div>" ;
249+ _hyperscript . processNode ( wa ) ;
250+ var div = wa . querySelector ( 'div' ) ;
251+ return {
252+ initialized : ! ! div . _hyperscript ?. initialized ,
253+ hasPowered : div . hasAttribute ( 'data-hyperscript-powered' ) ,
254+ } ;
255+ } ) ;
256+ expect ( result . initialized ) . toBe ( false ) ;
257+ expect ( result . hasPowered ) . toBe ( false ) ;
258+ } ) ;
259+
260+ test ( "fires hyperscript:before:cleanup and hyperscript:after:cleanup" , async ( { html, find, evaluate} ) => {
261+ await html ( "<div _='on click add .foo'></div>" ) ;
262+ var events = await evaluate ( ( ) => {
263+ var events = [ ] ;
264+ var div = document . querySelector ( '#work-area div' ) ;
265+ div . addEventListener ( 'hyperscript:before:cleanup' , ( ) => events . push ( 'before:cleanup' ) ) ;
266+ div . addEventListener ( 'hyperscript:after:cleanup' , ( ) => events . push ( 'after:cleanup' ) ) ;
267+ _hyperscript . cleanup ( div ) ;
268+ return events ;
269+ } ) ;
270+ expect ( events ) . toEqual ( [ 'before:cleanup' , 'after:cleanup' ] ) ;
271+ } ) ;
272+
273+ test ( "logAll config logs events to console" , async ( { html, find, evaluate} ) => {
274+ var logged = await evaluate ( ( ) => {
275+ var logs = [ ] ;
276+ var origLog = console . log ;
277+ console . log = ( ...args ) => logs . push ( args [ 0 ] ) ;
278+ _hyperscript . config . logAll = true ;
279+ try {
280+ var wa = document . getElementById ( 'work-area' ) ;
281+ wa . innerHTML = "<div _='on click add .foo'></div>" ;
282+ _hyperscript . processNode ( wa ) ;
283+ } finally {
284+ _hyperscript . config . logAll = false ;
285+ console . log = origLog ;
286+ }
287+ return logs . some ( l => typeof l === 'string' && l . includes ( 'hyperscript:' ) ) ;
288+ } ) ;
289+ expect ( logged ) . toBe ( true ) ;
290+ } ) ;
216291} ) ;
0 commit comments