【发布时间】:2023-03-03 17:22:02
【问题描述】:
我正在尝试在 jsdom 中加载 Google 地图,以便我可以使用 react-testing-library 测试地图/鼠标事件。不幸的是,<img> 地图图块和<button> 地图控件没有加载到 dom 中。我没有从 Google 地图或 jsdom 收到任何错误消息,所以我不确定是什么问题。
我正在使用以下软件包:
canvas@2.6.1
jest@26.1.0
jsdom@16.2.2
@testing-library/jest-dom@5.11.0
@testing-library/react@10.4.4
这是一个基于 Google 的 synchronous map loading 示例(API 密钥已编辑)的最小示例:
import { JSDOM } from 'jsdom';
import '@testing-library/jest-dom/extend-expect';
import { render, waitFor } from '@testing-library/react';
const dom = new JSDOM(`
<!DOCTYPE html>
<html>
<head>
<title>Synchronous Loading</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
<script>
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
</script>
</body>
</html>
`, {
pretendToBeVisual: true,
resources: 'usable',
runScripts: 'dangerously',
});
global.window = dom.window;
global.document = dom.window.document;
describe('Google Map', () => {
it('has a zoom button', async () => {
const { queryByRole } = render(); // just checking the dom
await waitFor(() => expect(queryByRole('button', { name: 'Zoom in' })).toBeInTheDocument(), { timeout: 5000 });
});
});
此测试因超时而失败。调试器显示地图 div 具有以下内容,其中缺少很多应有的内容(地图图块、地图控件等):
<div id="map" style="position: relative; overflow: hidden;">
<div style="height: 100%; width: 100%; position: absolute; top: 0px; left: 0px; background-color: rgb(229, 227, 223);">
<div style="overflow: hidden;"/>
<div class="gm-style" style="position: absolute; z-index: 0; left: 0px; top: 0px; height: 100%; width: 100%; padding: 0px; border-width: 0px; margin: 0px;">
<div style="position: absolute; z-index: 0; left: 0px; top: 0px; height: 100%; width: 100%; padding: 0px; border-width: 0px; margin: 0px;cursor: url(https://maps.gstatic.com/mapfiles/openhand_8_8.cur), default;" tabindex="0">
<div style="z-index: 1; position: absolute; left: 50%; top: 50%; width: 100%; transform: translate(0px,0px);">
<div style="position: absolute; left: 0px; top: 0px; z-index: 100; width: 100%;">
<div style="position: absolute; left: 0px; top: 0px; z-index: 0;">
<div style="position: absolute; z-index: 992; transform: matrix(1,0,0,1,-32,-20);">
<div style="position: absolute; left: 0px; top: 0px; width: 256px; height: 256px;">
<div style="width: 256px; height: 256px;" />
</div>
</div>
</div>
</div>
<div style="position: absolute; left: 0px; top: 0px; z-index: 101; width: 100%;" />
<div style="position: absolute; left: 0px; top: 0px; z-index: 102; width: 100%;" />
<div style="position: absolute; left: 0px; top: 0px; z-index: 103; width: 100%;" />
<div style="position: absolute; left: 0px; top: 0px; z-index: 0;" />
</div>
<div class="gm-style-pbc" style="z-index: 2; position: absolute; height: 100%; width: 100%; padding: 0px; border-width: 0px; margin: 0px left: 0px; top: 0px; transition-duration: 0; opacity: 0;">
<p class="gm-style-pbt" />
</div>
<div style="z-index: 3; position: absolute; height: 100%; width: 100%; padding: 0px; border-width: 0px; margin: 0px; left: 0px; top: 0px;">
<div style="z-index: 4; position: absolute; left: 50%; top: 50%; width: 100%; transform: translate(0px,0px);">
<div style="position: absolute; left: 0px; top: 0px; z-index: 104; width: 100%;" />
<div style="position: absolute; left: 0px; top: 0px; z-index: 105; width: 100%;" />
<div style="position: absolute; left: 0px; top: 0px; z-index: 106; width: 100%;" />
<div style="position: absolute; left: 0px; top: 0px; z-index: 107; width: 100%;" />
</div>
</div>
</div>
<iframe aria-hidden="true" frameborder="0" style="z-index: -1; position: absolute; width: 100%; height: 100%; top: 0px; left: 0px;" tabindex="-1" />
</div>
</div>
</div>
也许它在到达<iframe> 时遇到了错误(因为地图控件应该在那之后出现),但我还没有找到进一步调试问题的方法。有什么办法可以让这张地图在 jsdom 中完全加载?
【问题讨论】:
标签: javascript google-maps-api-3 jestjs react-testing-library jsdom