|
| 1 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 2 | +import useContributors from '../useContributors'; |
| 3 | + |
| 4 | +beforeEach(() => { |
| 5 | + global.fetch = jest.fn(); |
| 6 | + process.env.REACT_APP_PLAY_API_URL = 'https://api.test.com'; |
| 7 | +}); |
| 8 | + |
| 9 | +afterEach(() => { |
| 10 | + jest.restoreAllMocks(); |
| 11 | + delete process.env.REACT_APP_PLAY_API_URL; |
| 12 | +}); |
| 13 | + |
| 14 | +const mockContributors = [ |
| 15 | + { login: 'user1', type: 'User', contributions: 50 }, |
| 16 | + { login: 'dependabot', type: 'Bot', contributions: 100 }, |
| 17 | + { login: 'user2', type: 'User', contributions: 30 }, |
| 18 | + { login: 'user3', type: 'User', contributions: 80 } |
| 19 | +]; |
| 20 | + |
| 21 | +describe('useContributors', () => { |
| 22 | + it('fetches contributors and filters out bots', async () => { |
| 23 | + global.fetch.mockResolvedValueOnce({ |
| 24 | + json: () => Promise.resolve(mockContributors) |
| 25 | + }); |
| 26 | + |
| 27 | + const { result } = renderHook(() => useContributors(false)); |
| 28 | + |
| 29 | + expect(result.current.isLoading).toBe(true); |
| 30 | + |
| 31 | + await waitFor(() => { |
| 32 | + expect(result.current.isLoading).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + expect(result.current.data).toHaveLength(3); |
| 36 | + expect(result.current.data.every((c) => c.type !== 'Bot')).toBe(true); |
| 37 | + expect(result.current.error).toBeUndefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('sorts contributors by contributions when sorted=true', async () => { |
| 41 | + global.fetch.mockResolvedValueOnce({ |
| 42 | + json: () => Promise.resolve(mockContributors) |
| 43 | + }); |
| 44 | + |
| 45 | + const { result } = renderHook(() => useContributors(true)); |
| 46 | + |
| 47 | + await waitFor(() => { |
| 48 | + expect(result.current.isLoading).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + const contributions = result.current.data.map((c) => c.contributions); |
| 52 | + expect(contributions).toEqual([80, 50, 30]); // descending order, bot excluded |
| 53 | + }); |
| 54 | + |
| 55 | + it('sets error on fetch failure', async () => { |
| 56 | + const mockError = new Error('Network error'); |
| 57 | + global.fetch.mockRejectedValueOnce(mockError); |
| 58 | + |
| 59 | + const { result } = renderHook(() => useContributors(false)); |
| 60 | + |
| 61 | + await waitFor(() => { |
| 62 | + expect(result.current.isLoading).toBe(false); |
| 63 | + }); |
| 64 | + |
| 65 | + expect(result.current.error).toBe(mockError); |
| 66 | + expect(result.current.data).toBeUndefined(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('fetches from the correct API URL', async () => { |
| 70 | + global.fetch.mockResolvedValueOnce({ |
| 71 | + json: () => Promise.resolve([]) |
| 72 | + }); |
| 73 | + |
| 74 | + renderHook(() => useContributors(false)); |
| 75 | + |
| 76 | + expect(global.fetch).toHaveBeenCalledWith('https://api.test.com/react-play/contributors'); |
| 77 | + }); |
| 78 | +}); |
0 commit comments