Skip to content

Commit 9524c4a

Browse files
committed
feat: Merge pull request #53 from smartcar/unmount
Introduce unmount method.
2 parents daca4b7 + a054b97 commit 9524c4a

File tree

6 files changed

+42
-4
lines changed

6 files changed

+42
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ npm install @smartcar/auth
2424
### Smartcar CDN
2525

2626
```html
27-
<script src="https://javascript-sdk.smartcar.com/2.5.0/sdk.js"></script>
27+
<script src="https://javascript-sdk.smartcar.com/2.6.0/sdk.js"></script>
2828
```
2929

3030
Before v2.2.0, the SDK was versioned as follows:
@@ -192,4 +192,4 @@ https://application-backend.com/page?error=access_denied&error_description=User+
192192
[tag-image]: https://img.shields.io/github/tag/smartcar/javascript-sdk.svg
193193

194194
<!-- Please do not modify or remove this, it is used by the build process -->
195-
[version]: 2.5.0
195+
[version]: 2.6.0

doc/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Smartcar JavaScript SDK documentation.
2828
* [.getAuthUrl(options)](#Smartcar+getAuthUrl) ⇒ <code>String</code>
2929
* [.openDialog(options)](#Smartcar+openDialog)
3030
* [.addClickHandler(options)](#Smartcar+addClickHandler)
31+
* [.unmount()](#Smartcar+unmount)
3132
* _static_
3233
* [.AccessDenied](#Smartcar.AccessDenied) ⇐ <code>Error</code>
3334
* [new Smartcar.AccessDenied(message)](#new_Smartcar.AccessDenied_new)
@@ -111,6 +112,16 @@ On-click event calls openDialog when the specified element is clicked.
111112
| [options.vehicleInfo.make] | <code>String</code> | | `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. |
112113
| [options.singleSelect] | <code>Boolean</code> \| <code>Object</code> | | 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. |
113114

115+
<a name="Smartcar+unmount"></a>
116+
117+
### smartcar.unmount()
118+
Remove Smartcar's listeners on the global window object.
119+
120+
The Smartcar SDK uses a global 'message' event listener to recieve the
121+
authorization code from the pop-up dialog. Call this method to remove the
122+
event listener from the global window.
123+
124+
**Kind**: instance method of [<code>Smartcar</code>](#Smartcar)
114125
<a name="Smartcar.AccessDenied"></a>
115126

116127
### Smartcar.AccessDenied ⇐ <code>Error</code>

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@smartcar/auth",
3-
"version": "2.5.0",
3+
"version": "2.6.0",
44
"description": "javascript auth sdk for the smartcar",
55
"main": "dist/npm/sdk.js",
66
"license": "MIT",

src/sdk.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,17 @@ class Smartcar {
348348
return false;
349349
});
350350
}
351+
352+
/**
353+
* Remove Smartcar's listeners on the global window object.
354+
*
355+
* The Smartcar SDK uses a global 'message' event listener to recieve the
356+
* authorization code from the pop-up dialog. Call this method to remove the
357+
* event listener from the global window.
358+
*/
359+
unmount() {
360+
window.removeEventListener('message', this.messageHandler);
361+
}
351362
}
352363

353364
/**

test/unit/sdk.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,5 +990,21 @@ describe('sdk', () => {
990990
document.getElementById(id).click();
991991
expect(mockOpen).toHaveBeenCalled();
992992
});
993+
994+
test('unmount removes the eventListener from the window object', () => {
995+
const mockAddEventListener = jest.fn();
996+
const mockRemoveEventListener = jest.fn();
997+
998+
window.addEventListener = mockAddEventListener;
999+
window.removeEventListener = mockRemoveEventListener;
1000+
1001+
const smartcar = new Smartcar(options);
1002+
smartcar.unmount();
1003+
1004+
expect(mockAddEventListener)
1005+
.toHaveBeenCalledWith('message', smartcar.messageHandler);
1006+
expect(mockRemoveEventListener)
1007+
.toHaveBeenCalledWith('message', smartcar.messageHandler);
1008+
});
9931009
});
9941010
});

0 commit comments

Comments
 (0)