-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathuseQuery.test.tsx
200 lines (168 loc) · 6.62 KB
/
useQuery.test.tsx
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
198
199
200
import * as commonSdk from '@powersync/common';
import { cleanup, renderHook, waitFor } from '@testing-library/react';
import React from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { PowerSyncContext } from '../src/hooks/PowerSyncContext';
import { useQuery } from '../src/hooks/useQuery';
const mockPowerSync = {
currentStatus: { status: 'initial' },
registerListener: vi.fn(() => {}),
resolveTables: vi.fn(() => ['table1', 'table2']),
onChangeWithCallback: vi.fn(),
getAll: vi.fn(() => Promise.resolve(['list1', 'list2']))
};
vi.mock('./PowerSyncContext', () => ({
useContext: vi.fn(() => mockPowerSync)
}));
describe('useQuery', () => {
beforeEach(() => {
vi.clearAllMocks();
cleanup(); // Cleanup the DOM after each test
});
it('should error when PowerSync is not set', async () => {
const { result } = renderHook(() => useQuery('SELECT * from lists'));
const currentResult = result.current;
expect(currentResult.error).toEqual(Error('PowerSync not configured.'));
expect(currentResult.isLoading).toEqual(false);
expect(currentResult.data).toEqual([]);
});
it('should set isLoading to true on initial load', async () => {
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider>
);
const { result } = renderHook(() => useQuery('SELECT * from lists'), { wrapper });
const currentResult = result.current;
expect(currentResult.isLoading).toEqual(true);
});
it('should run the query once if runQueryOnce flag is set', async () => {
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider>
);
const { result } = renderHook(() => useQuery('SELECT * from lists', [], { runQueryOnce: true }), { wrapper });
expect(result.current.isLoading).toEqual(true);
await waitFor(
async () => {
const currentResult = result.current;
expect(currentResult.data).toEqual(['list1', 'list2']);
expect(currentResult.isLoading).toEqual(false);
expect(currentResult.isFetching).toEqual(false);
expect(mockPowerSync.onChangeWithCallback).not.toHaveBeenCalled();
expect(mockPowerSync.getAll).toHaveBeenCalledTimes(1);
},
{ timeout: 100 }
);
});
it('should rerun the query when refresh is used', async () => {
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider>
);
const { result } = renderHook(() => useQuery('SELECT * from lists', [], { runQueryOnce: true }), { wrapper });
expect(result.current.isLoading).toEqual(true);
let refresh;
await waitFor(
async () => {
const currentResult = result.current;
refresh = currentResult.refresh;
expect(currentResult.isLoading).toEqual(false);
expect(mockPowerSync.getAll).toHaveBeenCalledTimes(1);
},
{ timeout: 100 }
);
await refresh();
expect(mockPowerSync.getAll).toHaveBeenCalledTimes(2);
});
it('should set error when error occurs and runQueryOnce flag is set', async () => {
const mockPowerSyncError = {
currentStatus: { status: 'initial' },
registerListener: vi.fn(() => {}),
onChangeWithCallback: vi.fn(),
resolveTables: vi.fn(() => ['table1', 'table2']),
getAll: vi.fn(() => {
throw new Error('some error');
})
};
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSyncError as any}>{children}</PowerSyncContext.Provider>
);
const { result } = renderHook(() => useQuery('SELECT * from lists', [], { runQueryOnce: true }), { wrapper });
await waitFor(
async () => {
expect(result.current.error).toEqual(Error('PowerSync failed to fetch data: some error'));
},
{ timeout: 100 }
);
});
it('should set error when error occurs', async () => {
const mockPowerSyncError = {
currentStatus: { status: 'initial' },
registerListener: vi.fn(() => {}),
onChangeWithCallback: vi.fn(),
resolveTables: vi.fn(() => ['table1', 'table2']),
getAll: vi.fn(() => {
throw new Error('some error');
})
};
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSyncError as any}>{children}</PowerSyncContext.Provider>
);
const { result } = renderHook(() => useQuery('SELECT * from lists', []), { wrapper });
await waitFor(
async () => {
expect(result.current.error).toEqual(Error('PowerSync failed to fetch data: some error'));
},
{ timeout: 100 }
);
});
it('should accept compilable queries', async () => {
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider>
);
const { result } = renderHook(
() => useQuery({ execute: () => [] as any, compile: () => ({ sql: 'SELECT * from lists', parameters: [] }) }),
{ wrapper }
);
const currentResult = result.current;
expect(currentResult.isLoading).toEqual(true);
});
it('should execute compatible queries', async () => {
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider>
);
const query = () =>
useQuery({
execute: () => [{ test: 'custom' }] as any,
compile: () => ({ sql: 'SELECT * from lists', parameters: [] })
});
const { result } = renderHook(query, { wrapper });
await vi.waitFor(() => {
expect(result.current.data[0]?.test).toEqual('custom');
});
});
it('should show an error if parsing the query results in an error', async () => {
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider>
);
vi.spyOn(commonSdk, 'parseQuery').mockImplementation(() => {
throw new Error('error');
});
const { result } = renderHook(
() =>
useQuery({
execute: () => [] as any,
compile: () => ({ sql: 'SELECT * from lists', parameters: ['param'] })
}),
{ wrapper }
);
await waitFor(
async () => {
const currentResult = result.current;
expect(currentResult.isLoading).toEqual(false);
expect(currentResult.isFetching).toEqual(false);
expect(currentResult.data).toEqual([]);
expect(currentResult.error).toEqual(Error('error'));
},
{ timeout: 100 }
);
});
// TODO: Add tests for powersync.onChangeWithCallback path
});