chore(text): add lint test for ./test folder

This commit is contained in:
yilin.qyl 2018-12-24 16:28:26 +08:00
parent b4443916b0
commit 8d4ee6ddbb
3 changed files with 255 additions and 258 deletions

View File

@ -94,7 +94,7 @@
"publish": "node ./bin/publish.js",
"lint": "eslint --ext .js ./src",
"cdn": "antv-bin upload -n @antv/g6",
"lint-fix": "eslint --ext .html,.js --fix ./src",
"lint-fix": "eslint --ext .html,.js --fix ./src,./test",
"prepublishOnly": "npm run build-lib && npm run build",
"screenshot": "node ./bin/screenshot.js",
"start": "npm run dev",

View File

@ -3,7 +3,7 @@
* @author dxq613@gmai.com
*/
const {Canvas, Group} = require('@antv/g');
const { Canvas, Group } = require('@antv/g');
const Shape = require('../../../src/shape/shape');
const Global = require('../../../src/global');
require('../../../src/shape/node');
@ -13,9 +13,9 @@ div.id = 'node-shape';
document.body.appendChild(div);
Group.prototype.findByClassName = function(className) {
return this.find(function(shape) {
return shape.get('className') === className;
});
return this.find(function(shape) {
return shape.get('className') === className;
});
};
const canvas = new Canvas({
containerId: 'node-shape',
@ -24,224 +24,224 @@ const canvas = new Canvas({
});
describe('shape node test', () => {
describe('basic method test', () => {
it('get factory',() => {
const factory = Shape.getFactory('node');
expect(factory).not.eql(undefined);
});
it('get default', () => {
const factory = Shape.getFactory('node');
const shape = factory.getShape();
expect(shape.type).eql('circle');
});
describe('basic method test', () => {
it('get factory', () => {
const factory = Shape.getFactory('node');
expect(factory).not.eql(undefined);
});
it('get default', () => {
const factory = Shape.getFactory('node');
const shape = factory.getShape();
expect(shape.type).eql('circle');
});
});
});
describe('nodes test', () => {
const factory = Shape.getFactory('node');
it('circle no label', () => {
const group = canvas.addGroup();
group.translate(50, 50);
const shape = factory.draw('circle', {
size: 40,
color: 'red'
}, group);
canvas.draw();
expect(shape.attr('r')).eql(20);
expect(group.getCount()).eql(1);
});
describe('nodes test', () => {
const factory = Shape.getFactory('node');
it('circle no label', () => {
const group = canvas.addGroup();
group.translate(50, 50);
const shape = factory.draw('circle', {
size: 40,
color: 'red'
}, group);
canvas.draw();
expect(shape.attr('r')).eql(20);
expect(group.getCount()).eql(1);
});
it('circle with label', () => {
const group = canvas.addGroup();
group.translate(50, 100);
factory.draw('circle', {
size: 20,
color: 'blue',
label: '你好,我好,大家好'
}, group);
canvas.draw();
expect(group.getCount()).eql(2);
});
it('circle with label', () => {
const group = canvas.addGroup();
group.translate(50, 100);
factory.draw('circle', {
size: 20,
color: 'blue',
label: '你好,我好,大家好'
}, group);
canvas.draw();
expect(group.getCount()).eql(2);
});
it('ellipse', () => {
const group = canvas.addGroup();
group.translate(100, 50);
const shape = factory.draw('ellipse', {
size: [40, 20],
color: 'yellow',
label: 'ellipse'
}, group);
canvas.draw();
expect(shape.attr('rx')).eql(20);
expect(shape.attr('ry')).eql(10);
expect(group.getCount()).eql(2);
});
it('ellipse', () => {
const group = canvas.addGroup();
group.translate(100, 50);
const shape = factory.draw('ellipse', {
size: [ 40, 20 ],
color: 'yellow',
label: 'ellipse'
}, group);
canvas.draw();
expect(shape.attr('rx')).eql(20);
expect(shape.attr('ry')).eql(10);
expect(group.getCount()).eql(2);
});
it('rect', () => {
const group = canvas.addGroup({
id: 'rect'
});
group.translate(100, 100);
const shape = factory.draw('rect', {
size: [40, 20],
color: 'yellow',
label: 'rect',
labelStyle: {
fill: 'white'
},
style: {
fill: 'red'
}
}, group);
canvas.draw();
expect(shape.attr('x')).eql(-20);
expect(shape.attr('y')).eql(-10);
const label = group.get('children')[1];
expect(label.attr('fill')).eql('white');
expect(group.getCount()).eql(2);
});
it('rect', () => {
const group = canvas.addGroup({
id: 'rect'
});
group.translate(100, 100);
const shape = factory.draw('rect', {
size: [ 40, 20 ],
color: 'yellow',
label: 'rect',
labelStyle: {
fill: 'white'
},
style: {
fill: 'red'
}
}, group);
canvas.draw();
expect(shape.attr('x')).eql(-20);
expect(shape.attr('y')).eql(-10);
const label = group.get('children')[1];
expect(label.attr('fill')).eql('white');
expect(group.getCount()).eql(2);
});
it('image', () => {
const group = canvas.addGroup();
group.translate(150, 100);
const shape = factory.draw('image', {
size: [40, 20],
label: 'my custom image',
shape: 'https://img.alicdn.com/tfs/TB1_uT8a5ERMeJjSspiXXbZLFXa-143-59.png'
}, group);
canvas.draw();
expect(shape.attr('x')).eql(-20);
expect(shape.attr('y')).eql(-10);
expect(shape.attr('img')).not.eql(undefined);
const label = group.get('children')[1];
expect(label.attr('x')).eql(0);
expect(label.attr('y')).eql(10 + Global.nodeLabel.offset);
expect(group.getCount()).eql(2);
});
it('image', () => {
const group = canvas.addGroup();
group.translate(150, 100);
const shape = factory.draw('image', {
size: [ 40, 20 ],
label: 'my custom image',
shape: 'https://img.alicdn.com/tfs/TB1_uT8a5ERMeJjSspiXXbZLFXa-143-59.png'
}, group);
canvas.draw();
expect(shape.attr('x')).eql(-20);
expect(shape.attr('y')).eql(-10);
expect(shape.attr('img')).not.eql(undefined);
const label = group.get('children')[1];
expect(label.attr('x')).eql(0);
expect(label.attr('y')).eql(10 + Global.nodeLabel.offset);
expect(group.getCount()).eql(2);
});
it('update', () => {
const rectGroup = canvas.findById('rect');
const shape = rectGroup.get('children')[0];
it('update', () => {
const rectGroup = canvas.findById('rect');
const shape = rectGroup.get('children')[0];
// 伪造 item, 仅测试接口和图形的变化,不测试一致性
const item = {
getContainer: function() {
return rectGroup;
}
};
factory.update('rect', {
size: [100, 50],
style: {
fill: 'red'
}
}, item);
expect(shape.attr('x')).eql(-50);
expect(shape.attr('y')).eql(-25);
expect(shape.attr('width')).eql(100);
expect(rectGroup.getCount()).eql(1);
factory.update('rect', {
size: [50, 30],
style: {
fill: 'red'
},
label: 'new rect'
}, item);
const item = {
getContainer() {
return rectGroup;
}
};
factory.update('rect', {
size: [ 100, 50 ],
style: {
fill: 'red'
}
}, item);
expect(shape.attr('x')).eql(-50);
expect(shape.attr('y')).eql(-25);
expect(shape.attr('width')).eql(100);
expect(rectGroup.getCount()).eql(1);
factory.update('rect', {
size: [ 50, 30 ],
style: {
fill: 'red'
},
label: 'new rect'
}, item);
expect(rectGroup.getCount()).eql(2);
const label = rectGroup.get('children')[1];
expect(label.attr('text')).eql('new rect');
factory.update('rect', {
size: [50, 30],
style: {
fill: 'red'
},
label: 'old rect'
}, item);
expect(label.attr('text')).eql('old rect');
canvas.draw();
});
expect(rectGroup.getCount()).eql(2);
const label = rectGroup.get('children')[1];
expect(label.attr('text')).eql('new rect');
factory.update('rect', {
size: [ 50, 30 ],
style: {
fill: 'red'
},
label: 'old rect'
}, item);
expect(label.attr('text')).eql('old rect');
canvas.draw();
});
it('active', () => {
const rectGroup = canvas.findById('rect');
const shape = rectGroup.get('children')[0];
it('active', () => {
const rectGroup = canvas.findById('rect');
const shape = rectGroup.get('children')[0];
// 伪造 item, 仅测试接口和图形的变化,不测试一致性
const item = {
getContainer: function() {
return rectGroup;
},
get(name) {
return '';
}
};
const item = {
getContainer() {
return rectGroup;
},
get() {
return '';
}
};
expect(shape.attr('fillOpacity')).eql(1);
factory.setState('rect', 'active', true, item);
expect(shape.attr('fillOpacity')).eql(Global.nodeStateStyle.active.fillOpacity);
expect(shape.attr('fillOpacity')).not.eql(1);
factory.setState('rect', 'active', false, item);
expect(shape.attr('fillOpacity')).eql(1);
});
expect(shape.attr('fillOpacity')).eql(1);
factory.setState('rect', 'active', true, item);
expect(shape.attr('fillOpacity')).eql(Global.nodeStateStyle.active.fillOpacity);
expect(shape.attr('fillOpacity')).not.eql(1);
factory.setState('rect', 'active', false, item);
expect(shape.attr('fillOpacity')).eql(1);
});
it('selected', () => {
const rectGroup = canvas.findById('rect');
const shape = rectGroup.get('children')[0];
it('selected', () => {
const rectGroup = canvas.findById('rect');
const shape = rectGroup.get('children')[0];
// 伪造 item, 仅测试接口和图形的变化,不测试一致性
const item = {
getContainer: function() {
return rectGroup;
},
get(name) {
return '';
}
};
expect(shape.attr('lineWidth')).eql(1);
factory.setState('rect', 'selected', true, item);
const item = {
getContainer() {
return rectGroup;
},
get() {
return '';
}
};
expect(shape.attr('lineWidth')).eql(1);
factory.setState('rect', 'selected', true, item);
expect(shape.attr('lineWidth')).eql(Global.nodeStateStyle.selected.lineWidth);
factory.setState('rect', 'selected', false, item);
expect(shape.attr('lineWidth')).eql(1);
});
expect(shape.attr('lineWidth')).eql(Global.nodeStateStyle.selected.lineWidth);
factory.setState('rect', 'selected', false, item);
expect(shape.attr('lineWidth')).eql(1);
it('label position', () => {
const group = canvas.addGroup();
group.translate(200, 200);
const shape = factory.draw('ellipse', {
size: [60, 20],
color: 'green',
label: 'ellipse position',
labelPosition: 'top'
}, group);
const item = {
getContainer: function() {
return group;
},
get(name) {
return '';
}
};
const label = group.get('children')[1];
expect(label.attr('x')).eql(0);
expect(label.attr('y')).eql(-10 - Global.nodeLabel.offset);
});
factory.update('ellipse', {
size: [60, 20],
color: 'green',
label: 'ellipse position',
labelPosition: 'left'
},item);
expect(label.attr('y')).eql(0);
expect(label.attr('x')).eql(-30 - Global.nodeLabel.offset);
it('label position', () => {
const group = canvas.addGroup();
group.translate(200, 200);
factory.draw('ellipse', {
size: [ 60, 20 ],
color: 'green',
label: 'ellipse position',
labelPosition: 'top'
}, group);
const item = {
getContainer() {
return group;
},
get() {
return '';
}
};
const label = group.get('children')[1];
expect(label.attr('x')).eql(0);
expect(label.attr('y')).eql(-10 - Global.nodeLabel.offset);
factory.update('ellipse', {
size: [60, 20],
color: 'green',
label: 'ellipse position',
labelPosition: 'right'
},item);
expect(label.attr('y')).eql(0);
expect(label.attr('x')).eql(30 + Global.nodeLabel.offset);
canvas.draw();
});
});
});
factory.update('ellipse', {
size: [ 60, 20 ],
color: 'green',
label: 'ellipse position',
labelPosition: 'left'
}, item);
expect(label.attr('y')).eql(0);
expect(label.attr('x')).eql(-30 - Global.nodeLabel.offset);
factory.update('ellipse', {
size: [ 60, 20 ],
color: 'green',
label: 'ellipse position',
labelPosition: 'right'
}, item);
expect(label.attr('y')).eql(0);
expect(label.attr('x')).eql(30 + Global.nodeLabel.offset);
canvas.draw();
});
});
});

