在Jest测试中,我需要生成一个Bing Maps图钉:
it('...', () => {
var e = new window.Microsoft.Maps.Pushpin({ "latitude": 56.000, "longitude": 46.000 }, {});
/* do stuff */
expect(e.getColor()).toEqual('#ffd633');
})
但是当我运行测试时,我得到了错误:
TypeError: Cannot read property 'Maps' of undefined
有人知道如何模仿Bing Maps API Microsoft与React中的Jest接口吗?
答案 0 :(得分:1)
一种选择是引入Bing Maps API类模拟并通过beforeAll
Jest setup function注册它,如下所示:
const setupBingMapsMock = () => {
const Microsoft = {
Maps: {
Pushpin : class {
location = null;
options = null;
constructor(location,options) {
this.location = location;
this.options = options;
}
getColor(){
return this.options.color;
}
}
}
};
global.window.Microsoft = Microsoft;
};
beforeAll(() => {
setupBingMapsMock();
});
it("creates a marker", () => {
const e = new window.Microsoft.Maps.Pushpin(
{ latitude: 56.0, longitude: 46.0 },
{'color': '#ffd633'}
);
expect(e.getColor()).toEqual("#ffd633");
});
结果