123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import { of } from 'rxjs';
- import { setBackendSrv } from '@grafana/runtime';
- import { BackendSrv } from 'app/core/services/backend_srv';
- import { ContextSrv, User } from '../../../core/services/context_srv';
- import { GraphiteDatasource } from './datasource';
- interface Context {
- ds: GraphiteDatasource;
- }
- describe('graphiteDatasource integration with backendSrv and fetch', () => {
- let ctx = {} as Context;
- beforeEach(() => {
- jest.clearAllMocks();
- const instanceSettings = {
- url: '/api/datasources/proxy/1',
- name: 'graphiteProd',
- jsonData: {
- rollupIndicatorEnabled: true,
- },
- };
- const ds = new GraphiteDatasource(instanceSettings);
- ctx = { ds };
- });
- describe('returns a list of functions', () => {
- it('should return a list of functions with invalid JSON', async () => {
- const INVALID_JSON =
- '{"testFunction":{"name":"function","description":"description","module":"graphite.render.functions","group":"Transform","params":[{"name":"param","type":"intOrInf","required":true,"default":Infinity}]}}';
- mockBackendSrv(INVALID_JSON);
- const funcDefs = await ctx.ds.getFuncDefs();
- expect(funcDefs).toEqual({
- testFunction: {
- category: 'Transform',
- defaultParams: ['inf'],
- description: 'description',
- fake: true,
- name: 'function',
- params: [
- {
- multiple: false,
- name: 'param',
- optional: false,
- options: undefined,
- type: 'int_or_infinity',
- },
- ],
- },
- });
- });
- it('should return a list of functions with valid JSON', async () => {
- const VALID_JSON =
- '{"testFunction":{"name":"function","description":"description","module":"graphite.render.functions","group":"Transform","params":[{"name":"param","type":"intOrInf","required":true,"default":1e9999}]}}';
- mockBackendSrv(VALID_JSON);
- const funcDefs = await ctx.ds.getFuncDefs();
- expect(funcDefs).toEqual({
- testFunction: {
- category: 'Transform',
- defaultParams: ['inf'],
- description: 'description',
- fake: true,
- name: 'function',
- params: [
- {
- multiple: false,
- name: 'param',
- optional: false,
- options: undefined,
- type: 'int_or_infinity',
- },
- ],
- },
- });
- });
- });
- });
- function mockBackendSrv(data: string) {
- const defaults = {
- data: '',
- ok: true,
- status: 200,
- statusText: 'Ok',
- isSignedIn: true,
- orgId: 1337,
- redirected: false,
- type: 'basic',
- url: 'http://localhost:3000/api/some-mock',
- };
- const props = { ...defaults };
- props.data = data;
- const textMock = jest.fn().mockResolvedValue(props.data);
- const fromFetchMock = jest.fn().mockImplementation(() => {
- const mockedResponse = {
- ok: props.ok,
- status: props.status,
- statusText: props.statusText,
- text: textMock,
- redirected: false,
- type: 'basic',
- url: 'http://localhost:3000/api/some-mock',
- headers: {
- method: 'GET',
- url: '/functions',
- // to work around Graphite returning invalid JSON
- responseType: 'text',
- },
- };
- return of(mockedResponse);
- });
- const appEventsMock = {} as any;
- const user: User = {
- isSignedIn: props.isSignedIn,
- orgId: props.orgId,
- } as any as User;
- const contextSrvMock: ContextSrv = {
- user,
- } as any as ContextSrv;
- const logoutMock = jest.fn();
- const mockedBackendSrv = new BackendSrv({
- fromFetch: fromFetchMock,
- appEvents: appEventsMock,
- contextSrv: contextSrvMock,
- logout: logoutMock,
- });
- setBackendSrv(mockedBackendSrv);
- }
|