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
I'm currently using the signature_pad package along with Vue 3 + Formkit to create a component that allows the user to sign.
I want to create a unit test for a form that users the component but I'm not being able to make the library to trigger the endStroke event, which I'm listening on the component.
Here is a piece of the code and I hope someone could help me out with that:
SignaturePad.vue - the component that actually contain the signature_pad implementation
<script lang="ts">
import { FormKitFrameworkContext } from'@formkit/core'typeProps= { value?:string}exporttypeContext=FormKitFrameworkContext&Props</script><scriptsetuplang="ts">importSignaturePadfrom'signature_pad'import { computed, onMounted, ref, watch } from'vue'const props =defineProps<{ context:Context}>()// For the signature pad lib to work properly, we need to make sure the canvas element has its dimensions set inline instead of using classesconst resizeCanvas = (canvas:HTMLCanvasElement):void=> {const TAILWIND_SM_BREAKPOINT =640const TAILWIND_SM_PADDING =48const TAILWIND_MD_BREAKPOINT =834const TAILWIND_MD_PADDING =128const SIGNATURE_PAD_MINIMUM_WIDTH =500if (window.innerWidth<TAILWIND_SM_BREAKPOINT) {canvas.width=window.innerWidth-TAILWIND_SM_PADDING } elseif (window.innerWidth<TAILWIND_MD_BREAKPOINT) {canvas.width=window.innerWidth-TAILWIND_MD_PADDING } else {canvas.width=SIGNATURE_PAD_MINIMUM_WIDTH }}const attachEvents = (signaturePad, canvas):void=> {signaturePad.addEventListener('endStroke', () => {console.log('endStroke')const value =signaturePad.toDataURL()props.context.node.input(value) }, { once: false }, )window.onresize= () => {resizeCanvas(canvas) }}const initWatcher = (signaturePad):void=> {watch( () =>props.context.value, (current) => {if (!current) {signaturePad.clear() } }, )}const classes =computed<string>(() => {const stateClass =props.context?.state?.validationVisible&&!props.context?.state?.valid?'border-spf-danger':'border-spf-subtle'return`border ${props.context.class} ${stateClass}`})const signatureCanvas =ref(null)onMounted(() => {const canvas =signatureCanvas.valueif (canvas) {const signaturePad =newSignaturePad(canvas)console.log(signaturePad)attachEvents(signaturePad, canvas)resizeCanvas(canvas)initWatcher(signaturePad)if (props.context.value) {signaturePad.fromDataURL(props.context.value) } }})
</script>
<template>
<canvasid="signature-pad-canvas"ref="signatureCanvas":class="classes"data-testid="signature-pad-canvas"
/>
</template>
SignaturePad.vue - the component is wrapper for Formkit creating a custom component that users the one above
Form.spec.ts - test file for the form with the code I got so far
it('should emit success event when hitting next having the terms and signing',async()=>{render(TermsAndConditionsForm)// Trying to interact with the canvas element programaticallyconstcanvas=screen.getByTestId('signature-pad-canvas',)asHTMLCanvasElementconstcontext=canvas?.getContext('2d')if(context){canvas.dispatchEvent(newEvent('mousedown'))// Set the starting point of the linecontext?.beginPath()context?.moveTo(50,50)// Starting point (x, y)// Draw the line to the ending pointcontext?.lineTo(200,200)// Ending point (x, y)// Set the line color and widthcontext.strokeStyle='black'context.lineWidth=2// Render the linecontext?.stroke()canvas.dispatchEvent(newEvent('mouseup'))}else{thrownewError('Canvas context is not available')}constnextButton=screen.getByRole('button',{name: /next/i})awaituser.click(nextButton)expect(screen.queryByText(/Signatureisrequired./i)).toBeNull()})
The text was updated successfully, but these errors were encountered:
If you want an end to end test you should use pointer events instead of the canvas context to draw on the canvas. The pointer up event is what will dispatch the endStroke event. See our tests for how to do that.
If you want a unit test that does not also test the internals of signature_pad you can just dispatch the endStroke event in your test
Hello folks,
I'm currently using the
signature_pad
package along with Vue 3 + Formkit to create a component that allows the user to sign.I want to create a unit test for a form that users the component but I'm not being able to make the library to trigger the
endStroke
event, which I'm listening on the component.Here is a piece of the code and I hope someone could help me out with that:
SignaturePad.vue
- the component that actually contain the signature_pad implementationSignaturePad.vue
- the component is wrapper for Formkit creating a custom component that users the one aboveForm.vue
- the component that uses the custom signature padForm.spec.ts
- test file for the form with the code I got so farThe text was updated successfully, but these errors were encountered: