fix jest tests

This commit is contained in:
tom
2025-03-31 20:59:24 +02:00
parent be9c12550e
commit 3ad62fefde
3 changed files with 52 additions and 3 deletions
+12
View File
@@ -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;
}
};
}
+1
View File
@@ -1,6 +1,7 @@
'use client';
import { ChakraProvider } from '@chakra-ui/react';
import React from 'react';
import theme from 'toolkit/theme/theme';
+39 -3
View File
@@ -1,3 +1,5 @@
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);
});