Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add test fixtures & implement testDatasource for tag #9

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ process.env.TZ = 'UTC';
module.exports = {
// Jest configuration provided by Grafana scaffolding
...require('./.config/jest.config'),
// Clear mocks before every test
clearMocks: true
};
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dev": "webpack -w -c ./.config/webpack/webpack.config.ts --env development",
"test": "jest --watch --onlyChanged",
"test:ci": "jest --passWithNoTests --maxWorkers 4",
"test:template": "npx plop datasource Test && npx tsc --noEmit && rimraf src/datasources/test",
mure marked this conversation as resolved.
Show resolved Hide resolved
"lint": "eslint --cache --ignore-path ./.gitignore --ext .js,.jsx,.ts,.tsx .",
"lint:fix": "npm run lint -- --fix",
"e2e": "npm exec cypress install && npm exec grafana-e2e run",
Expand Down Expand Up @@ -47,6 +48,7 @@
"identity-obj-proxy": "3.0.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jest-mock-extended": "^3.0.4",
"plop": "^3.1.2",
"prettier": "^2.8.7",
"replace-in-file-webpack-plugin": "^1.0.6",
Expand Down
26 changes: 26 additions & 0 deletions src/datasources/tag/TagDataSource.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { MockProxy } from 'jest-mock-extended';
import { TagDataSource } from './TagDataSource';
import { createDataSource, createFetchError } from 'test/fixtures';
import { BackendSrv } from '@grafana/runtime';

let ds: TagDataSource, backendSrv: MockProxy<BackendSrv>;

beforeEach(() => {
[ds, backendSrv] = createDataSource(TagDataSource);
});

describe('testDatasource', () => {
test('returns success', async () => {
backendSrv.get.calledWith('/nitag/v2/tags-count').mockResolvedValue(25);

const result = await ds.testDatasource();

expect(result.status).toEqual('success');
});

test('bubbles up exception', async () => {
backendSrv.get.calledWith('/nitag/v2/tags-count').mockRejectedValue(createFetchError(400));

await expect(ds.testDatasource()).rejects.toHaveProperty('status', 400);
});
});
15 changes: 8 additions & 7 deletions src/datasources/tag/TagDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import {
DataSourceApi,
DataSourceInstanceSettings,
MutableDataFrame,
FieldType,
FieldType
} from '@grafana/data';

import { TestingStatus, getBackendSrv } from '@grafana/runtime';
import { BackendSrv, TestingStatus, getBackendSrv } from '@grafana/runtime';

import { TagQuery } from './types';

export class TagDataSource extends DataSourceApi<TagQuery> {
baseUrl: string;
constructor(private instanceSettings: DataSourceInstanceSettings) {
constructor(
private instanceSettings: DataSourceInstanceSettings,
private backendSrv: BackendSrv = getBackendSrv()
) {
super(instanceSettings);
// TODO: set base path of the service
this.baseUrl = this.instanceSettings.url + '/nifoo/v2';
this.baseUrl = this.instanceSettings.url + '/nitag/v2';
}

async query(options: DataQueryRequest<TagQuery>): Promise<DataQueryResponse> {
Expand All @@ -39,8 +41,7 @@ export class TagDataSource extends DataSourceApi<TagQuery> {
}

async testDatasource(): Promise<TestingStatus> {
// TODO: Implement a health and authentication check
await getBackendSrv().get(this.baseUrl + '/bar');
await this.backendSrv.get(this.baseUrl + '/tags-count');
return { status: 'success', message: 'Data source connected and authentication successful!' };
}
}
22 changes: 22 additions & 0 deletions src/test/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DataSourceInstanceSettings } from '@grafana/data';
import { BackendSrv, FetchError } from '@grafana/runtime';
import { mock, MockProxy } from 'jest-mock-extended';

export function createDataSource<T>(
ctor: new (instanceSettings: DataSourceInstanceSettings, backendSrv: BackendSrv) => T
): [T, MockProxy<BackendSrv>] {
const mockBackendSrv = mock<BackendSrv>(
{},
{
fallbackMockImplementation: () => {
throw new Error('Unexpected request');
},
}
);
const ds = new ctor({ url: '' } as DataSourceInstanceSettings, mockBackendSrv);
return [ds, mockBackendSrv];
}

export function createFetchError(status: number): FetchError {
return mock<FetchError>({ status });
}
9 changes: 6 additions & 3 deletions templates/datasource/{{Name}}DataSource.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import {
FieldType,
} from '@grafana/data';

import { TestingStatus, getBackendSrv } from '@grafana/runtime';
import { BackendSrv, TestingStatus, getBackendSrv } from '@grafana/runtime';

import { {{Name}}Query } from './types';

export class {{Name}}DataSource extends DataSourceApi<{{Name}}Query> {
baseUrl: string;
constructor(private instanceSettings: DataSourceInstanceSettings) {
constructor(
private instanceSettings: DataSourceInstanceSettings,
private backendSrv: BackendSrv = getBackendSrv()
) {
super(instanceSettings);
// TODO: set base path of the service
this.baseUrl = this.instanceSettings.url + '/nifoo/v2';
Expand All @@ -40,7 +43,7 @@ export class {{Name}}DataSource extends DataSourceApi<{{Name}}Query> {

async testDatasource(): Promise<TestingStatus> {
// TODO: Implement a health and authentication check
await getBackendSrv().get(this.baseUrl + '/bar');
await this.backendSrv.get(this.baseUrl + '/bar');
return { status: 'success', message: 'Data source connected and authentication successful!' };
}
}