Javascript 使用jest(或任何其他技术)模拟jquery$及其函数
我有一些使用jquery的javascript:Javascript 使用jest(或任何其他技术)模拟jquery$及其函数,javascript,jquery,testing,mocking,spy,Javascript,Jquery,Testing,Mocking,Spy,我有一些使用jquery的javascript: var $newItemButton = $('#newItemButton'); var $newItemForm = $('#newItemForm'); var $textInput = $('input:text'); $newItemButton.show(); $newItemForm.hide(); $('#showForm').on('click', function(){ console.
var $newItemButton = $('#newItemButton');
var $newItemForm = $('#newItemForm');
var $textInput = $('input:text');
$newItemButton.show();
$newItemForm.hide();
$('#showForm').on('click', function(){
console.log("SHOW FORM CLICKED");
$newItemButton.hide();
$newItemForm.show();
});
$newItemForm.on('submit', function(e){
e.preventDefault();
var newText = $textInput.val();
$('li:last').after('<li>' + newText + '</li>');
$newItemForm.hide();
$newItemButton.show();
$textInput.val('');
});
但不是所有对象上的hide()
,只在$newItemButton
上。
所以,总的来说,我的问题是如何模拟/监视jquery$
及其各种函数,以便在单元测试中控制它们
var spy = jest.spyOn($.fn, 'hide').mockImplementation(() =>{});
$('#showForm').click();
expect(spy).toHaveBeenCalled();