github.com/thanos-io/thanos@v0.32.5/pkg/ui/react-app/src/pages/targets/ScrapePoolList.test.tsx (about) 1 import * as React from 'react'; 2 import { mount, ReactWrapper } from 'enzyme'; 3 import { act } from 'react-dom/test-utils'; 4 import { UncontrolledAlert } from 'reactstrap'; 5 import { sampleApiResponse } from './__testdata__/testdata'; 6 import ScrapePoolList, { ScrapePoolPanel } from './ScrapePoolList'; 7 import { Target } from './target'; 8 import { FetchMock } from 'jest-fetch-mock/types'; 9 10 describe('ScrapePoolList', () => { 11 const defaultProps = { 12 pathPrefix: '..', 13 }; 14 15 beforeEach(() => { 16 fetchMock.resetMocks(); 17 }); 18 19 describe('when data is returned', () => { 20 let scrapePoolList: ReactWrapper; 21 let mock: FetchMock; 22 beforeEach(() => { 23 //Tooltip requires DOM elements to exist. They do not in enzyme rendering so we must manually create them. 24 const scrapePools: { [key: string]: number } = { blackbox: 3, node_exporter: 1, 'prometheus/test': 1 }; 25 Object.keys(scrapePools).forEach((pool: string): void => { 26 Array.from(Array(scrapePools[pool]).keys()).forEach((idx: number): void => { 27 const div = document.createElement('div'); 28 div.id = `series-labels-${pool}-${idx}`; 29 document.body.appendChild(div); 30 }); 31 }); 32 mock = fetchMock.mockResponse(JSON.stringify(sampleApiResponse)); 33 }); 34 35 it('renders a table', async () => { 36 await act(async () => { 37 scrapePoolList = mount(<ScrapePoolList {...defaultProps} />); 38 }); 39 scrapePoolList.update(); 40 expect(mock).toHaveBeenCalledWith('../api/v1/targets?state=active', { cache: 'no-store', credentials: 'same-origin' }); 41 const panels = scrapePoolList.find(ScrapePoolPanel); 42 expect(panels).toHaveLength(3); 43 const activeTargets: Target[] = sampleApiResponse.data.activeTargets as unknown as Target[]; 44 activeTargets.forEach(({ scrapePool }: Target) => { 45 const panel = scrapePoolList.find(ScrapePoolPanel).filterWhere((panel) => panel.prop('scrapePool') === scrapePool); 46 expect(panel).toHaveLength(1); 47 }); 48 }); 49 }); 50 51 describe('when an error is returned', () => { 52 it('displays an alert', async () => { 53 const mock = fetchMock.mockReject(new Error('Error fetching targets')); 54 55 let scrapePoolList: any; 56 await act(async () => { 57 scrapePoolList = mount(<ScrapePoolList {...defaultProps} />); 58 }); 59 scrapePoolList.update(); 60 61 expect(mock).toHaveBeenCalledWith('../api/v1/targets?state=active', { cache: 'no-store', credentials: 'same-origin' }); 62 const alert = scrapePoolList.find(UncontrolledAlert); 63 expect(alert.prop('color')).toBe('danger'); 64 expect(alert.text()).toContain('Error fetching targets'); 65 }); 66 }); 67 });