mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
116 lines
3.4 KiB
TypeScript
116 lines
3.4 KiB
TypeScript
import { useOrganizationData } from '@/hooks/pages/useOrganizationData.ts';
|
|
import { QueryProvider } from '@/providers/QueryProvider';
|
|
import { Organization } from '@/types.ts';
|
|
import { renderHook } from '@testing-library/react';
|
|
import { describe, expect, it, vi } from 'vitest';
|
|
|
|
// Use vi.hoisted to ensure mocks are applied before imports
|
|
const mockOrganizations: Organization[] = [
|
|
{
|
|
id: '1',
|
|
name: 'Org 1',
|
|
sector: 'tech',
|
|
location: { lat: 55.7887, lng: 49.1221 },
|
|
legal_form: 'LLC' as const,
|
|
description: 'A tech company',
|
|
primary_contact: {
|
|
name: 'John Doe',
|
|
email: 'john@example.com',
|
|
phone: '+71234567890',
|
|
},
|
|
company_size: 100,
|
|
founding_year: 2020,
|
|
business_focus: ['B2B'],
|
|
industries: ['software'],
|
|
tags: ['tech', 'innovation'],
|
|
verified: true,
|
|
status: 'active' as const,
|
|
needs: [],
|
|
offers: [],
|
|
},
|
|
{
|
|
id: '2',
|
|
name: 'Org 2',
|
|
sector: 'health',
|
|
location: { lat: 55.7887, lng: 49.1221 },
|
|
legal_form: 'corporation' as const,
|
|
description: 'A health company',
|
|
primary_contact: {
|
|
name: 'Jane Smith',
|
|
email: 'jane@example.com',
|
|
phone: '+79876543210',
|
|
},
|
|
company_size: 50,
|
|
founding_year: 2019,
|
|
business_focus: ['Услуги'],
|
|
industries: ['medical'],
|
|
tags: ['health', 'care'],
|
|
verified: false,
|
|
status: 'active' as const,
|
|
needs: [],
|
|
offers: [],
|
|
},
|
|
];
|
|
|
|
vi.mock('@/hooks/useOrganizations.ts', () => ({
|
|
useOrganizations: () => ({
|
|
organizations: mockOrganizations,
|
|
getOrganizationById: (id: string) => mockOrganizations.find((org) => org.id === id),
|
|
}),
|
|
}));
|
|
|
|
vi.mock('@/hooks/api', () => ({
|
|
useOrganization: (id: string | null | undefined) => ({
|
|
data: mockOrganizations.find((org) => org.id === id) ?? null,
|
|
isLoading: false,
|
|
error: null,
|
|
}),
|
|
}));
|
|
|
|
vi.mock('../../schemas/organization.ts', async (importOriginal) => {
|
|
const original = await importOriginal();
|
|
return {
|
|
...original,
|
|
organizationSchema: {
|
|
parse: (data: unknown) => data,
|
|
},
|
|
};
|
|
});
|
|
|
|
describe('useOrganizationData', () => {
|
|
it('should return organization data for a valid ID', () => {
|
|
const { result } = renderHook(() => useOrganizationData('1'), {
|
|
wrapper: QueryProvider as any,
|
|
});
|
|
expect(result.current.organization?.name).toBe('Org 1');
|
|
expect(result.current.isLoading).toBe(false);
|
|
expect(result.current.error).toBe(null);
|
|
});
|
|
|
|
it('should return undefined for an invalid ID', () => {
|
|
const { result } = renderHook(() => useOrganizationData('3'), {
|
|
wrapper: QueryProvider as any,
|
|
});
|
|
expect(result.current.organization).toBeUndefined();
|
|
expect(result.current.isLoading).toBe(false);
|
|
expect(result.current.error).toBe(null);
|
|
});
|
|
|
|
it('should handle parsing errors', async () => {
|
|
const { organizationSchema } = await import('../../schemas/organization.ts');
|
|
const mockParse = vi.fn().mockImplementation(() => {
|
|
throw new Error('Parsing failed');
|
|
});
|
|
vi.spyOn(organizationSchema, 'parse').mockImplementation(mockParse);
|
|
|
|
const { result } = renderHook(() => useOrganizationData('1'), {
|
|
wrapper: QueryProvider as any,
|
|
});
|
|
expect(result.current.organization).toBeUndefined();
|
|
expect(result.current.isLoading).toBe(false);
|
|
expect(result.current.error).toBe('Failed to parse organization data.');
|
|
|
|
vi.restoreAllMocks();
|
|
});
|
|
});
|