11import { html , HtmlTemplate , state } from './html'
2- import { when , repeat } from './helpers'
2+ import { when , repeat , oneOf } from './helpers'
33import { suspense } from './utils'
44import { helper } from "./Helper" ;
55
@@ -1269,15 +1269,15 @@ describe('html', () => {
12691269 < div class ="todo-actions ">
12701270 ${ when (
12711271 ( ) => this . #status === 'pending' ,
1272- html `${ completeBtn } ${ editBtn } ${ archiveBtn } `
1272+ html `${ completeBtn } ${ editBtn } `
12731273 ) }
12741274 ${ when (
1275- ( ) => this . #status === 'archived' ,
1276- html ` ${ progressBtn } ${ deleteBtn } `
1275+ oneOf ( this . #status, [ 'completed' , 'pending' ] ) ,
1276+ archiveBtn
12771277 ) }
12781278 ${ when (
1279- ( ) => this . #status === 'completed ' ,
1280- archiveBtn
1279+ ( ) => this . #status === 'archived ' ,
1280+ html ` ${ progressBtn } ${ deleteBtn } `
12811281 ) }
12821282 </ div >
12831283 </ div > `
@@ -1336,8 +1336,8 @@ describe('html', () => {
13361336 expect ( todo . shadowRoot ?. innerHTML ) . toBe ( '<div class="todo-item">\n' +
13371337 '\t\t\t\t\t\t\t<div class="details">\n' +
13381338 '\t\t\t\t\t\t\t\t<h3>sample</h3></div>\n' +
1339- '\t\t\t\t\t\t\t<div class="todo-actions"><button>complete</button><button>edit</button><button>archive</button> \n' +
1340- '\t\t\t\t\t\t\t\t\n' +
1339+ '\t\t\t\t\t\t\t<div class="todo-actions"><button>complete</button><button>edit</button>\n' +
1340+ '\t\t\t\t\t\t\t\t<button>archive</button> \n' +
13411341 '\t\t\t\t\t\t\t\t</div>\n' +
13421342 '\t\t\t\t\t\t</div>' )
13431343
@@ -1348,11 +1348,10 @@ describe('html', () => {
13481348 expect ( document . body . innerHTML ) . toBe (
13491349 '<todo-item name="sample" description="" status="completed"></todo-item>'
13501350 )
1351- expect ( todo . shadowRoot ?. innerHTML ) . toBe (
1352- '<div class="todo-item">\n' +
1351+ expect ( todo . shadowRoot ?. innerHTML ) . toBe ( '<div class="todo-item">\n' +
13531352 '\t\t\t\t\t\t\t<div class="details">\n' +
13541353 '\t\t\t\t\t\t\t\t<h3>sample</h3></div>\n' +
1355- '\t\t\t\t\t\t\t<div class="todo-actions"><button>edit</button><button> archive</button>\n' +
1354+ '\t\t\t\t\t\t\t<div class="todo-actions"><button>archive</button>\n' +
13561355 '\t\t\t\t\t\t\t\t</div>\n' +
13571356 '\t\t\t\t\t\t</div>'
13581357 )
@@ -1636,21 +1635,87 @@ describe('html', () => {
16361635 expect ( document . body . innerHTML ) . toBe ( '<span>1</span><button>+</button>' )
16371636 } ) ;
16381637
1639- it ( 'should handle onUpdate callback' , ( ) => {
1640- const [ count , setCount ] = state < number > ( 0 )
1641- const updateMock = jest . fn ( )
1638+ describe ( 'should handle lifecycles' , ( ) => {
1639+ beforeEach ( ( ) => {
1640+ jest . useFakeTimers ( )
1641+ } )
16421642
1643- const counter = html `< span > ${ count } </ span > `
1644- counter . onUpdate ( updateMock )
1645- counter . render ( document . body )
1643+ it ( 'onUpdate' , ( ) => {
1644+ const [ count , setCount ] = state < number > ( 0 )
1645+ const updateMock = jest . fn ( )
1646+
1647+ const counter = html `< span > ${ count } </ span > `
1648+ counter . onUpdate ( updateMock )
1649+ counter . render ( document . body )
1650+
1651+ expect ( document . body . innerHTML ) . toBe ( '<span>0</span>' )
1652+
1653+ setCount ( ( prev ) => prev + 1 )
1654+
1655+ jest . advanceTimersByTime ( 100 ) ;
1656+
1657+ expect ( updateMock ) . toHaveBeenCalledTimes ( 1 )
1658+
1659+ expect ( document . body . innerHTML ) . toBe ( '<span>1</span>' )
1660+ } )
16461661
1647- expect ( document . body . innerHTML ) . toBe ( '<span>0</span>' )
1662+ it ( 'onMount' , ( ) => {
16481663
1649- setCount ( ( prev ) => prev + 1 )
1664+ const mountMock = jest . fn ( )
1665+
1666+ html `< span > sample</ span > `
1667+ . onMount ( mountMock )
1668+ . render ( document . body )
1669+
1670+ jest . advanceTimersByTime ( 100 ) ;
1671+
1672+ expect ( mountMock ) . toHaveBeenCalledTimes ( 1 )
1673+ } ) ;
16501674
1651- expect ( updateMock ) . toHaveBeenCalledTimes ( 1 )
1675+ it ( 'onUnmount' , ( ) => {
1676+ const unmountMock = jest . fn ( )
1677+
1678+ const temp = html `< span > sample</ span > `
1679+ . onUnmount ( unmountMock )
1680+ . render ( document . body )
1681+
1682+ temp . unmount ( ) ;
1683+
1684+ jest . advanceTimersByTime ( 100 ) ;
1685+
1686+ expect ( unmountMock ) . toHaveBeenCalledTimes ( 1 )
1687+ } ) ;
16521688
1653- expect ( document . body . innerHTML ) . toBe ( '<span>1</span>' )
1689+ it ( 'onUnmount on removed item' , ( ) => {
1690+ const unmountMock = jest . fn ( ) ;
1691+ const list = [
1692+ html `one` . onUnmount ( unmountMock ) ,
1693+ html `two` . onUnmount ( unmountMock ) ,
1694+ html `three` . onUnmount ( unmountMock ) ,
1695+ ]
1696+
1697+ const temp = html `${ ( ) => list } `
1698+ . render ( document . body )
1699+
1700+ expect ( document . body . innerHTML ) . toBe ( 'onetwothree' )
1701+
1702+ list . splice ( 1 , 1 ) ;
1703+ const three = list . splice ( 1 , 1 ) ;
1704+
1705+ temp . update ( ) ;
1706+
1707+ expect ( document . body . innerHTML ) . toBe ( 'one' )
1708+
1709+ jest . advanceTimersByTime ( 100 ) ;
1710+
1711+ expect ( unmountMock ) . toHaveBeenCalledTimes ( 2 )
1712+
1713+ list . unshift ( ...three ) ;
1714+
1715+ temp . update ( ) ;
1716+
1717+ expect ( document . body . innerHTML ) . toBe ( 'threeone' )
1718+ } ) ;
16541719 } )
16551720
16561721 it ( 'should ignore values between tag and attribute' , ( ) => {
0 commit comments