Repository: googlemaps/google-maps-services-js
Pull request #362 (2020-03-12 01:51:23)

fix: config merge

codecov[bot]: # [Codecov](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/362?src=pr&el=h1) Report
> Merging [#362](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/362?src=pr&el=desc) into [master](https://codecov.io/gh/googlemaps/google-maps-services-js/commit/0a5694834817a6b1d06ff1550d16c4b3a18666ba?src=pr&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/362/graphs/tree.svg?width=650&token=wXidzRKqFM&height=150&src=pr)](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/362?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #362      +/-   ##
==========================================
+ Coverage    92.7%   92.72%   +0.02%     
==========================================
  Files          21       21              
  Lines         329      330       +1     
  Branches       84       85       +1     
==========================================
+ Hits          305      306       +1     
  Misses         24       24
```


| [Impacted Files](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/362?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/client.ts](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/362/diff?src=pr&el=tree#diff-c3JjL2NsaWVudC50cw==) | `98.38% <100%> (+0.02%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/362?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/362?src=pr&el=footer). Last update [0a56948...b4072a5](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/362?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/.travis.yml b/.travis.yml
index e1202722b..8ed76ed3a 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -17,7 +17,7 @@ jobs:
   include:
   - stage: test
     node_js: lts/*
-    if: branch = master AND type = push AND fork = false
+    if: fork = false
     script:
     - npm run test:e2e
   - stage: release
diff --git a/package.json b/package.json
index f386b2ca9..d59f56c33 100644
--- a/package.json
+++ b/package.json
@@ -38,13 +38,13 @@
   "scripts": {
     "prepare": "tsc",
     "test": "jest src",
-    "test:e2e": "jest e2e"
+    "test:e2e": "jest e2e",
+    "test:all": "jest"
   },
   "dependencies": {
     "agentkeepalive": "^4.1.0",
     "axios": "^0.19.0",
-    "query-string": "^6.11.0",
-    "deepmerge": "^4.2.2"
+    "query-string": "^6.11.0"
   },
   "devDependencies": {
     "@semantic-release/changelog": "^5.0.0",
diff --git a/src/client.ts b/src/client.ts
index 8737daa92..58c3f1b3c 100644
--- a/src/client.ts
+++ b/src/client.ts
@@ -1,5 +1,4 @@
 import axios, { AxiosRequestConfig, AxiosInstance } from "axios";
-import * as merge from "deepmerge";
 import { version } from "./index";
 import { HttpsAgent } from "agentkeepalive";
 import {
@@ -103,10 +102,14 @@ export class Client {
 
     if (axiosInstance) {
       this.axiosInstance = axiosInstance;
-      // Avoid changing this instance in any way that affects its behavior
-      this.axiosInstance.defaults.headers["User-Agent"] = userAgent;
+      this.axiosInstance.defaults.headers = {
+        ...defaultConfig.headers,
+        ...this.axiosInstance.defaults.headers
+      };
     } else if (config) {
-      this.axiosInstance = axios.create(merge(defaultConfig, config));
+      config = { ...defaultConfig, ...config };
+      config.headers = { ...defaultConfig.headers, ...(config.headers || {}) };
+      this.axiosInstance = axios.create(config);
     } else {
       this.axiosInstance = defaultAxiosInstance;
     }


Test patch:
diff --git a/e2e/client.test.ts b/e2e/client.test.ts
new file mode 100644
index 000000000..ddb18865c
--- /dev/null
+++ b/e2e/client.test.ts
@@ -0,0 +1,42 @@
+import axios from "axios";
+import { elevation } from "../src/elevation";
+import { Client, defaultHttpsAgent, defaultAxiosInstance } from "../src";
+
+test("client should work with defaults", async () => {
+  const location = { lat: 10, lng: 20 };
+
+  const params = {
+    locations: [location, location],
+    key: process.env.GOOGLE_MAPS_API_KEY
+  };
+  const client = new Client();
+  const r = await client.elevation({ params: params });
+
+  expect(r.data.results.length).toEqual(2);
+});
+
+test("client should work with modified config", async () => {
+  const location = { lat: 10, lng: 20 };
+
+  const params = {
+    locations: [location, location],
+    key: process.env.GOOGLE_MAPS_API_KEY
+  };
+  const client = new Client({ config: { timeout: 30000, httpsAgent: defaultHttpsAgent } });
+  const r = await client.elevation({ params: params });
+
+  expect(r.data.results.length).toEqual(2);
+});
+
+test("client should work with instance", async () => {
+  const location = { lat: 10, lng: 20 };
+
+  const params = {
+    locations: [location, location],
+    key: process.env.GOOGLE_MAPS_API_KEY
+  };
+  const client = new Client({ axiosInstance: defaultAxiosInstance });
+  const r = await client.elevation({ params: params });
+
+  expect(r.data.results.length).toEqual(2);
+});
\ No newline at end of file
diff --git a/src/client.test.ts b/src/client.test.ts
index 0055a6df9..6eea7290b 100644
--- a/src/client.test.ts
+++ b/src/client.test.ts
@@ -39,7 +39,9 @@ test("client can be instantiated with axiosInstance has correct defaults", () =>
   expect(client["axiosInstance"].defaults.headers["User-Agent"]).toEqual(
     userAgent
   );
-  expect(client["axiosInstance"].defaults.headers["Accept-Encoding"]).toBeUndefined();
+  expect(client["axiosInstance"].defaults.headers["Accept-Encoding"]).toBe(
+    acceptEncoding
+  );
   expect(client["axiosInstance"].defaults.timeout).toEqual(
     axios.defaults.timeout
   );