-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
835 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useBattery } from './index' | ||
|
||
describe('useBattery', () => { | ||
it('should defined', () => { | ||
expect(useBattery).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useBreakpoints } from './index' | ||
|
||
describe('useBreakpoints', () => { | ||
it('should defined', () => { | ||
expect(useBreakpoints).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useBrowserMemory } from './index' | ||
|
||
describe('useBrowserMemory', () => { | ||
it('should defined', () => { | ||
expect(useBrowserMemory).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useClipboardItems } from './index' | ||
|
||
describe('useClipboardItems', () => { | ||
it('should defined', () => { | ||
expect(useClipboardItems).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { act, renderHook } from '@/test' | ||
import { useState } from 'react' | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { useDebouncedEffect } from './index' | ||
|
||
describe('useDebouncedEffect', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers() | ||
}) | ||
afterEach(() => { | ||
vi.useRealTimers() | ||
}) | ||
|
||
it('should defined', () => { | ||
expect(useDebouncedEffect).toBeDefined() | ||
}) | ||
|
||
it('should debounce to run effect', async () => { | ||
const runCount = { value: 0 } | ||
|
||
const result = renderHook(() => { | ||
const [count, setCount] = useState(0) | ||
|
||
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation> | ||
useDebouncedEffect( | ||
() => { | ||
runCount.value++ | ||
}, | ||
[count], | ||
{ wait: 100 }, | ||
) | ||
return [count, setCount] as const | ||
}) | ||
|
||
expect(runCount.value).toBe(0) | ||
|
||
// reset first debounce | ||
await act(async () => { | ||
await vi.advanceTimersByTimeAsync(1000) | ||
}) | ||
expect(runCount.value).toBe(1) | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => c + 1) | ||
}) | ||
await act(async () => { | ||
await vi.advanceTimersByTimeAsync(1200) | ||
}) | ||
expect(runCount.value).toBe(2) | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => c + 1) | ||
}) | ||
expect(runCount.value).toBe(2) // start debounce, not changed yet | ||
|
||
await act(async () => { | ||
await vi.advanceTimersByTimeAsync(200) | ||
}) | ||
expect(runCount.value).toBe(3) // changed | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => c + 1) | ||
}) | ||
await act(async () => { | ||
await vi.advanceTimersByTimeAsync(50) | ||
}) | ||
await act(async () => { | ||
result.result.current[1]((c) => c + 1) | ||
}) | ||
await act(async () => { | ||
await vi.advanceTimersByTimeAsync(50) | ||
}) | ||
await act(async () => { | ||
result.result.current[1]((c) => c + 1) | ||
}) | ||
await act(async () => { | ||
await vi.advanceTimersByTimeAsync(50) | ||
}) | ||
await act(async () => { | ||
result.result.current[1]((c) => c + 1) | ||
}) | ||
await act(async () => { | ||
await vi.advanceTimersByTimeAsync(10000) | ||
}) | ||
expect(runCount.value).toBe(4) // debounced | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => c + 1) | ||
result.result.current[1]((c) => c + 1) | ||
result.result.current[1]((c) => c + 1) | ||
result.result.current[1]((c) => c + 1) | ||
result.result.current[1]((c) => c + 1) | ||
}) | ||
await act(async () => { | ||
await vi.advanceTimersByTimeAsync(1000) | ||
}) | ||
expect(runCount.value).toBe(5) // debounced | ||
}) | ||
}) |
75 changes: 75 additions & 0 deletions
75
packages/react-use/src/use-deep-compare-effect/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { act, renderHook } from '@/test' | ||
import { useState } from 'react' | ||
import { describe, expect, it } from 'vitest' | ||
import { useDeepCompareEffect } from './index' | ||
|
||
describe('useDeepCompareEffect', () => { | ||
it('should defined', () => { | ||
expect(useDeepCompareEffect).toBeDefined() | ||
}) | ||
|
||
it('should run when dep changed', async () => { | ||
const runCount = { value: 0 } | ||
|
||
const result = renderHook(() => { | ||
const [value, setValue] = useState({ count: 1 }) | ||
|
||
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation> | ||
useDeepCompareEffect(() => { | ||
runCount.value++ | ||
}, [value]) | ||
return [value, setValue] as const | ||
}) | ||
|
||
expect(runCount.value).toBe(1) // first mount | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => ({ ...c, count: c.count + 1 })) | ||
}) | ||
expect(runCount.value).toBe(2) // changed! | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => ({ ...c, count: c.count + 1 })) | ||
}) | ||
expect(runCount.value).toBe(3) // changed! | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => ({ ...c, count: c.count + 1 })) | ||
}) | ||
expect(runCount.value).toBe(4) // changed! | ||
}) | ||
|
||
it('should not run when deps do not changed', () => { | ||
const runCount = { value: 0 } | ||
|
||
const result = renderHook(() => { | ||
const [value, setValue] = useState({ count: 1 }) | ||
|
||
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation> | ||
useDeepCompareEffect(() => { | ||
runCount.value++ | ||
}, [value]) | ||
return [value, setValue] as const | ||
}) | ||
|
||
expect(runCount.value).toBe(1) // first mount | ||
|
||
act(() => { | ||
result.result.current[1]((v) => v) | ||
}) | ||
|
||
expect(runCount.value).toBe(1) // not changed! | ||
|
||
act(() => { | ||
result.result.current[1]((v) => v) | ||
}) | ||
|
||
expect(runCount.value).toBe(1) // not changed! | ||
|
||
act(() => { | ||
result.result.current[1]((v) => ({ count: v.count + 1 })) | ||
}) | ||
|
||
expect(runCount.value).toBe(2) // changed! | ||
}) | ||
}) |
75 changes: 75 additions & 0 deletions
75
packages/react-use/src/use-deep-compare-layout-effect/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { act, renderHook } from '@/test' | ||
import { useState } from 'react' | ||
import { describe, expect, it } from 'vitest' | ||
import { useDeepCompareLayoutEffect } from './index' | ||
|
||
describe('useDeepCompareLayoutEffect', () => { | ||
it('should defined', () => { | ||
expect(useDeepCompareLayoutEffect).toBeDefined() | ||
}) | ||
|
||
it('should run when dep changed', async () => { | ||
const runCount = { value: 0 } | ||
|
||
const result = renderHook(() => { | ||
const [value, setValue] = useState({ count: 1 }) | ||
|
||
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation> | ||
useDeepCompareLayoutEffect(() => { | ||
runCount.value++ | ||
}, [value]) | ||
return [value, setValue] as const | ||
}) | ||
|
||
expect(runCount.value).toBe(1) // first mount | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => ({ ...c, count: c.count + 1 })) | ||
}) | ||
expect(runCount.value).toBe(2) // changed! | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => ({ ...c, count: c.count + 1 })) | ||
}) | ||
expect(runCount.value).toBe(3) // changed! | ||
|
||
await act(async () => { | ||
result.result.current[1]((c) => ({ ...c, count: c.count + 1 })) | ||
}) | ||
expect(runCount.value).toBe(4) // changed! | ||
}) | ||
|
||
it('should not run when deps do not changed', () => { | ||
const runCount = { value: 0 } | ||
|
||
const result = renderHook(() => { | ||
const [value, setValue] = useState({ count: 1 }) | ||
|
||
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation> | ||
useDeepCompareLayoutEffect(() => { | ||
runCount.value++ | ||
}, [value]) | ||
return [value, setValue] as const | ||
}) | ||
|
||
expect(runCount.value).toBe(1) // first mount | ||
|
||
act(() => { | ||
result.result.current[1]((v) => v) | ||
}) | ||
|
||
expect(runCount.value).toBe(1) // not changed! | ||
|
||
act(() => { | ||
result.result.current[1]((v) => v) | ||
}) | ||
|
||
expect(runCount.value).toBe(1) // not changed! | ||
|
||
act(() => { | ||
result.result.current[1]((v) => ({ count: v.count + 1 })) | ||
}) | ||
|
||
expect(runCount.value).toBe(2) // changed! | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useDeviceList } from './index' | ||
|
||
describe('useDeviceList', () => { | ||
it('should defined', () => { | ||
expect(useDeviceList).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useDraggable } from './index' | ||
|
||
describe('useDraggable', () => { | ||
it('should defined', () => { | ||
expect(useDraggable).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useDropZone } from './index' | ||
|
||
describe('useDropZone', () => { | ||
it('should defined', () => { | ||
expect(useDropZone).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { renderHook } from '@/test' | ||
import { describe, expect, it } from 'vitest' | ||
import { useEffectOnce } from './index' | ||
|
||
describe('useEffectOnce', () => { | ||
it('should defined', () => { | ||
expect(useEffectOnce).toBeDefined() | ||
}) | ||
|
||
it('should run effect only once', () => { | ||
const renderCont = { effect: 0, clear: 0 } | ||
|
||
const result = renderHook(() => { | ||
useEffectOnce(() => { | ||
renderCont.effect++ | ||
return () => { | ||
renderCont.clear++ | ||
} | ||
}) | ||
}) | ||
|
||
expect(renderCont.effect).toBe(1) | ||
expect(renderCont.clear).toBe(0) | ||
|
||
result.rerender() | ||
expect(renderCont.effect).toBe(1) | ||
expect(renderCont.clear).toBe(0) | ||
|
||
result.rerender() | ||
expect(renderCont.effect).toBe(1) | ||
expect(renderCont.clear).toBe(0) | ||
|
||
result.rerender() | ||
expect(renderCont.effect).toBe(1) | ||
expect(renderCont.clear).toBe(0) | ||
|
||
result.unmount() | ||
expect(renderCont.effect).toBe(1) | ||
expect(renderCont.clear).toBe(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useElementBounding } from './index' | ||
|
||
describe('useElementBounding', () => { | ||
it('should defined', () => { | ||
expect(useElementBounding).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useElementByPoint } from './index' | ||
|
||
describe('useElementByPoint', () => { | ||
it('should defined', () => { | ||
expect(useElementByPoint).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useElementSize } from './index' | ||
|
||
describe('useElementSize', () => { | ||
it('should defined', () => { | ||
expect(useElementSize).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { useElementVisibility } from './index' | ||
|
||
describe('useElementVisibility', () => { | ||
it('should defined', () => { | ||
expect(useElementVisibility).toBeDefined() | ||
}) | ||
}) |
Oops, something went wrong.