Reactjs 模拟Bing将API映射到React.js中的Jest

Reactjs 模拟Bing将API映射到React.js中的Jest,reactjs,testing,mocking,jestjs,bing-maps,Reactjs,Testing,Mocking,Jestjs,Bing Maps,在我的玩笑测试中,我需要生成一个Bing地图图钉,如下所示: it('...', () => { var e = new window.Microsoft.Maps.Pushpin({ "latitude": 56.000, "longitude": 46.000 }, {}); /* do stuff */ expect(e.getColor()).toEqual('#ffd633'); }) 但是当我运行测试时,我得到了一个错误: TypeError: Cannot re

在我的玩笑测试中,我需要生成一个Bing地图图钉,如下所示:

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接口吗?

一个选项是引入Bing Maps API类模拟并通过注册,如下所示:

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");
});
结果