Skip to content

Commit

Permalink
Fixed react unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriztiaan committed Oct 10, 2024
1 parent a061d32 commit 41ae097
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions packages/react/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as commonSdk from '@powersync/common';
import { renderHook, waitFor } from '@testing-library/react';
import { cleanup, renderHook, waitFor } from '@testing-library/react';
import React from 'react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { PowerSyncContext } from '../src/hooks/PowerSyncContext';
import { useQuery } from '../src/hooks/useQuery';

Expand All @@ -12,16 +12,17 @@ const mockPowerSync = {
})),
resolveTables: vi.fn(() => ['table1', 'table2']),
onChangeWithCallback: vi.fn(),
getAll: vi.fn(() => ['list1', 'list2'])
getAll: vi.fn(() => Promise.resolve(['list1', 'list2']))
};

vi.mock('./PowerSyncContext', () => ({
useContext: vi.fn(() => mockPowerSync)
}));

describe('useQuery', () => {
afterEach(() => {
vi.resetAllMocks();
beforeEach(() => {
vi.clearAllMocks();
cleanup(); // Cleanup the DOM after each test
});

it('should error when PowerSync is not set', async () => {
Expand All @@ -48,14 +49,15 @@ describe('useQuery', () => {
);

const { result } = renderHook(() => useQuery('SELECT * from lists', [], { runQueryOnce: true }), { wrapper });
const currentResult = result.current;
expect(currentResult.isLoading).toEqual(true);
expect(result.current.isLoading).toEqual(true);

waitFor(
await waitFor(
async () => {
const currentResult = result.current;
expect(currentResult.isLoading).toEqual(false);
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);
},
Expand All @@ -69,13 +71,14 @@ describe('useQuery', () => {
);

const { result } = renderHook(() => useQuery('SELECT * from lists', [], { runQueryOnce: true }), { wrapper });
const currentResult = result.current;
expect(currentResult.isLoading).toEqual(true);

expect(result.current.isLoading).toEqual(true);

let refresh;

waitFor(
await waitFor(
async () => {
const currentResult = result.current;
refresh = currentResult.refresh;
expect(currentResult.isLoading).toEqual(false);
expect(mockPowerSync.getAll).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -105,11 +108,10 @@ describe('useQuery', () => {
);

const { result } = renderHook(() => useQuery('SELECT * from lists', [], { runQueryOnce: true }), { wrapper });
const currentResult = result.current;

waitFor(
await waitFor(
async () => {
expect(currentResult.error).toEqual(Error('PowerSync failed to fetch data: some error'));
expect(result.current.error).toEqual(Error('PowerSync failed to fetch data: some error'));
},
{ timeout: 100 }
);
Expand All @@ -133,11 +135,10 @@ describe('useQuery', () => {
);

const { result } = renderHook(() => useQuery('SELECT * from lists', []), { wrapper });
const currentResult = result.current;

waitFor(
await waitFor(
async () => {
expect(currentResult.error).toEqual(Error('PowerSync failed to fetch data: some error'));
expect(result.current.error).toEqual(Error('PowerSync failed to fetch data: some error'));
},
{ timeout: 100 }
);
Expand Down Expand Up @@ -173,13 +174,13 @@ describe('useQuery', () => {
});
});

// The test returns unhandled errors when run with all the others.
// TODO: Fix the test so that there are no unhandled errors (this may be a vitest or @testing-library/react issue)
it.skip('should show an error if parsing the query results in an error', async () => {
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').mockReturnValue(Error('error') as any);
vi.spyOn(commonSdk, 'parseQuery').mockImplementation(() => {
throw new Error('error');
});

const { result } = renderHook(
() =>
Expand All @@ -189,10 +190,10 @@ describe('useQuery', () => {
}),
{ wrapper }
);
const currentResult = result.current;

waitFor(
await waitFor(
async () => {
const currentResult = result.current;
expect(currentResult.isLoading).toEqual(false);
expect(currentResult.isFetching).toEqual(false);
expect(currentResult.data).toEqual([]);
Expand Down

0 comments on commit 41ae097

Please sign in to comment.