325 Consuming external APIs with JavaScript [2d]

Updated . Posted . Visible to the public.

Many features need data from third-party services. In this lesson you integrate geocoding, maps and a title-autocomplete API from JavaScript, and learn to test code that talks to external services.

Important

Work on this lesson in builder mode.

Learning goals

  • You can explain what is involved when a browser talks to a third-party service: API keys, CORS, rate limits, and why some calls belong on your server instead.
  • You can integrate a third-party JavaScript library (e.g. a map) and call a JSON API from the browser to enrich your UI.
  • You can design your JavaScript so a test can query its state through a small API, and drive it from a feature spec.
  • You can build a find-as-you-type suggestion that stays correct when responses arrive out of order, and lets the user override the suggestion.
  • You can test code that talks to an external service in two ways — against the real service, or against a fake with the same interface — and explain the trade-offs.
  • You can keep end-to-end tests stable by putting external integrations behind feature flags.

Resources

Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.

Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.

Maps and geocoding

Movie data

Talking to APIs from the browser and testing it

Exercises

Maps

  • In MovieDB, add a new field “Principal filming location”.
  • In a movie’s show view, geocode that location and show a Google map centered around it
  • Now write an E2E feature that tests that the map shows the correct location.

Hints

  • The purpose of this lesson to learn interacting with external APIs from JavaScript. Even though this exercise can be implemented trivially by embedding a Google Maps iframe Show archive.org snapshot , don't do that for the purpose of learning. Instead you should work with JavaScript-consumable APIs, either by calling functions of a client library or by making your own HTTP requests using fetch().
  • To implement this exercise you will need two external services: One to transform an address string to geographical coordinates ("geocoding") and one to display a map centered around given coordinates.
  • Google Maps offers both geocoding and map display APIs. If you want to use Google Maps, you need to sign up for an API key.
  • A free alternative for educational use is using Leaflet.js Show archive.org snapshot with an OpenStreetMap Tile Provider Show archive.org snapshot . You can geocode an address with the Nominatim API Show archive.org snapshot (it supports CORS requests).
  • Your JavaScript that creates and centers the map should expose an API that lets you query the current map position from your test.
  • Your test can use evaluate_script(...) Show archive.org snapshot to talk with that JavaScript.

Note

Adding leaflet to your build pipeline is quite tricky, as the JS script needs to know the fingerprinted image URLs e.g. of the marker icon on the map. For the sake of this specific exercise, we recommend integrating Leaflet using their CDN solution Show archive.org snapshot instead.

Title autocomplete

Testing

Write an E2E feature that tests the functionality above. Implement the test using two different techniques:

  1. Actually talk to the TMDB API during tests.
  2. Only in tests, replace the JavaScript function that talks to the TMDB API with a fake implementation that maps a fixed list of queries to their suggestions.
    • It should have the same API as your original TMDB client.
    • The fixed list can be part of your fake implementation. If it helps your test, your fake implementation might also expose an additional API to change the list.

Talk with your mentor about the pros and cons of each approach.

Tip

Use feature flags to disable the title autosuggestion and map rendering in tests. Enable each feature only for tests that actually care about the corresponding functionality.

Profile picture of Henning Koch
Henning Koch
Last edit
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2015-08-20 14:33)