From 5aeb18ec19ca958b51502d4983a9025d6bd22a8c Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 20 Jul 2018 13:56:32 +0300 Subject: [PATCH] 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 --- .gitignore | 1 + .../grafana/app/core/services/backend_srv.ts | 25 +++++++++++++++ __mocks__/grafana/app/core/utils/emitter.ts | 8 +++++ jest.config.js | 20 ++++++++++++ package.json | 10 ++++-- tests/analyticController.jest.ts | 32 +++++++++++++++++++ tests/setup_tests.ts | 28 ++++++++++++++++ tsconfig.jest.json | 3 ++ 8 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 __mocks__/grafana/app/core/services/backend_srv.ts create mode 100644 __mocks__/grafana/app/core/utils/emitter.ts create mode 100644 jest.config.js create mode 100644 tests/analyticController.jest.ts create mode 100644 tests/setup_tests.ts create mode 100644 tsconfig.jest.json diff --git a/.gitignore b/.gitignore index 752cab6..16dee9c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ node_modules dist +package-lock.json npm-debug.log coverage/ .aws-config.json diff --git a/__mocks__/grafana/app/core/services/backend_srv.ts b/__mocks__/grafana/app/core/services/backend_srv.ts new file mode 100644 index 0000000..2554908 --- /dev/null +++ b/__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) {}; +} diff --git a/__mocks__/grafana/app/core/utils/emitter.ts b/__mocks__/grafana/app/core/utils/emitter.ts new file mode 100644 index 0000000..f4e81ba --- /dev/null +++ b/__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) {} +} diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..093c1be --- /dev/null +++ b/jest.config.js @@ -0,0 +1,20 @@ +module.exports = { + verbose: true, + "globals": { + "ts-jest": { + "tsConfigFile": "tsconfig.jest.json" + } + }, + "transform": { + "\\.ts?": "/node_modules/ts-jest/preprocessor.js" + }, + "testRegex": "(\\.|/)([jt]est)\\.[jt]s$", + "moduleFileExtensions": [ + "ts", + "js", + "json" + ], + "setupFiles": [ + "/tests/setup_tests.ts" + ] +}; diff --git a/package.json b/package.json index 981b673..f677575 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "dist/module", "scripts": { "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": [], "author": "Alexey Velikiy", @@ -15,22 +16,25 @@ "@types/angular": "^1.6.43", "@types/flot": "0.0.31", "@types/grafana": "github:CorpGlory/types-grafana", + "@types/jest": "^23.3.0", "@types/jquery": "^3.3.0", "@types/lodash": "^4.14.104", + "@types/md5": "^2.1.32", "@types/moment": "^2.13.0", "@types/perfect-scrollbar": "^1.3.0", "@types/tinycolor2": "^1.4.0", - "@types/md5": "^2.1.32", "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-preset-env": "^1.6.0", "copy-webpack-plugin": "^4.0.1", + "jest": "^23.4.1", "loader-utils": "^1.1.0", + "md5": "^2.2.1", "ts-loader": "^4.2.0", "typescript": "^2.8.3", "webpack": "^4.7.0", "webpack-cli": "^2.1.2", - "md5": "^2.2.1" + "ts-jest": "^22.4.6" }, "dependencies": { "perfect-scrollbar": "^1.3.0", diff --git a/tests/analyticController.jest.ts b/tests/analyticController.jest.ts new file mode 100644 index 0000000..1271d1f --- /dev/null +++ b/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'); + }); +}); diff --git a/tests/setup_tests.ts b/tests/setup_tests.ts new file mode 100644 index 0000000..f80a704 --- /dev/null +++ b/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 +(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 { + id++; + return Promise.resolve(id.toString()); +} + +export const analyticController = new AnalyticController({}, analyticService, new Emitter()); + +console.log = jest.fn(); +console.error = jest.fn(); diff --git a/tsconfig.jest.json b/tsconfig.jest.json new file mode 100644 index 0000000..1c66acf --- /dev/null +++ b/tsconfig.jest.json @@ -0,0 +1,3 @@ +{ + "extends": "./tsconfig" +}