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

collation.suite.ts

146 lines4.5 KB

1/**
2 * Collation Test Suite
3 *
4 * Tests string collation configuration and behavior
5 */
6​
7import { describe, expect, it } from 'vitest'
8import { createLiveQueryCollection, eq } from '@tanstack/db'
9import { waitForQueryData } from '../utils/helpers'
10import type { E2ETestConfig } from '../types'
11​
12export function createCollationTestSuite(
13 getConfig: () => Promise<E2ETestConfig>,
14) {
15 describe(`Collation Suite`, () => {
16 describe(`Default Collation`, () => {
17 it(`should use default collation for string comparisons`, async () => {
18 const config = await getConfig()
19 const usersCollection = config.collections.onDemand.users
20​
21 const query = createLiveQueryCollection((q) =>
22 q
23 .from({ user: usersCollection })
24 .orderBy(({ user }) => user.name, `asc`),
25 )
26​
27 await query.preload()
28 await waitForQueryData(query, { minSize: 1 })
29​
30 const results = Array.from(query.state.values())
31 expect(results.length).toBeGreaterThan(0)
32​
33 await query.cleanup()
34 })
35​
36 it(`should handle case-sensitive comparisons by default`, async () => {
37 const config = await getConfig()
38 const usersCollection = config.collections.onDemand.users
39​
40 // Different case variations should be treated as different
41 const query = createLiveQueryCollection(
42 (q) =>
43 q
44 .from({ user: usersCollection })
45 .where(({ user }) => eq(user.name, `alice 0`)), // lowercase
46 )
47​
48 await query.preload()
49​
50 const results = Array.from(query.state.values())
51 // Should NOT match because seed data has "Alice 0" (capitalized) and default is case-sensitive
52 expect(results.length).toBe(0)
53​
54 await query.cleanup()
55 })
56 })
57​
58 describe(`Custom Collection-Level Collation`, () => {
59 it(`should use custom defaultStringCollation at collection level`, async () => {
60 const config = await getConfig()
61​
62 // Test will use collection with custom collation if provided
63 const query = createLiveQueryCollection({
64 query: (q) => q.from({ user: config.collections.onDemand.users }),
65 defaultStringCollation: {
66 stringSort: `lexical`,
67 },
68 })
69​
70 await query.preload()
71​
72 expect(query.compareOptions.stringSort).toBe(`lexical`)
73​
74 await query.cleanup()
75 })
76​
77 it(`should support locale-based collation`, async () => {
78 const config = await getConfig()
79​
80 const query = createLiveQueryCollection({
81 query: (q) => q.from({ user: config.collections.onDemand.users }),
82 defaultStringCollation: {
83 stringSort: `locale`,
84 locale: `de-DE`,
85 },
86 })
87​
88 await query.preload()
89​
90 expect(query.compareOptions.stringSort).toBe(`locale`)
91 // Type narrow: when stringSort is 'locale', locale property exists
92 if (query.compareOptions.stringSort === `locale`) {
93 expect(query.compareOptions.locale).toBe(`de-DE`)
94 }
95​
96 await query.cleanup()
97 })
98 })
99​
100 describe(`Query-Level Collation Override`, () => {
101 it(`should override collection collation at query level`, async () => {
102 const config = await getConfig()
103​
104 const query = createLiveQueryCollection({
105 query: (q) => q.from({ user: config.collections.onDemand.users }),
106 defaultStringCollation: {
107 stringSort: `lexical`,
108 },
109 })
110​
111 await query.preload()
112​
113 expect(query.compareOptions.stringSort).toBe(`lexical`)
114​
115 await query.cleanup()
116 })
117 })
118​
119 describe(`Collation in OrderBy`, () => {
120 it(`should respect collation when sorting strings`, async () => {
121 const config = await getConfig()
122 const usersCollection = config.collections.onDemand.users
123​
124 const query = createLiveQueryCollection((q) =>
125 q
126 .from({ user: usersCollection })
127 .orderBy(({ user }) => user.name, `asc`),
128 )
129​
130 await query.preload()
131 await waitForQueryData(query, { minSize: 1 })
132​
133 const results = Array.from(query.state.values())
134 expect(results.length).toBeGreaterThan(0)
135​
136 // Verify sorted (actual sort order depends on collation)
137 for (let i = 1; i < results.length; i++) {
138 // Just verify it's sorted, regardless of collation
139 expect(results[i - 1]!.name).toBeTruthy()
140 }
141​
142 await query.cleanup()
143 })
144 })
145 })