furniture-3d-system-Wu/tests/lib/engine-events.test.ts

31 lines
1.4 KiB
TypeScript

import { bindEngineEvents, unbindEngineEvents } from '@/lib/3d/engine-events'
describe('engine-events', () => {
it('应该在绑定和解绑时成对注册 DOM 监听器', () => {
const domElement = document.createElement('canvas')
const handlers = {
onResize: jest.fn(),
onContextMenu: jest.fn(),
onMouseDown: jest.fn(),
onAuxClick: jest.fn(),
}
const addWindowSpy = jest.spyOn(window, 'addEventListener')
const removeWindowSpy = jest.spyOn(window, 'removeEventListener')
const addElementSpy = jest.spyOn(domElement, 'addEventListener')
const removeElementSpy = jest.spyOn(domElement, 'removeEventListener')
bindEngineEvents(domElement, handlers)
unbindEngineEvents(domElement, handlers)
expect(addWindowSpy).toHaveBeenCalledWith('resize', handlers.onResize)
expect(removeWindowSpy).toHaveBeenCalledWith('resize', handlers.onResize)
expect(addElementSpy).toHaveBeenCalledWith('contextmenu', handlers.onContextMenu)
expect(removeElementSpy).toHaveBeenCalledWith('contextmenu', handlers.onContextMenu)
expect(addElementSpy).toHaveBeenCalledWith('mousedown', handlers.onMouseDown)
expect(removeElementSpy).toHaveBeenCalledWith('mousedown', handlers.onMouseDown)
expect(addElementSpy).toHaveBeenCalledWith('auxclick', handlers.onAuxClick)
expect(removeElementSpy).toHaveBeenCalledWith('auxclick', handlers.onAuxClick)
})
})