Skip to content

Commit c90e0ba

Browse files
committed
fix test
1 parent ad3ef8f commit c90e0ba

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

spec/__mocks__/ctrl.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
notify: jest.fn(),
3+
clearNotify: jest.fn(),
4+
};

spec/global_class.test.ts

+35-37
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ import { $GF, GFDrawioTools, GFTimer, GFVariables } from '../src/globals_class';
33
const $scope = require('$scope');
44
const templateSrv = {};
55
const dashboard = {};
6-
const ctrl = {
7-
notify: jest.fn(),
8-
clearNotify: jest.fn(),
9-
};
6+
const ctrl = require('ctrl');
107
const $gf = $GF.create($scope, templateSrv, dashboard, ctrl);
11-
128
const xmlGraph =
139
'<mxGraphModel dx="1073" dy="521" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="0" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0"><root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="arrow-1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;jettySize=auto;orthogonalLoop=1;strokeWidth=4;shadow=1;" parent="1" source="shape-grafana" target="shape-love" edge="1"><mxGeometry relative="1" as="geometry"/></mxCell><object label="Grafana" href="www.google.fr" id="shape-grafana"><mxCell style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;shadow=1;" parent="1" vertex="1"><mxGeometry x="10" y="10" width="120" height="60" as="geometry"/></mxCell></object><mxCell id="arrow-2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;jettySize=auto;orthogonalLoop=1;strokeWidth=4;shadow=1;" parent="1" source="shape-love" target="shape-mxgraph" edge="1"><mxGeometry relative="1" as="geometry"/></mxCell><mxCell id="shape-love" value="loves" style="triangle;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;shadow=1;" parent="1" vertex="1"><mxGeometry x="210" width="60" height="80" as="geometry"/></mxCell><mxCell id="shape-mxgraph" value="mxGraph" style="ellipse;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;shadow=1;" parent="1" vertex="1"><mxGeometry x="340" width="120" height="80" as="geometry"/></mxCell><mxCell id="text-grafana" value="MyText : TextVal" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1"><mxGeometry x="9" y="50" width="120" height="20" as="geometry"/></mxCell><mxCell id="text-arrow1" value="Text 2" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1"><mxGeometry x="150" y="20" width="40" height="20" as="geometry"/></mxCell><mxCell id="text-arrow2" value="Text 3" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1"><mxGeometry x="280" y="20" width="40" height="20" as="geometry"/></mxCell><mxCell id="text-mxgraph" value="Text 4" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1"><mxGeometry x="380" y="50" width="40" height="20" as="geometry"/></mxCell></root></mxGraphModel>';
1410
const compGraph =
@@ -42,65 +38,67 @@ describe('Test Global $GF utils', () => {
4238

4339
// Test if eval javascript works
4440
describe('Secure Eval', () => {
45-
//TODO : Fix eval
46-
// it('should be enable to understand Math Lib', () => {
47-
// expect($gf.eval('Math.random()')).toBeGreaterThan(0);
48-
// expect($gf.utils.evalIt_deprecated('Math.random() * 100')).toBeLessThanOrEqual(101);
49-
// });
50-
test('should be eval simple operation 2+2', () => {
51-
expect($gf.utils.evalIt_deprecated('2+2')).toBe(4);
41+
let variables;
42+
beforeAll(()=> {
43+
variables = GFVariables.create();
44+
})
45+
afterAll(()=> {
46+
variables = GFVariables.create();
47+
})
48+
it('should be enable to understand Math Lib', () => {
49+
expect(variables.eval('Math.random()')).toBeGreaterThan(0);
50+
expect(variables.eval('Math.random() * 100')).toBeLessThanOrEqual(101);
51+
});
52+
it('should be eval simple operation 2+2', () => {
53+
expect(variables.eval('2+2')).toBe(4);
5254
});
5355
});
5456

5557
// Test if globalvariables
5658
describe('Variables', () => {
57-
// let $gf: $GF;
58-
// const $scope = jest.fn();
59-
// const templateSrv = jest.fn();
60-
// const dashboard = jest.fn();
61-
// const ctrl = jest.fn();
62-
const key = '_value';
63-
const value = 12345;
64-
const text_entry = 'My text is ${_value} at this time';
65-
const text_result = 'My text is 12345 at this time';
66-
let variable: GFVariables;
59+
let variables, key, value, text_entry, text_result;
6760
beforeEach(()=>{
68-
// $gf = $GF.create($scope, templateSrv, dashboard, ctrl)
69-
variable = GFVariables.create();
61+
key = '_value';
62+
value = 12345;
63+
text_entry = 'My text is ${_value} at this time';
64+
text_result = 'My text is 12345 at this time';
65+
variables = GFVariables.create();
7066
});
7167
beforeEach(()=>{
72-
// $gf = $GF.create($scope, templateSrv, dashboard, ctrl)
73-
variable.clear();
68+
variables.clear();
7469
});
7570
test('Should be not null', () => {
76-
expect(variable).not.toBe(undefined);
77-
expect(variable).toMatchSnapshot();
78-
expect(variable.get(key)).toBe(undefined);
71+
expect(variables).not.toBe(undefined);
72+
expect(variables).toMatchSnapshot();
73+
expect(variables.get(key)).toBe(undefined);
7974
});
8075

8176
test('Should be null for key ' + key, () => {
82-
expect(variable.get(key)).toBe(undefined);
77+
expect(variables.get(key)).toBe(undefined);
8378
});
8479
test('Should be not null for key ' + key, () => {
85-
variable.set(key, value);
86-
expect(variable.get(key)).not.toBe(undefined);
87-
expect(variable.get(key)).toBe(value);
80+
variables.set(key, value);
81+
expect(variables.get(key)).not.toBe(undefined);
82+
expect(variables.get(key)).toBe(value);
8883
});
8984

9085
test('should be a list', () => {
91-
expect(variable.keys()).toStrictEqual(['_value']);
86+
variables.set(key, value);
87+
expect(variables.keys()).toStrictEqual(['_value']);
9288
});
9389

9490
test('should be a list ${}', () => {
95-
expect(variable.getVarsNames()).toStrictEqual(['${_value}']);
91+
variables.set(key, value);
92+
expect(variables.getVarsNames()).toStrictEqual(['${_value}']);
9693
});
9794

9895
test('Remplace text with variable', () => {
99-
expect(variable.replaceText(text_entry)).toBe(text_result);
96+
variables.set(key, value);
97+
expect(variables.replaceText(text_entry)).toBe(text_result);
10098
});
10199

102100
test('should be evaluate', () => {
103-
expect(variable.eval('${_value}*2')).toBe(24690);
101+
expect(variables.eval('${_value}*2')).toBe(24690);
104102
});
105103

106104
test('Should be null after remove key ' + key, () => {

src/globals_class.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export class GFVariables {
438438
replaceText(text: string): string {
439439
try {
440440
let templateSrv = GFPlugin.getTemplateSrv();
441-
text = templateSrv !== undefined ? templateSrv.replaceWithText(text) : text;
441+
text = templateSrv !== undefined ? templateSrv.replace(text) : text;
442442
for (let [key, value] of this._variables) {
443443
text = text.replace('${' + key + '}', value);
444444
}

0 commit comments

Comments
 (0)