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/plugins/extensions/errorHandling.test.ts

117 lines
3.8 KiB

import { AppPluginExtensionLink } from '@grafana/data';
import { handleErrorsInConfigure, handleErrorsInHandler } from './errorHandling';
import type { CommandHandlerFunc, ConfigureFunc } from './types';
describe('error handling for extensions', () => {
describe('error handling for configure', () => {
const pluginId = 'grafana-basic-app';
const errorHandler = handleErrorsInConfigure<AppPluginExtensionLink>({
pluginId: pluginId,
title: 'Go to page one',
logger: jest.fn(),
});
const context = {};
it('should return configured link if configure is successful', () => {
const configureWithErrorHandling = errorHandler(() => {
return {
title: 'This is a new title',
};
});
const configured = configureWithErrorHandling(context);
expect(configured).toEqual({
title: 'This is a new title',
});
});
it('should return undefined if configure throws error', () => {
const configureWithErrorHandling = errorHandler(() => {
throw new Error();
});
const configured = configureWithErrorHandling(context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure is promise/async-based', () => {
const promisebased = (async () => {}) as ConfigureFunc<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(promisebased);
const configured = configureWithErrorHandling(context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure is not a function', () => {
const objectbased = {} as ConfigureFunc<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(objectbased);
const configured = configureWithErrorHandling(context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure returns other than an object', () => {
const returnString = (() => '') as ConfigureFunc<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(returnString);
const configured = configureWithErrorHandling(context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure returns undefined', () => {
const returnUndefined = () => undefined;
const configureWithErrorHandling = errorHandler(returnUndefined);
const configured = configureWithErrorHandling(context);
expect(configured).toBeUndefined();
});
});
describe('error handling for command handler', () => {
const pluginId = 'grafana-basic-app';
const errorHandler = handleErrorsInHandler({
pluginId: pluginId,
title: 'open modal',
logger: jest.fn(),
});
it('should be called successfully when handler is a normal synchronous function', () => {
const handler = jest.fn();
const handlerWithErrorHandling = errorHandler(handler);
handlerWithErrorHandling();
expect(handler).toBeCalled();
});
it('should not error out even if the handler throws an error', () => {
const handlerWithErrorHandling = errorHandler(() => {
throw new Error();
});
expect(handlerWithErrorHandling).not.toThrowError();
});
it('should be called successfully when handler is an async function / promise', () => {
const promisebased = (async () => {}) as CommandHandlerFunc;
const configureWithErrorHandling = errorHandler(promisebased);
expect(configureWithErrorHandling).not.toThrowError();
});
it('should be called successfully when handler is not a function', () => {
const objectbased = {} as CommandHandlerFunc;
const configureWithErrorHandling = errorHandler(objectbased);
expect(configureWithErrorHandling).not.toThrowError();
});
});
});