How to write unit test in code using Google Maps for JS

I am writing some kind of JavaScript module designed to work in the browser (on the client side, and not on the server side). This module uses the Google Maps JavaScript API .

I want to cover my code with unit tests. I also want my tests to be isolated. I found several vcr -like JS libraries for recording and bullying the HTTP requests that Google Maps Api produces. But they are all intended for Node.JS (because PhantomJS does not support the use of the fs module ). Node.JS also has richer and more readable backtracks than PhantomJS.

So, I am wondering how to incorporate the Google Maps Javascript API into my tests using the Node.JS test runner and how to write a test for my code?

PS I do not adhere to any specific JS test library. It can be Jasmine, QUnit or any other.

PPS Doesn't have to be a Node.JS runner. If there are other options, everything is in order!

PPPS My goal is to avoid the following things:

  • to avoid dependence on the Internet connection and the corresponding delays in the tests
  • to avoid failed tests due to changes in some GEO data on Google’s servers. For example: if I use the directions, I don’t care if it is 2000 meters or 2001 meters, I just want to know that I get some relevant data from Google and do some calculations with it.

PPPPS Thanks to @MichaelGeary's answer, we know that Google only saves 3 versions of its API. But I do not focus only on Google Maps, I chose it in this question because of its popularity / I have the same question that applies to any other api cards, for example Yandex.Maps , Leaflet (with openstreet) , Bing , etc. .d. Most of them do not remove the old APIs, so I can fix the version and rely on without changing the internal API and HTTP requests.

I also want to avoid false hell because my code is quite complicated and uses many geo-objects of various types. Therefore, it will not be easy to mock them, and then maintain this code. It looks like an unbearable thing.

My idea is to fix the API version for some time (in the case of Google for a short time) and rely on the constancy of the internal format of HTTP requests. And from time to time, delete all recorded data to be sure that everything is in order in the real world.

I want me to be the one in control when I had to fix my test. I do not want Google to break my tests at a random point in time.

+8
source share
1 answer

Recording or mocking HTTP requests made by the Maps API is certainly an interesting idea!

Unfortunately, like any other undocumented API functions, these requests are an internal implementation detail and can be changed at any time. Google releases new versions of the API code four times a year, as well as patch fixes as often as every few weeks. Any of the internal API components, including HTTP requests, can change from one version to another, even in patch versions. The only thing that they guarantee a stable version or patches is a documented API.

Google gives you the opportunity to request a specific version of the API , but they do not support old versions for a very long time, and they do not keep the old patch fixes at all. In fact, only three versions are available at any time. With this writing of these versions are available:

  • The experimental version, currently 3.21.4.
  • Release version, currently 3.20.12.
  • The frozen version, currently 3.19.19.

When the next experimental version (3.22) is released, 3.21 will become the release version, 3.20 will be the frozen version, and 3.19 will be deleted and will no longer be available.

The frozen version has one difference from the release and experimental versions: it no longer receives any patches, so it is completely stable. It is safe to assume that HTTP requests made by the frozen version will not change. But this helps only until this version is removed.

Here is a fiddle to experiment with requesting different versions of the API and displaying which version is actually loaded. The code is as follows:

 <!DOCTYPE html> <html> <head> <title>Google Maps API version test</title> </head> <body> <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> <script src="http://maps.google.com/maps/api/js?v=3.18&sensor=false"></script> <div id="version"></div> <script> $('#version').text( 'google.maps.version is ' + google.maps.version ); </script> </body> </html> 

The violin uses the v=3.18 parameter in the Maps API script URL to request version 3.18, but currently it is loading version 3.19.19. You can change the v= parameter to different values ​​to see which version of the API is loaded. (In addition to specific numbered versions, you can also use v=3 to get the current stable version or v=3.exp to get the current experimental version.)

A sharp-eyed reader may notice that the google.maps.version property displayed by this code is not documented itself! But hey, this is an experimental test code. :-)

This is a fairly common occurrence when HTTP requests change from version to version and can even be changed in a patch fix. As you can see from the list above, version 3.19 went through 19 fixes for patches, and 3.20 went through 12 fixes for patches.

If you want to write unit tests for your Maps API code, my suggestion is to mock the documented Maps API, rather than mock any of its internals. For example, your layout for google.maps.Map can verify that its first parameter is a DOM node and that its second (optional) parameter object contains only known properties with legal values ​​for these properties.

Of course, the Maps API provides a fairly large number of objects, methods and properties, but you do not need to scoff at all of this, only those parts that you use.

+3
source

Source: https://habr.com/ru/post/989469/


All Articles