View File

@ -7,13 +7,13 @@ const Shape = require('../../../src/shape/shape');
const expect = require('chai').expect;
describe('shape test', () => {
it('register factory', () => {
Shape.registerFactory('test', {
defaultShapeType: 't1'
});
it('register factory', () => {
Shape.registerFactory('test', {
defaultShapeType: 't1'
});
expect(Shape.Test).not.eql(undefined);
expect(Shape.registerTest).not.eql(undefined);
expect(Shape.Test).not.eql(undefined);
expect(Shape.registerTest).not.eql(undefined);
});
it('get factory', () => {
@ -22,63 +22,60 @@ describe('shape test', () => {
});
it('register shape', () => {
Shape.registerTest('t1', {
});
const factory = Shape.getFactory('test');
const shape = factory.getShape('t1');
expect(shape.type).eql('t1');
expect(factory.getShape()).eql(shape);
Shape.registerTest('t1', {});
const factory = Shape.getFactory('test');
const shape = factory.getShape('t1');
expect(shape.type).eql('t1');
expect(factory.getShape()).eql(shape);
});
it('not allow update', () => {
const factory = Shape.getFactory('test');
var isDraw = false;
var afterDraw = false;
const t2 = Shape.registerTest('t2', {
draw() {
isDraw = true;
},
afterDraw() {
afterDraw = true;
}
});
expect(factory.shouldUpdate('t2')).eql(false);
factory.draw('t2', {});
expect(isDraw).eql(true);
expect(afterDraw).eql(true);
});
const factory = Shape.getFactory('test');
let isDraw = false;
let afterDraw = false;
Shape.registerTest('t2', {
draw() {
isDraw = true;
},
afterDraw() {
afterDraw = true;
}
});
expect(factory.shouldUpdate('t2')).eql(false);
factory.draw('t2', {});
expect(isDraw).eql(true);
expect(afterDraw).eql(true);
});
it('allow update', () => {
const factory = Shape.getFactory('test');
var isUpdate = false;
it('allow update', () => {
const factory = Shape.getFactory('test');
let isUpdate = false;
Shape.registerTest('t3', {
update() {
isUpdate = true;
}
});
expect(factory.shouldUpdate('t3')).eql(true);
factory.update('t3');
expect(isUpdate).eql(true);
});
update() {
isUpdate = true;
}
});
expect(factory.shouldUpdate('t3')).eql(true);
factory.update('t3');
expect(isUpdate).eql(true);
});
it('extend shape', () => {
const factory = Shape.getFactory('test');
var isUpdate = false;
let isUpdate = false;
Shape.registerTest('t4', {
draw() {
}
draw() {},
update() { isUpdate = true; }
}, 't3');
expect(factory.shouldUpdate('t4')).eql(true);
factory.update('t4');
expect(isUpdate).to.be.false;
});
it('clear', () => {
delete Shape['Test'];
delete Shape['registerTest'];
expect(Shape.getFactory('test')).eql(undefined);
});
it('clear', () => {
delete Shape.Test;
delete Shape.registerTest;
expect(Shape.getFactory('test')).eql(undefined);
});
});