The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/alerting/unified/useRouteGroupsMatcher.test.tsx

40 lines
1.4 KiB

import { renderHook } from '@testing-library/react';
import * as comlink from 'comlink';
import { createWorker } from './createRouteGroupsMatcherWorker';
import { useRouteGroupsMatcher } from './useRouteGroupsMatcher';
jest.mock('./createRouteGroupsMatcherWorker');
jest.mock('comlink');
const createWorkerMock = jest.mocked(createWorker);
const wrapMock = jest.mocked(comlink.wrap);
beforeEach(() => {
createWorkerMock.mockReset();
wrapMock.mockReset();
});
describe('useRouteGroupsMatcher', () => {
it('should load web worker if the feature flag is enabled', function () {
const { result } = renderHook(() => useRouteGroupsMatcher());
expect(createWorkerMock).toHaveBeenCalledTimes(1);
expect(wrapMock).toHaveBeenCalledTimes(1);
expect(result.current.getRouteGroupsMap).toBeDefined();
});
it('getMatchedRouteGroups should throw error if loading worker failed', async function () {
createWorkerMock.mockImplementation(() => {
throw new DOMException('Failed to load worker');
});
const { result } = renderHook(() => useRouteGroupsMatcher());
expect(createWorkerMock).toHaveBeenCalledTimes(1);
expect(wrapMock).toHaveBeenCalledTimes(0); // When loading worker failed we shouldn't call wrap
expect(async () => {
await result.current.getRouteGroupsMap({ id: '1' }, []);
}).rejects.toThrowError(Error);
});
});