From a054b97a619a45401fb7a9977d01eff1c6c2396c Mon Sep 17 00:00:00 2001 From: Sanketh Katta Date: Tue, 7 Jan 2020 16:34:35 -0800 Subject: [PATCH] Add `unmount()` method to remove `message` event listener from the `window`. --- doc/README.md | 11 +++++++++++ package.json | 2 +- src/sdk.js | 11 +++++++++++ test/unit/sdk.test.js | 16 ++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/doc/README.md b/doc/README.md index 10001bcc..15940ec4 100644 --- a/doc/README.md +++ b/doc/README.md @@ -28,6 +28,7 @@ Smartcar JavaScript SDK documentation. * [.getAuthUrl(options)](#Smartcar+getAuthUrl) ⇒ String * [.openDialog(options)](#Smartcar+openDialog) * [.addClickHandler(options)](#Smartcar+addClickHandler) + * [.unmount()](#Smartcar+unmount) * _static_ * [.AccessDenied](#Smartcar.AccessDenied) ⇐ Error * [new Smartcar.AccessDenied(message)](#new_Smartcar.AccessDenied_new) @@ -111,6 +112,16 @@ On-click event calls openDialog when the specified element is clicked. | [options.vehicleInfo.make] | String | | `vehicleInfo` is an object with an optional property `make`, which allows users to bypass the car brand selection screen. For a complete list of supported makes, please see our [API Reference](https://smartcar.com/docs/api#authorization) documentation. | | [options.singleSelect] | Boolean \| Object | | An optional value that sets the behavior of the grant dialog displayed to the user. If set to `true`, `single_select` limits the user to selecting only one vehicle. If `single_select` is passed in as an object with the property `vin`, Smartcar will only authorize the vehicle with the specified VIN. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/) for more information. | + + +### smartcar.unmount() +Remove Smartcar's listeners on the global window object. + +The Smartcar SDK uses a global 'message' event listener to recieve the +authorization code from the pop-up dialog. Call this method to remove the +event listener from the global window. + +**Kind**: instance method of [Smartcar](#Smartcar) ### Smartcar.AccessDenied ⇐ Error diff --git a/package.json b/package.json index c40a0b40..7846588f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@smartcar/auth", - "version": "2.5.0", + "version": "2.6.0", "description": "javascript auth sdk for the smartcar", "main": "dist/npm/sdk.js", "license": "MIT", diff --git a/src/sdk.js b/src/sdk.js index 2831f31d..5cfce5fd 100644 --- a/src/sdk.js +++ b/src/sdk.js @@ -348,6 +348,17 @@ class Smartcar { return false; }); } + + /** + * Remove Smartcar's listeners on the global window object. + * + * The Smartcar SDK uses a global 'message' event listener to recieve the + * authorization code from the pop-up dialog. Call this method to remove the + * event listener from the global window. + */ + unmount() { + window.removeEventListener('message', this.messageHandler); + } } /** diff --git a/test/unit/sdk.test.js b/test/unit/sdk.test.js index ff01d8dc..66eafdd7 100644 --- a/test/unit/sdk.test.js +++ b/test/unit/sdk.test.js @@ -990,5 +990,21 @@ describe('sdk', () => { document.getElementById(id).click(); expect(mockOpen).toHaveBeenCalled(); }); + + test('unmount removes the eventListener from the window object', () => { + const mockAddEventListener = jest.fn(); + const mockRemoveEventListener = jest.fn(); + + window.addEventListener = mockAddEventListener; + window.removeEventListener = mockRemoveEventListener; + + const smartcar = new Smartcar(options); + smartcar.unmount(); + + expect(mockAddEventListener) + .toHaveBeenCalledWith('message', smartcar.messageHandler); + expect(mockRemoveEventListener) + .toHaveBeenCalledWith('message', smartcar.messageHandler); + }); }); });