Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

utils.ts

61 lines1.3 KB

1import type { Mock } from 'vitest'
2​
3export function sleep(ms: number) {
4 return new Promise<void>((resolve) => setTimeout(resolve, ms))
5}
6​
7export function createTimer() {
8 let time = Date.now()
9​
10 return {
11 start: () => {
12 time = Date.now()
13 },
14 getTime: () => {
15 return Date.now() - time
16 },
17 }
19​
20export const getIntersectionObserverMock = ({
21 observe,
22 disconnect,
23 onCreate,
24}: {
25 observe: Mock
26 disconnect: Mock
27 onCreate?: (callback: IntersectionObserverCallback) => void
28}) => {
29 return class IO implements IntersectionObserver {
30 root: Document | Element | null
31 rootMargin: string
32 scrollMargin: string
33 thresholds: Array<number>
34 constructor(
35 _cb: IntersectionObserverCallback,
36 options?: IntersectionObserverInit,
37 ) {
38 onCreate?.(_cb)
39 this.root = options?.root ?? null
40 this.rootMargin = options?.rootMargin ?? '0px'
41 this.scrollMargin = options?.scrollMargin ?? '0px'
42 this.thresholds = options?.threshold ?? ([0] as any)
43 }
44​
45 takeRecords(): Array<IntersectionObserverEntry> {
46 return []
47 }
48 unobserve(): void {}
49 observe(): void {
50 observe()
51 }
52 disconnect(): void {
53 disconnect()
54 }
55 }
57​
58export function getSearchParamsFromURI(uri: string) {
59 const [, paramString] = uri.split('?')
60 return new URLSearchParams(paramString)