Files
grafana/public/app/features/alerting/unified/useRouteGroupsMatcher.test.tsx
Konrad Lalik 2f0728ac67 Alerting: Matching instances preview for notification policies (#68882)
* Basic implementation in web worker

* Move instances discovery to the worker

* Remove filtering from the worker

* Use normalized routes, use rtk query for alert groups fetching

* Reorganize matchers utilities to be available for web workers

* Move object matchers to the machers util file, rename worker

* Move worker code to a separate hook, add perf logging

* Add a mock for the web worker code, fix tests

* Fix tests warnings

* Remove notification policy feature flag

* Add normalizeRoute tests, change the regex match to test for label matching

* Move worker init to the file scope

* Simplify useAsyncFn hook

* Use CorsWorker as a workaround for web workers loading from CDN

* Use a feature flag to enable/disable worker-based preview, add worker error handling

* Add POC for react-enable working with grafana feature toggles

* Code cleanup

* Remove console error, add useRouteGroupsMatcher tests

* Fix tests mock
2023-05-30 15:15:22 +02:00

71 lines
2.5 KiB
TypeScript

import { renderHook } from '@testing-library/react';
import * as comlink from 'comlink';
import React from 'react';
import { Features } from 'react-enable';
import { FeatureDescription } from 'react-enable/dist/FeatureState';
import { createWorker } from './createRouteGroupsMatcherWorker';
import { AlertingFeature } from './features';
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 not load web worker if the feature flag is disabled', function () {
const featureFlag = getInstancePreviewFeature(false);
const { result } = renderHook(() => useRouteGroupsMatcher(), {
wrapper: ({ children }) => <Features features={[featureFlag]}>{children}</Features>,
});
expect(createWorkerMock).not.toHaveBeenCalled();
expect(wrapMock).not.toHaveBeenCalled();
expect(result.current.getRouteGroupsMap).toBeDefined();
});
it('should load web worker if the feature flag is enabled', function () {
const featureFlag = getInstancePreviewFeature(true);
const { result } = renderHook(() => useRouteGroupsMatcher(), {
wrapper: ({ children }) => <Features features={[featureFlag]}>{children}</Features>,
});
expect(createWorkerMock).toHaveBeenCalledTimes(1);
expect(wrapMock).toHaveBeenCalledTimes(1);
expect(result.current.getRouteGroupsMap).toBeDefined();
});
it('getMatchedRouteGroups should throw error if loading worker failed', async function () {
const featureFlag = getInstancePreviewFeature(true);
createWorkerMock.mockImplementation(() => {
throw new DOMException('Failed to load worker');
});
const { result } = renderHook(() => useRouteGroupsMatcher(), {
wrapper: ({ children }) => <Features features={[featureFlag]}>{children}</Features>,
});
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);
});
});
function getInstancePreviewFeature(enabled: boolean): FeatureDescription {
return {
name: AlertingFeature.NotificationPoliciesV2MatchingInstances,
defaultValue: enabled,
};
}