Repository: element-plus/element-plus
File: packages/components/popper/__tests__/trigger.test.tsx (TypeScript)

Code before:
      const virtualRef = document.createElement('div')
      await wrapper.setProps({
        virtualRef,
      })

      expect(popperInjection.triggerRef.value).toStrictEqual
    })
  })

  describe('can attach handlers', () => {
    it('should be able to attach handlers to the trigger', async () => {
      const onClick = vi.fn()
      const virtualRef = document.createElement('div')
      wrapper = mountTrigger({
        onClick,
        virtualTriggering: true,
        virtualRef,
      })
      await nextTick()
      expect(onClick).not.toHaveBeenCalled()
      const evt = new MouseEvent('click')
      virtualRef.dispatchEvent(evt)
      await nextTick()
      expect(onClick).toHaveBeenCalled()
    })
    it('should cleanup listeners when ref unmounts', async () => {
      const onClick = vi.fn()
      const first = document.createElement('button')
      const second = document.createElement('button')
      const removeSpy = vi.spyOn(first, 'removeEventListener')
      const addSpy = vi.spyOn(second, 'addEventListener')

      wrapper = mountTrigger({
        onClick,
        virtualTriggering: true,
        virtualRef: first,
      })
      await nextTick()

      await wrapper.setProps({
        virtualRef: second,
      })
      await nextTick()

      first.dispatchEvent(new MouseEvent('click'))
      second.dispatchEvent(new MouseEvent('click'))

      expect(onClick).toHaveBeenCalledTimes(1)
      expect(removeSpy).toHaveBeenCalledWith('click', onClick, false)
      expect(addSpy).toHaveBeenCalledWith('click', onClick, false)
    })

Review comment (suggestion):
This test case still passed before this change.
Perhaps you could refer to the scenario described in https://github.com/element-plus/element-plus/pull/22763#issuecomment-3527858314 and write a related test case.

Code after:
      const virtualRef = document.createElement('div')
      await wrapper.setProps({
        virtualRef,
      })

      expect(popperInjection.triggerRef.value).toStrictEqual
    })
  })

  describe('can attach handlers', () => {
    it('should be able to attach handlers to the trigger', async () => {
      const onClick = vi.fn()
      const virtualRef = document.createElement('div')
      wrapper = mountTrigger({
        onClick,
        virtualTriggering: true,
        virtualRef,
      })
      await nextTick()
      expect(onClick).not.toHaveBeenCalled()
      const evt = new MouseEvent('click')
      virtualRef.dispatchEvent(evt)
      await nextTick()
      expect(onClick).toHaveBeenCalled()
    })
    it('should cleanup listeners when triggerRef changes', async () => {
      const onClick = vi.fn()
      const first = document.createElement('p')
      const removeSpy = vi.spyOn(first, 'removeEventListener')
      const addSpy = vi.spyOn(first, 'addEventListener')

      wrapper = mountTrigger({
        onClick,
        virtualTriggering: true,
        virtualRef: first,
      })
      await nextTick()

      await wrapper.setProps({
        virtualRef: {
          getBoundingClientRect: () => {
            return { top: 0, left: 0, right: 0, bottom: 0, width: 0, height: 0 }
          },
        },
      })
      await nextTick()

      expect(addSpy).toHaveBeenCalledWith('click', onClick, false)
      expect(removeSpy).toHaveBeenCalledWith('click', onClick, false)
    })
  })