useAdvisorSignals.test.tsx79 lines · main
| 1 | import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 2 | import { act, renderHook, waitFor } from '@testing-library/react' |
| 3 | import { PropsWithChildren } from 'react' |
| 4 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | |
| 6 | import { useAdvisorSignals } from './useAdvisorSignals' |
| 7 | |
| 8 | const { mockUseBannedIPsQuery } = vi.hoisted(() => ({ |
| 9 | mockUseBannedIPsQuery: vi.fn(), |
| 10 | })) |
| 11 | |
| 12 | vi.mock('@/data/banned-ips/banned-ips-query', () => ({ |
| 13 | useBannedIPsQuery: mockUseBannedIPsQuery, |
| 14 | })) |
| 15 | |
| 16 | const createWrapper = () => { |
| 17 | const queryClient = new QueryClient({ |
| 18 | defaultOptions: { |
| 19 | queries: { |
| 20 | retry: false, |
| 21 | }, |
| 22 | }, |
| 23 | }) |
| 24 | |
| 25 | return function Wrapper({ children }: PropsWithChildren) { |
| 26 | return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | describe('useAdvisorSignals', () => { |
| 31 | beforeEach(() => { |
| 32 | window.localStorage.clear() |
| 33 | vi.clearAllMocks() |
| 34 | }) |
| 35 | |
| 36 | it('resurfaces a dismissed banned-IP signal after the IP disappears and is banned again', async () => { |
| 37 | let bannedIPs = ['203.0.113.10'] |
| 38 | |
| 39 | mockUseBannedIPsQuery.mockImplementation(() => ({ |
| 40 | data: { |
| 41 | banned_ipv4_addresses: bannedIPs, |
| 42 | }, |
| 43 | isPending: false, |
| 44 | isError: false, |
| 45 | })) |
| 46 | |
| 47 | const { result, rerender } = renderHook( |
| 48 | () => useAdvisorSignals({ projectRef: 'project-ref' }), |
| 49 | { |
| 50 | wrapper: createWrapper(), |
| 51 | } |
| 52 | ) |
| 53 | |
| 54 | expect(result.current.data).toHaveLength(1) |
| 55 | |
| 56 | act(() => { |
| 57 | result.current.dismissSignal('signal:banned-ip:203.0.113.10:v1') |
| 58 | }) |
| 59 | |
| 60 | await waitFor(() => { |
| 61 | expect(result.current.data).toEqual([]) |
| 62 | }) |
| 63 | |
| 64 | bannedIPs = [] |
| 65 | rerender() |
| 66 | |
| 67 | await waitFor(() => { |
| 68 | expect(window.localStorage.getItem('advisor-signal-dismissals:project-ref')).toBe('[]') |
| 69 | }) |
| 70 | |
| 71 | bannedIPs = ['203.0.113.10'] |
| 72 | rerender() |
| 73 | |
| 74 | await waitFor(() => { |
| 75 | expect(result.current.data).toHaveLength(1) |
| 76 | expect(result.current.data[0].sourceData.ip).toBe('203.0.113.10') |
| 77 | }) |
| 78 | }) |
| 79 | }) |