Repository: auth0/auth0-spa-js
Pull request #61 (2019-07-11 09:50:38)

Made options argument for popup and redirect optional
### Description

This PR makes the options paramters on `loginWithPopup` and `loginWithRedirect` optional. Currently, when a developer wishes to not set any options on these calls, they must pass an empty object:

`auth0Client.loginWithRedirect({})`

All of the properties on the options type are optional, so it makes sense that the argument itself be optional to offer a nicer syntax:

`auth0Client.loginWithRedirect()`

### References

This PR has come about because of some feedback on our internal channels.

### Testing

Added a unit test for the `loginWithRedirect` to make sure that the argument was optional and would compile.

_Modified_ existing tests for `loginWithPopup` that were already passing an empty object, so that they now pass no arguments.

- [x] This change adds test coverage for new/changed/fixed functionality

### Checklist

- [x] I have added documentation for new/changed functionality in this PR or in auth0.com/docs
- [x] All active GitHub checks for tests, formatting, and security are passing
- [x] The correct base branch is being used, if not `master`



Patch:
diff --git a/src/Auth0Client.ts b/src/Auth0Client.ts
index 06944cc..891c387 100644
--- a/src/Auth0Client.ts
+++ b/src/Auth0Client.ts
@@ -98,7 +98,7 @@ export default class Auth0Client {
    *
    * @param options
    */
-  public async loginWithPopup(options: PopupLoginOptions) {
+  public async loginWithPopup(options: PopupLoginOptions = {}) {
     const popup = await openPopup();
     const { ...authorizeOptions } = options;
     const stateIn = encodeState(createRandomString());
@@ -191,7 +191,7 @@ export default class Auth0Client {
    *
    * @param options
    */
-  public async loginWithRedirect(options: RedirectLoginOptions) {
+  public async loginWithRedirect(options: RedirectLoginOptions = {}) {
     const { redirect_uri, appState, ...authorizeOptions } = options;
     const stateIn = encodeState(createRandomString());
     const nonceIn = createRandomString();


Test patch:
diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts
index d781d06..88fd920 100644
--- a/__tests__/index.test.ts
+++ b/__tests__/index.test.ts
@@ -246,6 +246,12 @@ describe('Auth0', () => {
         { daysUntilExpire: 1 }
       );
     });
+    it('can be called with no arguments', async () => {
+      const { auth0, utils } = await setup();
+
+      await auth0.loginWithPopup();
+      expect(utils.openPopup).toHaveBeenCalled();
+    });
   });
   describe('loginWithRedirect()', () => {
     const REDIRECT_OPTIONS = {
@@ -347,6 +353,15 @@ describe('Auth0', () => {
         `https://test.auth0.com/authorize?query=params${TEST_TELEMETRY_QUERY_STRING}`
       );
     });
+    it('can be called with no arguments', async () => {
+      const { auth0 } = await setup();
+
+      await auth0.loginWithRedirect();
+
+      expect(window.location.assign).toHaveBeenCalledWith(
+        `https://test.auth0.com/authorize?query=params${TEST_TELEMETRY_QUERY_STRING}`
+      );
+    });
   });
   describe('handleRedirectCallback()', () => {
     it('throws when there is no query string', async () => {