Browse Source

Add jest basic tests #30 (#51)

* setup jest
add basic tests to check if works

* temporary commit

* Create mocks

* Add tests setup

* Update tests

* Remove package-lock.json
master
rozetko 6 years ago committed by Alexey Velikiy
parent
commit
5aeb18ec19
  1. 1
      .gitignore
  2. 25
      __mocks__/grafana/app/core/services/backend_srv.ts
  3. 8
      __mocks__/grafana/app/core/utils/emitter.ts
  4. 20
      jest.config.js
  5. 10
      package.json
  6. 32
      tests/analyticController.jest.ts
  7. 28
      tests/setup_tests.ts
  8. 3
      tsconfig.jest.json

1
.gitignore vendored

@ -1,5 +1,6 @@
node_modules node_modules
dist dist
package-lock.json
npm-debug.log npm-debug.log
coverage/ coverage/
.aws-config.json .aws-config.json

25
__mocks__/grafana/app/core/services/backend_srv.ts

@ -0,0 +1,25 @@
export class BackendSrv {
private $http;
private alertSrv;
private $rootScope;
private $q;
private $timeout;
inFlightRequests: {};
HTTP_REQUEST_CANCELLED: number;
constructor($http: any, alertSrv: any, $rootScope: any, $q: any, $timeout: any) {}
get(url: any, params?: any) {
return Promise.resolve({ enabled: true });
}
delete(url: any) {}
post(url: any, data: any) {}
patch(url: any, data: any) {}
put(url: any, data: any) {}
requestErrorHandler(err: any) {}
request(options: any) {};
datasourceRequest(options: any) {};
loginPing() {};
search(query: any) {};
getDashboard(type: any, slug: any) {};
saveDashboard(dash: any, options: any) {};
}

8
__mocks__/grafana/app/core/utils/emitter.ts

@ -0,0 +1,8 @@
export class Emitter {
emitter: any;
constructor() {}
emit(name: any, data?: any) {}
on(name: any, handler: any, scope?: any) {}
removeAllListeners(evt?: any) {}
off(name: any, handler: any) {}
}

20
jest.config.js

@ -0,0 +1,20 @@
module.exports = {
verbose: true,
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.jest.json"
}
},
"transform": {
"\\.ts?": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(\\.|/)([jt]est)\\.[jt]s$",
"moduleFileExtensions": [
"ts",
"js",
"json"
],
"setupFiles": [
"<rootDir>/tests/setup_tests.ts"
]
};

10
package.json

@ -5,7 +5,8 @@
"main": "dist/module", "main": "dist/module",
"scripts": { "scripts": {
"build": "webpack --config build/webpack.prod.conf.js", "build": "webpack --config build/webpack.prod.conf.js",
"dev": "webpack --config build/webpack.dev.conf.js" "dev": "webpack --config build/webpack.dev.conf.js",
"test": "jest --config jest.config.js"
}, },
"keywords": [], "keywords": [],
"author": "Alexey Velikiy", "author": "Alexey Velikiy",
@ -15,22 +16,25 @@
"@types/angular": "^1.6.43", "@types/angular": "^1.6.43",
"@types/flot": "0.0.31", "@types/flot": "0.0.31",
"@types/grafana": "github:CorpGlory/types-grafana", "@types/grafana": "github:CorpGlory/types-grafana",
"@types/jest": "^23.3.0",
"@types/jquery": "^3.3.0", "@types/jquery": "^3.3.0",
"@types/lodash": "^4.14.104", "@types/lodash": "^4.14.104",
"@types/md5": "^2.1.32",
"@types/moment": "^2.13.0", "@types/moment": "^2.13.0",
"@types/perfect-scrollbar": "^1.3.0", "@types/perfect-scrollbar": "^1.3.0",
"@types/tinycolor2": "^1.4.0", "@types/tinycolor2": "^1.4.0",
"@types/md5": "^2.1.32",
"babel-core": "^6.26.0", "babel-core": "^6.26.0",
"babel-loader": "^7.1.2", "babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0", "babel-preset-env": "^1.6.0",
"copy-webpack-plugin": "^4.0.1", "copy-webpack-plugin": "^4.0.1",
"jest": "^23.4.1",
"loader-utils": "^1.1.0", "loader-utils": "^1.1.0",
"md5": "^2.2.1",
"ts-loader": "^4.2.0", "ts-loader": "^4.2.0",
"typescript": "^2.8.3", "typescript": "^2.8.3",
"webpack": "^4.7.0", "webpack": "^4.7.0",
"webpack-cli": "^2.1.2", "webpack-cli": "^2.1.2",
"md5": "^2.2.1" "ts-jest": "^22.4.6"
}, },
"dependencies": { "dependencies": {
"perfect-scrollbar": "^1.3.0", "perfect-scrollbar": "^1.3.0",

32
tests/analyticController.jest.ts

@ -0,0 +1,32 @@
import { ANALYTIC_UNIT_COLORS } from '../src/colors';
import { MetricExpanded } from '../src/models/metric';
import { DatasourceRequest } from '../src/models/datasource';
import { analyticController } from './setup_tests';
describe('AnalyticController', function () {
it('should create analytic units with colors from palette', async function () {
for (let color of ANALYTIC_UNIT_COLORS) {
analyticController.createNew();
expect(analyticController.newAnalyticUnit.color).toBe(color);
await analyticController.saveNew({} as MetricExpanded, {} as DatasourceRequest, 1);
}
});
it('should save analytic units', function () {
expect(analyticController.analyticUnits).toHaveLength(ANALYTIC_UNIT_COLORS.length);
});
it('should remove analytic unit with right id', async function () {
await analyticController.removeAnalyticUnit('2');
for (let analyticUnit of analyticController.analyticUnits) {
expect(analyticUnit.id).not.toBe('2');
}
});
it('should change color on choosing from palette', function () {
analyticController.onAnalyticUnitColorChange('1', 'red');
expect(analyticController.analyticUnits[0].color).toBe('red');
});
});

28
tests/setup_tests.ts

@ -0,0 +1,28 @@
import { AnalyticController } from '../src/controllers/analytic_controller';
import { AnalyticUnit, AnalyticUnitId } from '../src/models/analytic_unit';
import { AnalyticService } from '../src/services/analytic_service';
import { MetricExpanded } from '../src/models/metric';
import { DatasourceRequest } from '../src/models/datasource';
import { BackendSrv } from 'grafana/app/core/services/backend_srv';
import { Emitter } from 'grafana/app/core/utils/emitter';
// prevent "Symbol.asyncIterator is not defined" error
(<any>Symbol).asyncIterator = Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator");
var id = 0;
const analyticService = new AnalyticService('', new BackendSrv({}, {}, {}, {}, {}));
analyticService.postNewItem = async function (
metric: MetricExpanded, datasourceRequest: DatasourceRequest,
newItem: AnalyticUnit, panelId: number
): Promise<AnalyticUnitId> {
id++;
return Promise.resolve(id.toString());
}
export const analyticController = new AnalyticController({}, analyticService, new Emitter());
console.log = jest.fn();
console.error = jest.fn();

3
tsconfig.jest.json

@ -0,0 +1,3 @@
{
"extends": "./tsconfig"
}
Loading…
Cancel
Save