You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// example using ava test runnerimporttestfrom'ava'import{setupDOM}from'@litecanvas/jsdom-extras'importlitecanvasfrom'litecanvas'/** @type {LitecanvasInstance} */letlocaltest.before(()=>{setupDOM()local=litecanvas({global: false,})})test.after(()=>{local.quit()})test('test',async(t)=>{// `onLitecanvas()` returns a promise// and resolve it by default after after that eventconstevent='init'awaitonLitecanvas(local,event,()=>{t.is(Math.PI,local.PI)})})test('another test',async(t)=>{constevent='update'awaitonLitecanvas(local,event,(dt)=>{// if elapsed time is less than 2 secondsif(local.T<2){// return `false` to not resolve the promise yet// and keep that event listenerreturnfalse}else{// that test will run after 2 secondst.pass()}})})
How to test <canvas> rendering context:
// example using ava test runnerimporttestfrom'ava'import{setupDOM}from'@litecanvas/jsdom-extras'test.before(()=>{setupDOM()})test('test canvas',(t)=>{constcanvas=document.createElement('canvas')constctx=canvas.getContext('2d')// draw a filled rect_fillRect(ctx,0,0,300,150,'#FF0000')// test the rendering context latest callsconstexpected=['set fillStyle #FF0000','fillRect(0,0,300,150)']constactual=ctx._calls.slice(-expected.length)t.deepEqual(actual,expected)})function_fillRect(ctx,x,y,w,h,color){ctx.fillStyle=colorctx.fillRect(x,y,w,h)}