我正在嘗試通過測驗覆寫此代碼:
export default class {
constructor({ document, onNavigate, firestore, localStorage }) {
this.document = document
this.onNavigate = onNavigate
this.firestore = firestore
const buttonNewBill = document.querySelector(`button[data-testid="btn-new-bill"]`)
if (buttonNewBill) buttonNewBill.addEventListener('click', this.handleClickNewBill)
const iconEye = document.querySelectorAll(`div[data-testid="icon-eye"]`)
if (iconEye) iconEye.forEach(icon => {
icon.addEventListener('click', (e) => this.handleClickIconEye(icon))
})
new Logout({ document, localStorage, onNavigate })
}
handleClickNewBill = e => {
this.onNavigate(ROUTES_PATH['NewBill'])
}
handleClickIconEye = (icon) => {
const billUrl = icon.getAttribute("data-bill-url")
const imgWidth = Math.floor($('#modaleFile').width() * 0.5)
$('#modaleFile').find(".modal-body").html(`<div style='text-align: center;'><img width=${imgWidth} src=${billUrl} /></div>`)
$('#modaleFile').modal('show')
}
這就是測驗handelClickIconEye函式的累
test("Then i click on Action icon modal should be open", ()=>{
const onNavigate=(pathname)=>{
document.body.innerHTML=ROUTES({pathname})
}
Object.defineProperty(window,'localStorage',{value:localStorageMock})
window.localStorage.setItem('user',JSON.stringify({
type:'Employee'
}))
const bill=new Bills({
document,onNavigate,firestore:null,localStorage:window.localStorage})
const html=BillsUI({data:bills})
document.body.innerHTML=html
const icons=screen.getAllByTestId('icon-eye')
const handleClickIconEye1=jest.fn((e)=>bill.handleClickIconEye(e,icons))
icons.forEach(icon=>{icon.addEventListener('click',handleClickIconEye1)
fireEvent.click(icon)
})
expect(handleClickIconEye1).toHaveBeenCalled()
})
我不明白為什么它會給我錯誤:

這是 HTML 變數包含的內容:

uj5u.com熱心網友回復:
我無法從您展示的如何匯入渲染/檔案或正在使用的測驗庫的代碼中準確看到,但是我所知道的是,您不能只對實際上不是的代碼使用 DOM 操作一個 DOM。您的代碼以前可能可以作業,因為它是在瀏覽器中執行的,現在您在沒有任何 DOM 的情況下運行它。對此的解決方法是確保您嘗試測驗的元素實際上已渲染到實際的 DOM,即render來自react-dom.
你遇到這個問題的原因是因為getAttribute它不是一個函式,你呼叫它的物件不被識別為一個元素。
uj5u.com熱心網友回復:
我不認為圖示的定義是這樣的:
let icon = document.querySelector("your-element")
您想要獲得該屬性
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/345890.html
標籤:javascript 单元测试 玩笑
