|
| 1 | +import * as commonSdk from '@powersync/common'; |
| 2 | +import { cleanup, renderHook, waitFor } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { PowerSyncContext } from '@powersync/react/'; |
| 6 | +import { useQuery } from '../src/hooks/useQuery'; |
| 7 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 8 | + |
| 9 | +const mockPowerSync = { |
| 10 | + currentStatus: { status: 'initial' }, |
| 11 | + registerListener: vi.fn(() => { }), |
| 12 | + resolveTables: vi.fn(() => ['table1', 'table2']), |
| 13 | + onChangeWithCallback: vi.fn(), |
| 14 | + getAll: vi.fn(() => Promise.resolve(['list1', 'list2'])) |
| 15 | +}; |
| 16 | + |
| 17 | +vi.mock('./PowerSyncContext', () => ({ |
| 18 | + useContext: vi.fn(() => mockPowerSync) |
| 19 | +})); |
| 20 | + |
| 21 | +describe('useQuery', () => { |
| 22 | + let queryClient = new QueryClient({ |
| 23 | + defaultOptions: { |
| 24 | + queries: { |
| 25 | + retry: false, |
| 26 | + }, |
| 27 | + } |
| 28 | + }) |
| 29 | + |
| 30 | + const wrapper = ({ children }) => ( |
| 31 | + <QueryClientProvider client={queryClient}> |
| 32 | + <PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider> |
| 33 | + </QueryClientProvider> |
| 34 | + ); |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + queryClient.clear(); |
| 38 | + |
| 39 | + vi.clearAllMocks(); |
| 40 | + cleanup(); // Cleanup the DOM after each test |
| 41 | + }); |
| 42 | + |
| 43 | + |
| 44 | + it('should set loading states on initial load', async () => { |
| 45 | + const { result } = renderHook(() => useQuery({ |
| 46 | + queryKey: ['lists'], |
| 47 | + query: 'SELECT * from lists' |
| 48 | + }), { wrapper }); |
| 49 | + const currentResult = result.current; |
| 50 | + expect(currentResult.isLoading).toEqual(true); |
| 51 | + expect(currentResult.isFetching).toEqual(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should execute string queries', async () => { |
| 55 | + const query = () => |
| 56 | + useQuery({ |
| 57 | + queryKey: ['lists'], |
| 58 | + query: "SELECT * from lists" |
| 59 | + }); |
| 60 | + const { result } = renderHook(query, { wrapper }); |
| 61 | + |
| 62 | + await vi.waitFor(() => { |
| 63 | + expect(result.current.data![0]).toEqual('list1'); |
| 64 | + expect(result.current.data![1]).toEqual('list2'); |
| 65 | + }, { timeout: 500 }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should set error during query execution', async () => { |
| 69 | + const mockPowerSyncError = { |
| 70 | + currentStatus: { status: 'initial' }, |
| 71 | + registerListener: vi.fn(() => { }), |
| 72 | + onChangeWithCallback: vi.fn(), |
| 73 | + resolveTables: vi.fn(() => ['table1', 'table2']), |
| 74 | + getAll: vi.fn(() => { |
| 75 | + throw new Error('some error'); |
| 76 | + }) |
| 77 | + }; |
| 78 | + |
| 79 | + const wrapper = ({ children }) => ( |
| 80 | + <QueryClientProvider client={queryClient}> |
| 81 | + <PowerSyncContext.Provider value={mockPowerSyncError as any}>{children}</PowerSyncContext.Provider> |
| 82 | + </QueryClientProvider> |
| 83 | + ); |
| 84 | + |
| 85 | + const { result } = renderHook(() => useQuery({ |
| 86 | + queryKey: ['lists'], |
| 87 | + query: 'SELECT * from lists' |
| 88 | + }), { wrapper }); |
| 89 | + |
| 90 | + await waitFor( |
| 91 | + async () => { |
| 92 | + expect(result.current.error).toEqual(Error('some error')); |
| 93 | + }, |
| 94 | + { timeout: 100 } |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should execute compatible queries', async () => { |
| 99 | + const compilableQuery = { |
| 100 | + execute: () => [{ test: 'custom' }] as any, |
| 101 | + compile: () => ({ sql: 'SELECT * from lists' }) |
| 102 | + } as commonSdk.CompilableQuery<any>; |
| 103 | + |
| 104 | + const query = () => |
| 105 | + useQuery({ |
| 106 | + queryKey: ['lists'], |
| 107 | + query: compilableQuery |
| 108 | + }); |
| 109 | + const { result } = renderHook(query, { wrapper }); |
| 110 | + |
| 111 | + await vi.waitFor(() => { |
| 112 | + expect(result.current.data![0].test).toEqual('custom'); |
| 113 | + }, { timeout: 500 }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should show an error if parsing the query results in an error', async () => { |
| 117 | + const compilableQuery = { |
| 118 | + execute: () => [] as any, |
| 119 | + compile: () => ({ sql: 'SELECT * from lists', parameters: ['param'] }) |
| 120 | + } as commonSdk.CompilableQuery<any>; |
| 121 | + |
| 122 | + const { result } = renderHook( |
| 123 | + () => |
| 124 | + useQuery({ |
| 125 | + queryKey: ['lists'], |
| 126 | + query: compilableQuery, |
| 127 | + parameters: ['redundant param'] |
| 128 | + }), |
| 129 | + { wrapper } |
| 130 | + ); |
| 131 | + |
| 132 | + await waitFor( |
| 133 | + async () => { |
| 134 | + const currentResult = result.current; |
| 135 | + expect(currentResult.isLoading).toEqual(false); |
| 136 | + expect(currentResult.isFetching).toEqual(false); |
| 137 | + expect(currentResult.error).toEqual(Error('You cannot pass parameters to a compiled query.')); |
| 138 | + expect(currentResult.data).toBeUndefined() |
| 139 | + }, |
| 140 | + { timeout: 100 } |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | +}); |
0 commit comments