11import * as commonSdk from '@powersync/common' ;
2- import { renderHook , waitFor } from '@testing-library/react' ;
2+ import { cleanup , renderHook , waitFor } from '@testing-library/react' ;
33import React from 'react' ;
4- import { afterEach , describe , expect , it , vi } from 'vitest' ;
4+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
55import { PowerSyncContext } from '../src/hooks/PowerSyncContext' ;
66import { useQuery } from '../src/hooks/useQuery' ;
77
@@ -12,16 +12,17 @@ const mockPowerSync = {
1212 } ) ) ,
1313 resolveTables : vi . fn ( ( ) => [ 'table1' , 'table2' ] ) ,
1414 onChangeWithCallback : vi . fn ( ) ,
15- getAll : vi . fn ( ( ) => [ 'list1' , 'list2' ] )
15+ getAll : vi . fn ( ( ) => Promise . resolve ( [ 'list1' , 'list2' ] ) )
1616} ;
1717
1818vi . mock ( './PowerSyncContext' , ( ) => ( {
1919 useContext : vi . fn ( ( ) => mockPowerSync )
2020} ) ) ;
2121
2222describe ( 'useQuery' , ( ) => {
23- afterEach ( ( ) => {
24- vi . resetAllMocks ( ) ;
23+ beforeEach ( ( ) => {
24+ vi . clearAllMocks ( ) ;
25+ cleanup ( ) ; // Cleanup the DOM after each test
2526 } ) ;
2627
2728 it ( 'should error when PowerSync is not set' , async ( ) => {
@@ -48,14 +49,14 @@ describe('useQuery', () => {
4849 ) ;
4950
5051 const { result } = renderHook ( ( ) => useQuery ( 'SELECT * from lists' , [ ] , { runQueryOnce : true } ) , { wrapper } ) ;
51- const currentResult = result . current ;
52- expect ( currentResult . isLoading ) . toEqual ( true ) ;
52+ expect ( result . current . isLoading ) . toEqual ( true ) ;
5353
54- waitFor (
54+ await waitFor (
5555 async ( ) => {
56- expect ( currentResult . isLoading ) . toEqual ( false ) ;
56+ const currentResult = result . current ;
5757 expect ( currentResult . data ) . toEqual ( [ 'list1' , 'list2' ] ) ;
5858 expect ( currentResult . isLoading ) . toEqual ( false ) ;
59+ expect ( currentResult . isFetching ) . toEqual ( false ) ;
5960 expect ( mockPowerSync . onChangeWithCallback ) . not . toHaveBeenCalled ( ) ;
6061 expect ( mockPowerSync . getAll ) . toHaveBeenCalledTimes ( 1 ) ;
6162 } ,
@@ -69,13 +70,14 @@ describe('useQuery', () => {
6970 ) ;
7071
7172 const { result } = renderHook ( ( ) => useQuery ( 'SELECT * from lists' , [ ] , { runQueryOnce : true } ) , { wrapper } ) ;
72- const currentResult = result . current ;
73- expect ( currentResult . isLoading ) . toEqual ( true ) ;
73+
74+ expect ( result . current . isLoading ) . toEqual ( true ) ;
7475
7576 let refresh ;
7677
77- waitFor (
78+ await waitFor (
7879 async ( ) => {
80+ const currentResult = result . current ;
7981 refresh = currentResult . refresh ;
8082 expect ( currentResult . isLoading ) . toEqual ( false ) ;
8183 expect ( mockPowerSync . getAll ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -105,11 +107,10 @@ describe('useQuery', () => {
105107 ) ;
106108
107109 const { result } = renderHook ( ( ) => useQuery ( 'SELECT * from lists' , [ ] , { runQueryOnce : true } ) , { wrapper } ) ;
108- const currentResult = result . current ;
109110
110- waitFor (
111+ await waitFor (
111112 async ( ) => {
112- expect ( currentResult . error ) . toEqual ( Error ( 'PowerSync failed to fetch data: some error' ) ) ;
113+ expect ( result . current . error ) . toEqual ( Error ( 'PowerSync failed to fetch data: some error' ) ) ;
113114 } ,
114115 { timeout : 100 }
115116 ) ;
@@ -133,11 +134,10 @@ describe('useQuery', () => {
133134 ) ;
134135
135136 const { result } = renderHook ( ( ) => useQuery ( 'SELECT * from lists' , [ ] ) , { wrapper } ) ;
136- const currentResult = result . current ;
137137
138- waitFor (
138+ await waitFor (
139139 async ( ) => {
140- expect ( currentResult . error ) . toEqual ( Error ( 'PowerSync failed to fetch data: some error' ) ) ;
140+ expect ( result . current . error ) . toEqual ( Error ( 'PowerSync failed to fetch data: some error' ) ) ;
141141 } ,
142142 { timeout : 100 }
143143 ) ;
@@ -173,13 +173,13 @@ describe('useQuery', () => {
173173 } ) ;
174174 } ) ;
175175
176- // The test returns unhandled errors when run with all the others.
177- // TODO: Fix the test so that there are no unhandled errors (this may be a vitest or @testing-library/react issue)
178- it . skip ( 'should show an error if parsing the query results in an error' , async ( ) => {
176+ it ( 'should show an error if parsing the query results in an error' , async ( ) => {
179177 const wrapper = ( { children } ) => (
180178 < PowerSyncContext . Provider value = { mockPowerSync as any } > { children } </ PowerSyncContext . Provider >
181179 ) ;
182- vi . spyOn ( commonSdk , 'parseQuery' ) . mockReturnValue ( Error ( 'error' ) as any ) ;
180+ vi . spyOn ( commonSdk , 'parseQuery' ) . mockImplementation ( ( ) => {
181+ throw new Error ( 'error' ) ;
182+ } ) ;
183183
184184 const { result } = renderHook (
185185 ( ) =>
@@ -189,10 +189,10 @@ describe('useQuery', () => {
189189 } ) ,
190190 { wrapper }
191191 ) ;
192- const currentResult = result . current ;
193192
194- waitFor (
193+ await waitFor (
195194 async ( ) => {
195+ const currentResult = result . current ;
196196 expect ( currentResult . isLoading ) . toEqual ( false ) ;
197197 expect ( currentResult . isFetching ) . toEqual ( false ) ;
198198 expect ( currentResult . data ) . toEqual ( [ ] ) ;
0 commit comments