Tengo una función en mi aplicación que devuelve verdadero o falso al verificar una segunda función. Y esta función deshabilitará/habilitará el campo de entrada.
Ahora quiero espiar la primera función y llamarla con un valor específico.
Sé que esto es posible en Jasmine con spyOn y .and.returnValue . Y con eso puedo cubrir los diferentes casos en los que la función devuelve verdadero o falso.
¿Cómo puedo llamar a una función en Jest con un valor específico?
Mi código:
<input :id="id" v-model="searchKey" :disabled="testFunctionB" type="text" name="search box" autocomplete="off" > computed: { testFunctionA () { if(!this.testData) return false return this.testData1 !== '' }, testFunctionB () { if (this.testData2 === false) return false return this.testFunctionA } } Así que quiero cubrir la función cuando testData2 se define como null , esto significa que testFunctionB devolverá testFunctionA .
Quiero asegurarme de que la función testFunctionA sea verdadera y que testFunctionB también devuelva verdadero. Entonces puedo cubrir los múltiples casos.
Esto es lo que probé:
it('should disable the input when the testData2 value is not defined and the testFunctionA is true', async () => { await wrapper.setProps({testData2: null}) wrapper.vm.testFunctionA = jest.fn() wrapper.vm.testFunctionA.mockReturnValueOnce(true) expect(wrapper.vm.testFunctionB).toBe(true) }) Así que trato de poner un espía en testFunctionA y devolver un verdadero o falso. ¿Cómo puedo hacer esto?
Para cubrir su caso de prueba, debe testFunctionA antes de llamar a setProps({testData2: null}) . Eso es porque wrapper.setProps(...) es lo que activa la evaluación de testFunctionB :
it('should disable the input when the testData2 value is not defined and the testFunctionA is true', async () => { jest.spyOn(wrapper.vm, 'testFunctionA').mockReturnValueOnce(true); await wrapper.setProps({testData2: null}); expect(wrapper.vm.testFunctionB).toBe(true); })