forked from TanStack/db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.test-d.ts
More file actions
197 lines (170 loc) · 6.37 KB
/
collection.test-d.ts
File metadata and controls
197 lines (170 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { assertType, describe, expectTypeOf, it } from "vitest"
import { z } from "zod"
import { createCollection } from "../src/collection"
import type { CollectionImpl } from "../src/collection"
import type { OperationConfig, ResolveType } from "../src/types"
import type { StandardSchemaV1 } from "@standard-schema/spec"
describe(`Collection.update type tests`, () => {
type TypeTestItem = { id: string; value: number; optional?: boolean }
const updateMethod: CollectionImpl<TypeTestItem>[`update`] = (() => {}) as any // Dummy assignment for type checking
it(`should correctly type drafts for multi-item update with callback (Overload 1)`, () => {
updateMethod([`id1`, `id2`], (drafts) => {
expectTypeOf(drafts).toEqualTypeOf<Array<TypeTestItem>>()
// @ts-expect-error - This line should error because drafts is an array, not a single item.
assertType<TypeTestItem>(drafts)
})
})
it(`should correctly type drafts for multi-item update with config and callback (Overload 2)`, () => {
const config: OperationConfig = { metadata: { test: true } }
updateMethod([`id1`, `id2`], config, (drafts) => {
expectTypeOf(drafts).toEqualTypeOf<Array<TypeTestItem>>()
// @ts-expect-error - This line should error.
assertType<TypeTestItem>(drafts)
})
})
it(`should correctly type draft for single-item update with callback (Overload 3)`, () => {
updateMethod(`id1`, (draft) => {
expectTypeOf(draft).toEqualTypeOf<TypeTestItem>()
// @ts-expect-error - This line should error because draft is a single item, not an array.
assertType<Array<TypeTestItem>>(draft)
})
})
it(`should correctly type draft for single-item update with config and callback (Overload 4)`, () => {
const config: OperationConfig = { metadata: { test: true } }
updateMethod(`id1`, config, (draft) => {
expectTypeOf(draft).toEqualTypeOf<TypeTestItem>()
// @ts-expect-error - This line should error.
assertType<Array<TypeTestItem>>(draft)
})
})
})
describe(`Collection type resolution tests`, () => {
// Define test types
type ExplicitType = { id: string; explicit: boolean }
type FallbackType = { id: string; fallback: boolean }
const testSchema = z.object({
id: z.string(),
schema: z.boolean(),
})
type SchemaType = StandardSchemaV1.InferOutput<typeof testSchema>
type ItemOf<T> = T extends Array<infer U> ? U : T
it(`should use explicit type when provided without schema`, () => {
const _collection = createCollection<ExplicitType>({
getKey: (item) => item.id,
sync: { sync: () => {} },
})
type Param = Parameters<typeof _collection.insert>[0]
expectTypeOf<ItemOf<Param>>().toEqualTypeOf<ExplicitType>()
})
it(`should use schema type when explicit type is not provided`, () => {
const _collection = createCollection<
unknown,
string,
{},
typeof testSchema
>({
getKey: (item) => item.id,
sync: { sync: () => {} },
schema: testSchema,
})
type ExpectedType = ResolveType<unknown, typeof testSchema, FallbackType>
type Param = Parameters<typeof _collection.insert>[0]
expectTypeOf<ItemOf<Param>>().toEqualTypeOf<SchemaType>()
expectTypeOf<ExpectedType>().toEqualTypeOf<SchemaType>()
})
it(`should use fallback type when neither explicit nor schema type is provided`, () => {
const _collection = createCollection<
unknown,
string,
{},
never,
FallbackType
>({
getKey: (item) => item.id,
sync: { sync: () => {} },
})
type ExpectedType = ResolveType<unknown, never, FallbackType>
type Param = Parameters<typeof _collection.insert>[0]
expectTypeOf<ItemOf<Param>>().toEqualTypeOf<FallbackType>()
expectTypeOf<ExpectedType>().toEqualTypeOf<FallbackType>()
})
it(`should correctly resolve type with all three types provided`, () => {
// Explicit type should win
const _collection = createCollection<
ExplicitType,
string,
{},
typeof testSchema,
FallbackType
>({
getKey: (item) => item.id,
sync: { sync: () => {} },
schema: testSchema,
})
type ExpectedType = ResolveType<
ExplicitType,
typeof testSchema,
FallbackType
>
type Param = Parameters<typeof _collection.insert>[0]
expectTypeOf<ItemOf<Param>>().toEqualTypeOf<ExplicitType>()
expectTypeOf<ExpectedType>().toEqualTypeOf<ExplicitType>()
})
it(`should automatically infer type from schema without generic arguments`, () => {
// This is the key test case that was missing - no generic arguments at all
const _collection = createCollection({
getKey: (item) => item.id,
sync: { sync: () => {} },
schema: testSchema,
})
type Param = Parameters<typeof _collection.insert>[0]
// Should infer the schema type automatically
expectTypeOf<ItemOf<Param>>().toEqualTypeOf<SchemaType>()
})
it(`should automatically infer type from Zod schema with optional fields`, () => {
// Test with a Zod schema that has optional fields
const userSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email().optional(),
created_at: z.date().optional(),
})
const _collection = createCollection({
getKey: (item) => item.id,
sync: { sync: () => {} },
schema: userSchema,
})
type Param = Parameters<typeof _collection.insert>[0]
type ExpectedType = {
id: number
name: string
email?: string
created_at?: Date
}
// Should automatically infer the complete Zod schema type
expectTypeOf<ItemOf<Param>>().toEqualTypeOf<ExpectedType>()
})
it(`should automatically infer type from Zod schema with nullable fields`, () => {
// Test with nullable fields (different from optional)
const postSchema = z.object({
id: z.string(),
title: z.string(),
author_id: z.string().nullable(),
published_at: z.date().nullable(),
})
const _collection = createCollection({
getKey: (item) => item.id,
sync: { sync: () => {} },
schema: postSchema,
})
type Param = Parameters<typeof _collection.insert>[0]
type ExpectedType = {
id: string
title: string
author_id: string | null
published_at: Date | null
}
// Should automatically infer nullable types correctly
expectTypeOf<ItemOf<Param>>().toEqualTypeOf<ExpectedType>()
})
})