Skip to content

Commit

Permalink
feat: Merge pull request #53 from smartcar/unmount
Browse files Browse the repository at this point in the history
Introduce unmount method.
  • Loading branch information
sankethkatta committed Jan 8, 2020
2 parents daca4b7 + a054b97 commit 9524c4a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ npm install @smartcar/auth
### Smartcar CDN

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

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

<!-- Please do not modify or remove this, it is used by the build process -->
[version]: 2.5.0
[version]: 2.6.0
11 changes: 11 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Smartcar JavaScript SDK documentation.
* [.getAuthUrl(options)](#Smartcar+getAuthUrl) ⇒ <code>String</code>
* [.openDialog(options)](#Smartcar+openDialog)
* [.addClickHandler(options)](#Smartcar+addClickHandler)
* [.unmount()](#Smartcar+unmount)
* _static_
* [.AccessDenied](#Smartcar.AccessDenied) ⇐ <code>Error</code>
* [new Smartcar.AccessDenied(message)](#new_Smartcar.AccessDenied_new)
Expand Down Expand Up @@ -111,6 +112,16 @@ On-click event calls openDialog when the specified element is clicked.
| [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. |
| [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. |

<a name="Smartcar+unmount"></a>

### 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 [<code>Smartcar</code>](#Smartcar)
<a name="Smartcar.AccessDenied"></a>

### Smartcar.AccessDenied ⇐ <code>Error</code>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 11 additions & 0 deletions src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions test/unit/sdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 9524c4a

Please sign in to comment.