useTenants.test.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { describe, it, expect, vi, beforeEach } from 'vitest';
  2. import { renderHook, waitFor } from '@testing-library/react';
  3. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  4. import { useTenants } from './useTenants';
  5. import { tenantClient } from '../api/tenantClient';
  6. // Mock the API client
  7. vi.mock('../api/tenantClient', () => ({
  8. tenantClient: {
  9. $get: vi.fn(),
  10. $post: vi.fn(),
  11. ':id': {
  12. $put: vi.fn(),
  13. $delete: vi.fn()
  14. }
  15. }
  16. }));
  17. const createWrapper = () => {
  18. const queryClient = new QueryClient({
  19. defaultOptions: {
  20. queries: { retry: false },
  21. mutations: { retry: false }
  22. }
  23. });
  24. return ({ children }: { children: React.ReactNode }) => (
  25. <QueryClientProvider client={queryClient}>
  26. {children}
  27. </QueryClientProvider>
  28. );
  29. };
  30. describe('useTenants', () => {
  31. beforeEach(() => {
  32. vi.clearAllMocks();
  33. });
  34. it('should fetch tenants successfully', async () => {
  35. const mockResponse = {
  36. data: [
  37. { id: 1, name: 'Tenant 1', code: 'tenant1', status: 1 },
  38. { id: 2, name: 'Tenant 2', code: 'tenant2', status: 2 }
  39. ],
  40. pagination: { total: 2, page: 1, pageSize: 10 }
  41. };
  42. (tenantClient.$get as any).mockResolvedValue({
  43. status: 200,
  44. json: async () => mockResponse
  45. });
  46. const { result } = renderHook(() => useTenants(), {
  47. wrapper: createWrapper()
  48. });
  49. await waitFor(() => expect(result.current.isLoading).toBe(false));
  50. expect(result.current.data).toEqual(mockResponse);
  51. expect(tenantClient.$get).toHaveBeenCalledWith({
  52. query: {
  53. page: 1,
  54. pageSize: 10,
  55. keyword: '',
  56. filters: undefined
  57. }
  58. });
  59. });
  60. it('should handle fetch error', async () => {
  61. (tenantClient.$get as any).mockResolvedValue({
  62. status: 500,
  63. json: async () => ({ error: 'Internal Server Error' })
  64. });
  65. const { result } = renderHook(() => useTenants(), {
  66. wrapper: createWrapper()
  67. });
  68. await waitFor(() => expect(result.current.isError).toBe(true));
  69. });
  70. it('should use custom options', async () => {
  71. const mockResponse = {
  72. data: [],
  73. pagination: { total: 0, page: 2, pageSize: 20 }
  74. };
  75. (tenantClient.$get as any).mockResolvedValue({
  76. status: 200,
  77. json: async () => mockResponse
  78. });
  79. const { result } = renderHook(
  80. () => useTenants({
  81. page: 2,
  82. pageSize: 20,
  83. keyword: 'test',
  84. filters: { status: 1 }
  85. }),
  86. {
  87. wrapper: createWrapper()
  88. }
  89. );
  90. await waitFor(() => expect(result.current.isLoading).toBe(false));
  91. expect(tenantClient.$get).toHaveBeenCalledWith({
  92. query: {
  93. page: 2,
  94. pageSize: 20,
  95. keyword: 'test',
  96. filters: JSON.stringify({ status: 1 })
  97. }
  98. });
  99. });
  100. });