import userEvent from "@testing-library/user-event" ; import { createSignal , onCleanup } from "solid-js" ; import { screen , render } from ".." ; function StopWatch ( ) { const [ lapse , setLapse ] = createSignal ( 0 ) ; const [ running , setRunning ] = createSignal ( false ) ; let timer ; const handleRunClick = ( ) => { if ( running ( ) ) { clearInterval ( timer ) ; } else { const startTime = Date . now ( ) - lapse ( ) ; timer = setInterval ( ( ) => setLapse ( Date . now ( ) - startTime ) ) ; } return setRunning ( ! running ( ) ) ; } ; const handleClearClick = ( ) => { clearInterval ( timer ) ; setLapse ( 0 ) ; setRunning ( false ) ; } ; onCleanup ( ( ) => clearInterval ( timer ) ) ; return ( < div > < span > { lapse ( ) } ms < /span> < button onClick = { handleRunClick } > { running ( ) ? "Stop" : "Start" } < /button> < button onClick = { handleClearClick } > Clear < /button> < /div>); } const wait = ( time ) => new Promise ( resolve => setTimeout ( resolve , time ) ) ; test ( "unmounts a component" , async ( ) => { jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ; const { unmount , container } = render ( ( ) => < StopWatch />); userEvent . click ( screen . getByText ( "Start" ) as Element ) ; unmount ( ) ; expect ( container . innerHTML ) . toBe ( "" ) ; await wait ( ( ( ) => expect ( console . error ) . not . toHaveBeenCalled ( ) ) as any ) ; } ) ;