Commit 7efdf065 authored by tom's avatar tom

fix jest tests

parent b65ae1b5
...@@ -41,3 +41,15 @@ global.console = { ...@@ -41,3 +41,15 @@ global.console = {
consoleError(...args); 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'; 'use client';
import { ChakraProvider } from '@chakra-ui/react'; import { ChakraProvider } from '@chakra-ui/react';
import React from 'react';
import theme from 'toolkit/theme/theme'; import theme from 'toolkit/theme/theme';
......
import { act } from 'react';
import { renderHook, wrapper } from 'jest/lib'; import { renderHook, wrapper } from 'jest/lib';
import useRatings from './useRatings'; import useRatings from './useRatings';
...@@ -5,7 +7,11 @@ import useRatings from './useRatings'; ...@@ -5,7 +7,11 @@ import useRatings from './useRatings';
const useAccount = jest.fn(); const useAccount = jest.fn();
const useApiQuery = 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('wagmi', () => ({ useAccount: () => useAccount() }));
jest.mock('lib/api/useApiQuery', () => () => useApiQuery()); 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 ...@@ -19,7 +25,12 @@ it('should set canRate to true if address is defined and transactions_count is 5
isPlaceholderData: false, isPlaceholderData: false,
data: { transactions_count: 5 }, data: { transactions_count: 5 },
}); });
const { result } = renderHook(() => useRatings(), { wrapper }); const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(true); expect(result.current.canRate).toBe(true);
}); });
...@@ -29,7 +40,12 @@ it('should set canRate to undefined if address is undefined', async() => { ...@@ -29,7 +40,12 @@ it('should set canRate to undefined if address is undefined', async() => {
isPlaceholderData: false, isPlaceholderData: false,
data: { transactions_count: 5 }, data: { transactions_count: 5 },
}); });
const { result } = renderHook(() => useRatings(), { wrapper }); const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(undefined); expect(result.current.canRate).toBe(undefined);
}); });
...@@ -39,7 +55,12 @@ it('should set canRate to false if transactions_count is less than 5', async() = ...@@ -39,7 +55,12 @@ it('should set canRate to false if transactions_count is less than 5', async() =
isPlaceholderData: false, isPlaceholderData: false,
data: { transactions_count: 4 }, data: { transactions_count: 4 },
}); });
const { result } = renderHook(() => useRatings(), { wrapper }); const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(false); expect(result.current.canRate).toBe(false);
}); });
...@@ -49,7 +70,12 @@ it('should set canRate to false if isPlaceholderData is true', async() => { ...@@ -49,7 +70,12 @@ it('should set canRate to false if isPlaceholderData is true', async() => {
isPlaceholderData: true, isPlaceholderData: true,
data: { transactions_count: 5 }, data: { transactions_count: 5 },
}); });
const { result } = renderHook(() => useRatings(), { wrapper }); const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(false); expect(result.current.canRate).toBe(false);
}); });
...@@ -59,7 +85,12 @@ it('should set canRate to false if data is undefined', async() => { ...@@ -59,7 +85,12 @@ it('should set canRate to false if data is undefined', async() => {
isPlaceholderData: false, isPlaceholderData: false,
data: undefined, data: undefined,
}); });
const { result } = renderHook(() => useRatings());
const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(false); expect(result.current.canRate).toBe(false);
}); });
...@@ -69,6 +100,11 @@ it('should set canRate to false if transactions_count is undefined', async() => ...@@ -69,6 +100,11 @@ it('should set canRate to false if transactions_count is undefined', async() =>
isPlaceholderData: false, isPlaceholderData: false,
data: {}, data: {},
}); });
const { result } = renderHook(() => useRatings());
const { result } = renderHook(() => useRatings(), { wrapper });
await act(async() => {
await Promise.resolve();
});
expect(result.current.canRate).toBe(false); 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