Skip to content

Commit

Permalink
fix: update code in readme (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt committed Apr 14, 2020
1 parent 1327d9b commit 1a39ba3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
18 changes: 11 additions & 7 deletions README.md
Expand Up @@ -44,12 +44,12 @@ Below is a simple example calling the elevation method on the client class.
Import the Google Maps Client using Typescript and ES6 module:

```js
import {Client} from "@googlemaps/google-maps-services-js";
import {Client, Status} from "@googlemaps/google-maps-services-js";
```

Alternatively using JavaScript without ES6 module support:
```js
const Client = require("@googlemaps/google-maps-services-js").Client;
const {Client, Status} = require("@googlemaps/google-maps-services-js");
```

Now instantiate the client to make a call to one of the APIs.
Expand All @@ -61,14 +61,18 @@ client
.elevation({
params: {
locations: [{ lat: 45, lng: -110 }],
key: process.env.GOOGLE_MAPS_API_KEY
key: "asdf",
},
timeout: 1000 // milliseconds
timeout: 1000, // milliseconds
})
.then(r => {
console.log(r.data.results[0].elevation);
.then((r) => {
if (r.data.status === Status.OK) {
console.log(r.data.results[0].elevation);
} else {
console.log(r.data.error_message);
}
})
.catch(e => {
.catch((e) => {
console.log(e);
});
```
Expand Down
33 changes: 26 additions & 7 deletions e2e/client.test.ts
Expand Up @@ -16,9 +16,6 @@

import { Client, defaultAxiosInstance, defaultHttpsAgent } from "../src";

import axios from "axios";
import { elevation } from "../src/elevation";

test("client should work with defaults", async () => {
const location = { lat: 10, lng: 20 };

Expand Down Expand Up @@ -61,9 +58,9 @@ test("client should work with instance", async () => {
});

test("readme example using client", async () => {
const Client = require("../src").Client;
const { Client, Status } = require("../src");
const client = new Client({});

client
.elevation({
params: {
Expand All @@ -73,11 +70,33 @@ test("readme example using client", async () => {
timeout: 1000, // milliseconds
})
.then((r) => {
expect(r.data.status).toEqual(Status.OK);
expect(r.data.results[0].elevation).toBeGreaterThan(2000);
expect(r.data.results[0].elevation).toBeLessThan(3000);
})
.catch((e) => {
console.log(e)
throw "Should not error"
console.log(e);
throw "Should not error";
});
});

test("readme example using client fails correctly", async () => {
const { Client, Status } = require("../src");
const client = new Client({});

client
.elevation({
params: {
locations: [{ lat: 45, lng: -110 }],
key: "invalid key",
},
timeout: 1000, // milliseconds
})
.then((r) => {
expect(r.data.status).toEqual(Status.REQUEST_DENIED);
})
.catch((e) => {
console.log(e);
throw "Should not error";
});
});

0 comments on commit 1a39ba3

Please sign in to comment.