我不知道如何测试updateTotal
...如果requestAxios
是否成功返回回调函数updateTotal
,但是我怎么监视呢?
...methods:{
updateAll() {
const updateTotal = (request) => {
this.total = request.data.total
}
this.requestAxios(
'get',
'/api/',
{},
[updateTotal],
)
}
}...
requestAxios:
async requestAxios(
method = 'get',
url = '',
objSend = {},
successFunctions = [],
errorsFunctions = [],
formKey = 'form',
) {
let request = ''
if (method !== 'delete') {
request = await axios[method](url, objSend, this.headerRequestJson)
.then(response => this.responseRequestText(response))
.catch(errors => this.responseRequestText(errors.response));
} else {
request = await axios.delete(url, {
data: objSend,
headers: this.headerRequestJson.headers,
})
.then(response => this.responseRequestText(response))
.catch(errors => this.responseRequestText(errors.response));
}
if (request.status === 'success') {
// success callback fn
successFunctions.forEach((value) => {
value(request, formKey)
})
} else {
// errors callback fn
errorsFunctions.forEach((value) => {
value(request)
})
// adicionar erros nos campos
this.addErrors(request, formKey);
}
},
我的尝试:
test('updateTotalFinancial: ', () => {
const update = jest.fn()
const response = {
data: {
total: 100,
},
}
const requestAxios = jest.fn(() => update(response))
const wrapper = shallowMount(ModalUnderwriting, {
store,
localVue,
methods: {
requestAxios,
},
})
wrapper.setData({
total: '0',
})
wrapper.vm.updateTotalFinancial()
首先期望成功,其次不是,不更新数据/变量total
expect(update).toBeCalled()
expect(wrapper.vm.total).toEqual(100)