Further updated Snap.set() typings, fixed issues with scoping in test file

This commit is contained in:
Terry Mun
2019-01-04 21:31:10 +01:00
parent e7f993ee3e
commit b40e204bbf
2 changed files with 87 additions and 85 deletions
+6 -6
View File
@@ -303,9 +303,9 @@ declare namespace Snap {
}
export interface Set {
animate(attrs:{[attr:string]:string|number|boolean|any},duration:number,easing?:(num:number)=>number,callback?:()=>void):Snap.Element;
animate(...params:Array<{attrs:any,duration:number,easing:(num:number)=>number,callback?:()=>void}>):Snap.Element;
attr(params: {[attr:string]:string|number|boolean|BBox|any}): Snap.Element;
animate(attrs:{[attr:string]:string|number|boolean|any},duration:number,easing?:(num:number)=>number,callback?:()=>void):Snap.Set;
animate(...params:Array<{attrs:any,duration:number,easing:(num:number)=>number,callback?:()=>void}>):Snap.Set;
attr(params: {[attr:string]:string|number|boolean|BBox|any}): Snap.Set;
attr(param: "viewBox"): BBox;
attr(param: string): string;
bind(attr: string, callback: Function): Snap.Set;
@@ -315,10 +315,10 @@ declare namespace Snap {
exclude(element:Snap.Element):boolean;
forEach(callback:Function,thisArg?:Object):Snap.Set;
pop():Snap.Element;
push(el:Snap.Element):Snap.Element;
push(els:Snap.Element[]):Snap.Element;
push(el:Snap.Element):Snap.Set;
push(...els:Snap.element[]):Snap.Set;
remove(): Snap.Set;
splice(index:number,count:number,insertion?:Object[]):Snap.Element[];
splice(index:number,count:number,...insertion:Snap.Element[]):Snap.Set;
}
interface Filter {
+81 -79
View File
@@ -1,92 +1,94 @@
// Tests by Terry Mun
// Checks that Snap.set() is properly typed
(() => {
// Tests by Terry Mun
// Checks that Snap.set() is properly typed
const s = Snap(200, 500);
s.attr({
viewBox: '0 0 200 500'
});
const s = Snap(200, 500);
s.attr({
viewBox: '0 0 200 500'
});
// Create multiple elements to be added to set
const rect1 = s.rect(0,0,50,50);
const rect2 = s.rect(0,100,50,50);
const rect3 = s.rect(0,200,50,50);
const rect4 = s.rect(0,300,50,50);
let rect5 = null;
// Create multiple elements to be added to set
const rect1 = s.rect(0,0,50,50);
const rect2 = s.rect(0,100,50,50);
const rect3 = s.rect(0,200,50,50);
const rect4 = s.rect(0,300,50,50);
let rect5: Snap.Element;
// Create new set
const rectSet = Snap.set(rect1, rect2);
// Create new set
const rectSet = Snap.set(rect1, rect2);
// Add missing rectangles to set
rectSet.push(rect3, rect4);
// Add missing rectangles to set
rectSet.push(rect3, rect4);
// Set all attributes
rectSet.attr({ fill: 'steelblue' });
// Set all attributes
rectSet.attr({ fill: 'steelblue' });
// Animate all elements in set
function animateTest() {
return new Promise(resolve => {
rectSet.animate({ x: 50 }, 500, mina.bounce, resolve);
});
}
// Animate all elements in set
function animateTest() {
return new Promise(resolve => {
rectSet.animate({ x: 50 }, 500, mina.bounce, resolve);
});
}
// Animate elements individually
function forEachTest() {
const colors = ['red', 'green', 'blue'];
return new Promise(resolve => {
rectSet.forEach((element, idx) => element.attr({ fill: colors[idx] || '#aaa' }));
resolve();
});
}
// Animate elements individually
function forEachTest() {
const colors = ['red', 'green', 'blue'];
return new Promise(resolve => {
rectSet.forEach((element, idx) => element.attr({ fill: colors[idx] || '#aaa' }));
resolve();
});
}
// Exclude rect4 from set
function excludeTest() {
return new Promise(resolve => {
rectSet.exclude(rect4);
rectSet.animate({ x: 100 }, 500, mina.bounce, resolve);
});
}
// Exclude rect4 from set
function excludeTest() {
return new Promise(resolve => {
rectSet.exclude(rect4);
rectSet.animate({ x: 100 }, 500, mina.bounce, resolve);
});
}
// Remove rect2 from set (it has index of 1)
// Add new rect5 to set
async function spliceTest() {
rectSet.splice(1, 1);
rectSet.attr({ fill: '#000' });
rect5 = s.rect(100, 400, 50, 50);
rectSet.splice(1, 0, rect5);
return;
}
// Remove rect2 from set (it has index of 1)
// Add new rect5 to set
async function spliceTest() {
rectSet.splice(1, 1);
rectSet.attr({ fill: '#000' });
rect5 = s.rect(100, 400, 50, 50);
rectSet.splice(1, 0, rect5);
return;
}
// Clear set. Rectangles should not go back to x=0
function clearTest() {
return new Promise(resolve => {
rectSet.clear();
// SHOULD NOT WORK
rectSet.animate({ x: 0 }, 500, mina.bounce, resolve);
// Re-add rects to set
rectSet.push(rect1, rect2, rect3, rect4, rect5);
resolve();
});
}
// Clear set. Rectangles should not go back to x=0
function clearTest() {
return new Promise(resolve => {
rectSet.clear();
// SHOULD NOT WORK
rectSet.animate({ x: 0 }, 500, mina.bounce, resolve);
// Re-add rects to set
rectSet.push(rect1, rect2, rect3, rect4, rect5);
resolve();
});
}
// Remove all elements from DOM that are present in set
async function removeTest() {
rectSet.remove();
return;
}
// Remove all elements from DOM that are present in set
async function removeTest() {
rectSet.remove();
return;
}
// Start test
async function startTest() {
await animateTest();
await forEachTest();
await excludeTest();
await spliceTest();
await clearTest();
await removeTest();
return;
}
// Start test
async function startTest() {
await animateTest();
await forEachTest();
await excludeTest();
await spliceTest();
await clearTest();
await removeTest();
return;
}
startTest();
startTest();
})();