How to test yandex map in Jest?

  • 0
    init(_mapWrapper) {
            let map = {};
    
            const setMapCenter = (_newCoords) => {
                map.setCenter(_newCoords);
                map.setZoom(yandexMapSettings.zoom);
            };
    
            const setPlaceMarks = (_placeMarks) => {
                map.geoObjects.get(0).removeAll();
                map.geoObjects.get(0).add(_placeMarks);
            };
    
            const setPlaceMarkObjects = (_placeMarks) => {
                const placeMarks = [];
                Iterable.each(_placeMarks, (_placeMark) => {
                    placeMarks.push(computePlaceMark(_placeMark));
                });
    
                setPlaceMarks(placeMarks);
            };
    
            return new Promise((_resolve) => {
                Meta.addScript(src, () => {
                    window.ymaps.ready(() => {
                        map = new window.ymaps.Map(_mapWrapper, yandexMapSettings);
                        map.geoObjects.add(new window.ymaps.Clusterer({
                            preset                        : 'islands#invertedVioletClusterIcons',
                            clusterDisableClickZoom       : true,
                            groupByCoordinates            : false,
                            clusterHideIconOnBalloonOpen  : false,
                            geoObjectHideIconOnBalloonOpen: false,
                        }));
                        _resolve(markRaw({
                            setMapCenter,
                            computePlaceMark,
                            setPlaceMarks,
                            setPlaceMarkObjects,
                        }));
                    });
                });
            });
        },

    addScript(_src, _callback = null) {
            let script = window.document.querySelector('script[src="' + _src + '"]');
    
            if (!script) {
                script     = window.document.createElement('script');
                script.src = _src;
                window.document.head.appendChild(script);
            }
    
            script.addEventListener('load', _callback);
        }

    test('initialization', async () => {
            const mapWrapper = window.document.createElement('div');
    
            const yandexInterface = await YandexMap.init(window.document.body.appendChild(mapWrapper));
        });


    It works well on the client side. But here are the tests ... For some reason, window.ymaps is undefined, although it should be set when the script is loaded automatically.

    Maybe some features of testing in Jest? Can anyone help?
    JavaScript Anonymous, Oct 21, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!