Я пытаюсь проверить, вызывает ли мой MyCustomMap
мой MapController
и создает новый компонент листовки.
Однако MapController
устанавливает _mapGroup
как undefined
на MyCustomMap
компонент, и он должен быть L.layerGroup()
(не undefined
).
Я уже пытался высмеивать MapController
и leaflet
по-разному, но _mapGroup
по-прежнему undefined
. Что не так? Как я могу решить эту проблему?
Мой тестовый файл MyCustomMap (custom-map.test. js):
import MyCustomMap from './custom-map';
import initialMapOptions from './map-options';
import MapController from './map-controller';
const mapCreateGroupMock = jest.fn(),
mapAddGroupToMap = jest.fn();
let myMap = null,
mapController = null;
beforeAll(() => {
const mapOptions = initialMapOptions();
mapController = {
createGroup: mapCreateGroupMock,
addGroupToMap: mapAddGroupToMap
};
myMap = new MyCustomMap(mapController, mapOptions);
});
describe('My custom map', () => {
it(`should call MapController and create a new leaflet map component`, () => {
expect(mapCreateGroupMock).toBeCalledTimes(1);
expect(mapAddGroupToMap).toBeCalledTimes(1);
expect(myMap._mapGroup).not.toBeNull(); // -> here is failing because myMap._mapGroup is undefined and shouldn't be
});
});
My MapController (map -контроллер. js):
import L from 'leaflet';
class MapController {
constructor(container, configuration) {
this._map = new L.Map(container, { zoomControl: false, minZoom: this._minZoom });
this._container = document.getElementById(container);
//more code here ...
}
createGroup() {
return L.layerGroup();
}
addGroupToMap(group) {
this._map.addLayer(group);
}
//more code here ...
}
export default MapController;
Мой компонент MyCustomMap (custom-map. js):
class MyCustomMap {
constructor(mapController, configuration) {
this._mapController = mapController;
this._configuration = configuration;
this._mapGroup = this._mapController.createGroup();
this._mapController.addGroupToMap(this._mapGroup);
//more code here ...
}
}