diff --git a/client/state/login/test/utils.js b/client/state/login/test/utils.js new file mode 100644 index 0000000000000..f03d0eba2fa82 --- /dev/null +++ b/client/state/login/test/utils.js @@ -0,0 +1,44 @@ +import { isPartnerSignupQuery } from '../utils'; + +describe( 'isPartnerSignupQuery', () => { + it( 'should return false when currentQuery is undefined', () => { + const result = isPartnerSignupQuery( undefined ); + expect( result ).toBe( false ); + } ); + + it( 'should return false when currentQuery is null', () => { + const result = isPartnerSignupQuery( null ); + expect( result ).toBe( false ); + } ); + + it( 'should return true when currentQuery has is_partner_signup', () => { + const currentQuery = { is_partner_signup: true }; + const result = isPartnerSignupQuery( currentQuery ); + expect( result ).toBe( true ); + } ); + + it( 'should return true when currentQuery has redirect_to with partner-signup', () => { + const currentQuery = { redirect_to: 'https://woocommerce.com/partner-signup' }; + const result = isPartnerSignupQuery( currentQuery ); + expect( result ).toBe( true ); + } ); + + it( 'should return true when currentQuery has oauth2_redirect with partner-signup', () => { + const currentQuery = { oauth2_redirect: 'https://woocommerce.com/partner-signup' }; + const result = isPartnerSignupQuery( currentQuery ); + expect( result ).toBe( true ); + } ); + + it( 'should throw an error when an unexpected error occurs', () => { + const currentQuery = { redirect_to: 'https://woocommerce.com/partner-signup' }; + const originalDecodeURIComponent = global.decodeURIComponent; + global.decodeURIComponent = jest.fn( () => { + throw new Error( 'Unexpected error' ); + } ); + + expect( () => isPartnerSignupQuery( currentQuery ) ).toThrow( 'Unexpected error' ); + + // Restore the original decodeURIComponent function + global.decodeURIComponent = originalDecodeURIComponent; + } ); +} ); diff --git a/client/state/login/utils.js b/client/state/login/utils.js index f12c932ce1ca4..52ca29ffc1e75 100644 --- a/client/state/login/utils.js +++ b/client/state/login/utils.js @@ -190,18 +190,27 @@ export function isPartnerSignupQuery( currentQuery ) { return true; } - // Handles login through /log-in/?redirect_to=... - if ( typeof currentQuery?.redirect_to === 'string' ) { - return /woocommerce\.(?:com|test)\/partner-signup/.test( - decodeURIComponent( currentQuery.redirect_to ) - ); - } + try { + // Handles login through /log-in/?redirect_to=... + if ( typeof currentQuery?.redirect_to === 'string' ) { + return /woocommerce\.(?:com|test)\/partner-signup/.test( + decodeURIComponent( currentQuery.redirect_to ) + ); + } - // Handles user creation through /start/wpcc?oauth2_redirect=... - if ( typeof currentQuery?.oauth2_redirect === 'string' ) { - return /woocommerce\.(?:com|test)\/partner-signup/.test( - decodeURIComponent( currentQuery.oauth2_redirect ) - ); + // Handles user creation through /start/wpcc?oauth2_redirect=... + if ( typeof currentQuery?.oauth2_redirect === 'string' ) { + return /woocommerce\.(?:com|test)\/partner-signup/.test( + decodeURIComponent( currentQuery.oauth2_redirect ) + ); + } + } catch ( e ) { + if ( e instanceof URIError ) { + // Ignore the URIError + return false; + } + // Should not happen, re-throw the error + throw e; } return false;