Commit 7efdf065 authored by tom's avatar tom

fix jest tests

parent b65ae1b5
......@@ -41,3 +41,15 @@ global.console = {
consoleError(...args);
},
};
// Polyfill for structuredClone
if (typeof structuredClone === 'undefined') {
global.structuredClone = <T>(obj: T): T => {
try {
return JSON.parse(JSON.stringify(obj)) as T;
} catch (error) {
// Fallback for circular references and other special cases
return obj;
}
};
}
'use client';
import { ChakraProvider } from '@chakra-ui/react';
import React from 'react';
import theme from 'toolkit/theme/theme';
......
import { act } from 'react';
import { renderHook, wrapper } from 'jest/lib';
import useRatings from './useRatings';
......@@ -5,7 +7,11 @@ import useRatings from './useRatings';
const useAccount = jest.fn();
const useApiQuery = jest.fn();
jest.mock('lib/hooks/useToast', () => jest.fn());
jest.mock('toolkit/chakra/toaster', () => ({
toaster: {
error: jest.fn(),
},
}));
jest.mock('wagmi', () => ({ useAccount: () => useAccount() }));
jest.mock('lib/api/useApiQuery', () => () => useApiQuery());
......@@ -19,7 +25,12 @@ it('should set canRate to true if address is defined and transactions_count is 5
isPlaceholderData: false,
data: { transactions_count: 5 },
});
const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(true);
});
......@@ -29,7 +40,12 @@ it('should set canRate to undefined if address is undefined', async() => {
isPlaceholderData: false,
data: { transactions_count: 5 },
});
const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(undefined);
});
......@@ -39,7 +55,12 @@ it('should set canRate to false if transactions_count is less than 5', async() =
isPlaceholderData: false,
data: { transactions_count: 4 },
});
const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(false);
});
......@@ -49,7 +70,12 @@ it('should set canRate to false if isPlaceholderData is true', async() => {
isPlaceholderData: true,
data: { transactions_count: 5 },
});
const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(false);
});
......@@ -59,7 +85,12 @@ it('should set canRate to false if data is undefined', async() => {
isPlaceholderData: false,
data: undefined,
});
const { result } = renderHook(() => useRatings());
const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(false);
});
......@@ -69,6 +100,11 @@ it('should set canRate to false if transactions_count is undefined', async() =>
isPlaceholderData: false,
data: {},
});
const { result } = renderHook(() => useRatings());
const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(false);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment