English 中文(简体)
姆克地图箱在 j顶点击手ler
原标题:Mock mapbox click handler callback in jest
问题回答

问题有两个部分:

  • testing runSomeFunction()
  • testing that the map handles click events correctly

对我来说,点击手是匿名功能:

(e) => {
  const { lng, lat } = e.lngLat;
  runSomeFunction(lat, lng);
}

<代码>runSomeFunction(>> 可与单位测试单独测试。

The click handler could possibly be tested in isolation with a unit test if it s extracted to a named function.

The click handler can also be tested by firing a click event and confirming that it calls a mocked runSomeFunction() with the correct arguments, or confirming the end result of the click. I would call these integration tests instead of unit tests because they don t test the click handler in isolation. I like this take on integration vs unit testing.

MapBox has resources on automated testing with real browsers.

Jest

Here is a basic Map jest mock to handle events with mapbox-gl 3.1.2.

测试环境是按最弱的docs子编成的bel子。

// tests/__mocks__/mapbox-gl.js

import EventEmitter from  events ;
import { TextDecoder } from  util ;

// Add `TextDecoder` to globals.
globalThis.TextDecoder = TextDecoder;

const mapboxActual = jest.requireActual( mapbox-gl );

/**
 * Mock Map class.
 *
 * Extends the `EventEmitter` class and adds mock properties & methods.
 *
 * The real `Map` extends `Evented`, but `Evented.fire()` is now private
 * and may be deprecated in the future.
 *
 * @see https://nodejs.org/api/events.html#class-eventemitter
 * @see https://github.com/mapbox/mapbox-gl-js/blob/main/CHANGELOG.md#%EF%B8%8F-breaking-changes-5
 */
class MockMap extends EventEmitter {
  // ... Put mock properties & methods here.
}

mapboxActual.Map = MockMap;

export default mapboxActual;

Using it

This is a trivial example that tests the runSomeFunction() follow-through with a spy, only b/c I don t know a better way to confirm that console.log() was called correctly. In a real app, if runSomeFunction() renders something, you can test that it appears on the screen as expected.

// runSomeFunction.js

export default function runSomeFunction(lat, lng) {
  console.log(`Map clicked at ${lat}, ${lng}.`);
}
// map.js

import mapboxgl from  mapbox-gl ;
import runSomeFunction from "./runSomeFunction";

// If you pull in `mapbox-gl` with an html `script` tag instead of
// importing it, you ll need to add `mapboxgl` to the globals for the
// test environment so that it s available here.

mapboxgl.accessToken =  pk.eyJ1Ijoia2VudHIiLCJhIjoiY2tva3YxOWFzMDdpcDJvcno5ZjBzM3JvNCJ9.ZdH2WahCpmtdiLsNLWZNFQ ;

const map = new mapboxgl.Map({
  container:  map , // container ID
  center: [ -74.5, 40 ], // starting position [lng, lat]
  zoom: 9, // starting zoom
});

map.on( click , (e) => {
  const { lng, lat } = e.lngLat;
  runSomeFunction(lat, lng);
});

export default map;
// map.test.js

import map from  ./map ;

jest.spyOn(console,  log );

it( logs click on map , () => {

  const testLatLng = {
    lat: 39,
    lng: -74,
  };

  // Confirm `console.log` not called before event is fired.
  expect(console.log)
    .not
    .toBeCalled();

  // Emit click event.
  map.emit(
     click ,
    {
      lngLat: testLatLng,
    }
  );

  // Check result.
  // May need delay for async event.
  expect(console.log)
    .toBeCalledTimes(1);

  expect(console.log)
    .toBeCalledWith(`Map clicked at ${testLatLng.lat}, ${testLatLng.lng}.`);
});

mapbox-gl via script tag

If you re pulling in mapbox-gl via an html script tag instead of importing it into map.js, here s an example of setting the global for tests.

// jest.config.js

/** @type {import( jest ).Config} */
const config = {
  setupFiles: [
     <rootDir>/tests/setupTests.js 
  ],
  testEnvironment: "jsdom",
};

module.exports = config;
// tests/setupTests.js

import mapboxgl from "mapbox-gl";

globalThis.mapboxgl = mapboxgl;




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签