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

utils.ts

56 lines1.2 KB

1import type { Mock } from 'vitest'
2​
3export function sleep(ms: number) {
4 return new Promise((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}: {
24 observe: Mock
25 disconnect: Mock
26}) => {
27 return class IO implements IntersectionObserver {
28 root: Document | Element | null
29 rootMargin: string
30 thresholds: Array<number>
31 constructor(
32 _cb: IntersectionObserverCallback,
33 options?: IntersectionObserverInit,
34 ) {
35 this.root = options?.root ?? null
36 this.rootMargin = options?.rootMargin ?? '0px'
37 this.thresholds = options?.threshold ?? ([0] as any)
38 }
39​
40 takeRecords(): Array<IntersectionObserverEntry> {
41 return []
42 }
43 unobserve(): void {}
44 observe(): void {
45 observe()
46 }
47 disconnect(): void {
48 disconnect()
49 }
50 }
52​
53export function getSearchParamsFromURI(uri: string) {
54 const [, paramString] = uri.split('?')
55 return new URLSearchParams(paramString)