Consuming external APIs with JavaScript [2d]

Exercise 1: Google 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 a Cucumber 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 Archive , 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() or AJAX.
  • 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 Archive with an OpenStreetMap Tile Provider Archive . You can geocode an address with the Nominatim API Archive (it supports CORS requests).
  • When using Unpoly, <script> tags in page fragments will only be executed with up.fragment.config.runScripts = true. As an alternative you may put the <script> tag that loads mapping libraries in your application layout (not ideal, discuss with your mentor why) or load it on demand as described in a separate card.
  • 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(...) Archive to talk with that JavaScript.

Exercise 2: Gender API

  • In MovieDB, add a field "gender" to the Actor model.
    • For the purpose of this exercise, the field is an enum with the values male, female and diverse.
    • Use assignable_values Archive to implement the enum.
  • In the actor form automatically suggest a gender as the user types a name
    • Also read this card for an issue you might encounter.
    • The user should be able to override the suggestion.
  • Use https://gender-api.com/ Archive for this.

Testing

Write a Cucumber feature that tests the functionality above. Use two different techniques to write the test:

  1. Actually talk to the Gender API during tests.
  2. Only in tests, replace the JavaScript function that talks to the Gender API, with a fake implementation that maps a fixed list of names to their genders.
  • It should have the same API as your original Gender API client.
  • The fixed list of names 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 of names.

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

Henning Koch about 6 years ago
This website uses short-lived cookies to improve usability.
Accept or learn more