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

infinite-query-render-cuts-support.tsx

55 lines1.5 KB

1import { Component } from 'react'
2import { createCollection } from '@tanstack/db'
3import { mockSyncCollectionOptions } from '../../db/tests/utils'
4import type { ReactNode } from 'react'
5​
6// Shared fixtures for the infinite query render-cut tests. The laws are owned by
7// infinite-query-render-cuts-oracle.test.tsx, and the ordinary pinned tests live in
8// infinite-query-render-cuts.test.tsx.
9export type Row = { id: string; label: string; rank: number }
10​
11export const cleanups: Array<() => Promise<void> | void> = []
12let sequence = 0
13export function nextSequence(): number {
14 return sequence++
16​
17export const PAGE_SIZE = 2
18​
19export class ErrorBoundary extends Component<
20 { children: ReactNode },
21 { error?: Error }
22> {
23 state: { error?: Error } = {}
24 static getDerivedStateFromError(error: Error) {
25 return { error }
26 }
27 render() {
28 return this.state.error ? (
29 <div>Rejected: {this.state.error.message}</div>
30 ) : (
31 this.props.children
32 )
33 }
35​
36export function rows(count: number): Array<Row> {
37 return Array.from({ length: count }, (_, index) => ({
38 id: String(index + 1),
39 label: `row-${index + 1}`,
40 rank: count - index,
41 }))
43​
44export function makeSyncSource(count: number) {
45 const source = createCollection(
46 mockSyncCollectionOptions<Row>({
47 id: `infinite-render-cuts-${nextSequence()}`,
48 getKey: (row) => row.id,
49 autoIndex: `eager`,
50 initialData: rows(count),
51 }),
52 )
53 cleanups.push(() => source.cleanup())
54 return source