我需要在Jasmine中编写以下函数的规范。
export function getComponent<T>(ele: HTMLElement, comp: string): T {
let instance: T;
for (let i = 0; i < (<DomElements>(ele as HTMLElement)).ej2_instances.length; i++) {
instance = <T>(ele as DomElements).ej2_instances[i];
let compName: string = (instance as { getModuleName: () => string } & T).getModuleName();
if (comp === compName) {
return instance;
}
}
return undefined;
}
我尝试了以下但是没有用。
describe('getcomponent function', (): void => {
let instance: string;
let componentname: string;
function test(instance, componentname) {
it('if statement', function () {
expect(instance).toEqual(componentname)
});
}
for (let i = 0; i < instance.length; i++) {
test(instance[i], componentname[i])
}
})
答案 0 :(得分:0)
您可以使用此代码,它将帮助您获得解决方案
describe("getcomponent funtion", function () {
it("if statement", function () {
let comp: any= {ej2_instances:[new Touch()]};
expect(getComponent(comp, 'touch') instanceof Touch).toBe(true);
});
it("else statement", function () {
let comps: any= {ej2_instances:[new Touch()]};
expect(getComponent(comps, 'button') instanceof Touch).toBe(true);
});
});
&#13;