isc*_*odv 2 google-maps typescript karma-jasmine angular
我有一个取决于google.maps.Map类的 Angular 组件。它看起来像这样:
export class MapViewComponent implements OnInit {
@Input()
public mapOptions: google.maps.MapOptions;
public map: google.maps.Map;
@ViewChild("map", { static: true })
mapDomNode: ElementRef;
public ngOnInit() {
this.map = new google.maps.Map(this.mapDomNode.nativeElement, this.mapOptions);
}
}
Run Code Online (Sandbox Code Playgroud)
我根据文档安装了 Google 地图类型定义:
npm i -D @types/google.maps
Run Code Online (Sandbox Code Playgroud)
现在我需要为我的组件创建单元测试,并尝试使用以下SO 答案中的方法(我使用 Karma 进行测试):
window['google'] = {
maps: {
Map: () => ({})
}
};
const spy = spyOn(window['google']['maps'], 'Maps').and.returnValue({});
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Type '() => {}' is not assignable to type 'typeof Map'.
Type '() => {}' provides no match for the signature 'new (mapDiv: Element, opts?: MapOptions): Map'.ts(2322)
index.d.ts(3223, 9): The expected type comes from property 'Map' which is declared here on type 'typeof maps'
Run Code Online (Sandbox Code Playgroud)
我尝试使用不同的变体,例如Map: () => { return {} as google.maps.Map }许多其他东西,但 TypeScript 总是显示类型错误。
我如何使用任何对象而不是类型google.maps.Map。
我设法通过添加any到模拟对象的末尾来修复它:
googleMaps = jasmine.createSpyObj('Maps', ['setCenter', 'setZoom', 'setOptions']);
function Map() {
return googleMaps;
}
window['google'] = {
maps: {
Map: Map,
...
}
} as any; // <---
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1846 次 |
| 最近记录: |