Tengo un componente de gancho que está haciendo llamadas al lado del servidor usando socket.io
. Necesito probar la funcionalidad del código. Y para hacer eso, necesito simular la respuesta de socket.io. ¿Cómo burlarse de la respuesta?
useResult.js
import { useEffect, useRef, useState } from 'react' import io from 'socket.io-client' const useResultComponent = () => { const socket = useRef() // state const [loading, setLoading] = useState(false) // side effects useEffect(() => { setLoading(true) socket.current = io.connect('', { secure: true, path: 'url1', rejectUnauthorized: true, verify: true, transports: ['websocket'] }) socket.current.on('my_fn_1', function ({ id }) { if (id) { my_fn_2(id) } else { setLoading(false) } }) socket.current.on('my_fn_3', (res) => { setLoading(false) if (res.data) { socket.current.emit('my_fn_4', { some_data: some_value }) } }) setTimeout(function () { my_fn_1() }, 3000) // eslint-disable-next-line }, []) function my_fn_1() { socket.current.emit('my_fn_1', params) } function my_fn_2() {} return { loading } }