Browse Source
* setup jest add basic tests to check if works * temporary commit * Create mocks * Add tests setup * Update tests * Remove package-lock.jsonmaster
8 changed files with 124 additions and 3 deletions
@ -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) {}; |
||||||
|
} |
@ -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) {} |
||||||
|
} |
@ -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" |
||||||
|
] |
||||||
|
}; |
@ -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'); |
||||||
|
}); |
||||||
|
}); |
@ -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(); |
Loading…
Reference in new issue