我的應用程式中有一個函式,它通過檢查第二個函式回傳真或假。此功能將禁用/啟用輸入欄位。
現在我想監視第一個函式并用一個特定的值呼叫它。
我知道這在 Jasmine 中是可能的spyOn和.and.returnValue。這樣我就可以涵蓋函式回傳真或假的不同情況。
如何在 Jest 中呼叫具有特定值的函式?
我的代碼:
<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
}
}
所以我想覆寫函式whentestData2定義為null,這意味著testFunctionB將回傳testFunctionA。
我想確保該函式testFunctionA為真并且testFunctionB也回傳真。所以我可以涵蓋多種情況。
這是我嘗試過的:
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)
})
因此,我嘗試安裝間諜testFunctionA并回傳真或假。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
為了覆寫您的測驗用例,您需要testFunctionA在呼叫setProps({testData2: null}). 那是因為wrapper.setProps(...)是觸發評估的原因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);
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/417408.html
標籤:
