|
1 | 1 | import * as commonSdk from '@powersync/common';
|
| 2 | +import { toCompilableQuery, wrapPowerSyncWithDrizzle } from '@powersync/drizzle-driver'; |
2 | 3 | import { PowerSyncDatabase } from '@powersync/web';
|
3 | 4 | import { act, cleanup, renderHook, waitFor } from '@testing-library/react';
|
| 5 | +import { eq } from 'drizzle-orm'; |
| 6 | +import { sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
4 | 7 | import pDefer from 'p-defer';
|
5 | 8 | import React, { useEffect } from 'react';
|
6 | 9 | import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest';
|
@@ -168,6 +171,69 @@ describe('useQuery', () => {
|
168 | 171 | );
|
169 | 172 | });
|
170 | 173 |
|
| 174 | + it('should react to updated queries (Explicit Drizzle DB)', async () => { |
| 175 | + const db = openPowerSync(); |
| 176 | + |
| 177 | + const lists = sqliteTable('lists', { |
| 178 | + id: text('id'), |
| 179 | + name: text('name') |
| 180 | + }); |
| 181 | + |
| 182 | + const drizzleDb = wrapPowerSyncWithDrizzle(db, { |
| 183 | + schema: { |
| 184 | + lists |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | + const wrapper = ({ children }) => <PowerSyncContext.Provider value={db}>{children}</PowerSyncContext.Provider>; |
| 189 | + |
| 190 | + let updateParameters = (params: string): void => {}; |
| 191 | + const newParametersPromise = new Promise<string>((resolve) => { |
| 192 | + updateParameters = resolve; |
| 193 | + }); |
| 194 | + |
| 195 | + await db.execute(/* sql */ ` |
| 196 | + INSERT INTO |
| 197 | + lists (id, name) |
| 198 | + VALUES |
| 199 | + (uuid (), 'first'), |
| 200 | + (uuid (), 'second') |
| 201 | + `); |
| 202 | + |
| 203 | + const query = () => { |
| 204 | + const [name, setName] = React.useState<string>('first'); |
| 205 | + const drizzleQuery = drizzleDb.select().from(lists).where(eq(lists.name, name)); |
| 206 | + |
| 207 | + useEffect(() => { |
| 208 | + // allow updating the parameters externally |
| 209 | + newParametersPromise.then((params) => setName(params)); |
| 210 | + }, []); |
| 211 | + |
| 212 | + return useQuery(toCompilableQuery(drizzleQuery)); |
| 213 | + }; |
| 214 | + |
| 215 | + const { result } = renderHook(query, { wrapper }); |
| 216 | + |
| 217 | + // We should only receive the first list due to the WHERE clause |
| 218 | + await vi.waitFor( |
| 219 | + () => { |
| 220 | + expect(result.current.data[0]?.name).toEqual('first'); |
| 221 | + }, |
| 222 | + { timeout: 500, interval: 100 } |
| 223 | + ); |
| 224 | + |
| 225 | + // Now update the parameter |
| 226 | + updateParameters('second'); |
| 227 | + |
| 228 | + // We should now only receive the second list due to the WHERE clause and updated parameter |
| 229 | + await vi.waitFor( |
| 230 | + () => { |
| 231 | + expect(result.current.data[0]?.name).toEqual('second'); |
| 232 | + }, |
| 233 | + { timeout: 500, interval: 100 } |
| 234 | + ); |
| 235 | + }); |
| 236 | + |
171 | 237 | it('should react to updated queries', async () => {
|
172 | 238 | const db = openPowerSync();
|
173 | 239 |
|
|
0 commit comments