From 03915a25809cc4befe13b740fb1393d98f284b87 Mon Sep 17 00:00:00 2001 From: Viki Date: Thu, 24 Oct 2024 15:05:39 +0800 Subject: [PATCH] test: add more test cases --- .../src/use-element-bounding/index.test.ts | 46 ++++++++++++++++++- .../src/use-element-by-point/index.test.ts | 7 +++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/packages/react-use/src/use-element-bounding/index.test.ts b/packages/react-use/src/use-element-bounding/index.test.ts index 8b4ad2f..4a6262e 100644 --- a/packages/react-use/src/use-element-bounding/index.test.ts +++ b/packages/react-use/src/use-element-bounding/index.test.ts @@ -1,8 +1,52 @@ -import { describe, expect, it } from 'vitest' +import { renderHook } from '@/test' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { useElementBounding } from './index' describe('useElementBounding', () => { + let div: HTMLDivElement + + beforeEach(() => { + div = document.createElement('div') + div.style.width = '200px' + div.style.height = '100px' + document.body.appendChild(div) + }) + + afterEach(() => { + div.remove() + }) + it('should defined', () => { expect(useElementBounding).toBeDefined() }) + + it('should return initial value', () => { + const { result } = renderHook(() => useElementBounding(null)) + + expect(result.current[0].x).toBe(0) + expect(result.current[0].y).toBe(0) + expect(result.current[0].width).toBe(0) + expect(result.current[0].height).toBe(0) + expect(result.current[0].left).toBe(0) + expect(result.current[0].top).toBe(0) + expect(result.current[0].right).toBe(0) + expect(result.current[0].bottom).toBe(0) + expect(result.current[1]).toBeInstanceOf(Function) + }) + + // for now, jsdom/happy-dom not support `MutationObserver` + + // it('should return bounding value', () => { + // const { result } = renderHook(() => useElementBounding(div)) + + // expect(result.current[0].x).toBe(0) + // expect(result.current[0].y).toBe(0) + // expect(result.current[0].width).toBe(0) + // expect(result.current[0].height).toBe(0) + // expect(result.current[0].left).toBe(0) + // expect(result.current[0].top).toBe(0) + // expect(result.current[0].right).toBe(0) + // expect(result.current[0].bottom).toBe(0) + // expect(result.current[1]).toBeInstanceOf(Function) + // }) }) diff --git a/packages/react-use/src/use-element-by-point/index.test.ts b/packages/react-use/src/use-element-by-point/index.test.ts index 3e9a870..9d08a36 100644 --- a/packages/react-use/src/use-element-by-point/index.test.ts +++ b/packages/react-use/src/use-element-by-point/index.test.ts @@ -1,3 +1,4 @@ +import { renderHook } from '@/test' import { describe, expect, it } from 'vitest' import { useElementByPoint } from './index' @@ -5,4 +6,10 @@ describe('useElementByPoint', () => { it('should defined', () => { expect(useElementByPoint).toBeDefined() }) + + it('should return initial value', () => { + const { result } = renderHook(() => useElementByPoint({ x: 0, y: 0 })) + expect(result.current.element).toBe(null) + expect(result.current.isSupported).toBe(false) + }) })