-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update redirectUrl URL construction (#1667)
update redirectUrl URL construction to prioritize user-specified params over answers params J=SLAP-232 TEST=manual & auto - tested search bar and filter search component in chrome, safari, firefox, and ie11. Configure redirectUrl to be 'https://answers.yext.com/?query=test&another-param=stuff' and search for 'rose'. See that the redirectUrl preserve query param and other non-conflicting params from answers and user-specified ones - see the new jest tests passed
- Loading branch information
Showing
5 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import SearchParams from '../dom/searchparams'; | ||
|
||
/** | ||
* Construct a new redirect url with params saved in answers storage. | ||
* In the case of duplicate params, priorize user-specified params. | ||
* | ||
* @param {string} redirectUrl user-specified redirect url | ||
* @param {SearchParams} params url params saved in answers storage | ||
* @returns {URL} new redirect url including params from answers storage | ||
*/ | ||
export function constructRedirectUrl (redirectUrl, params) { | ||
const newRedirectUrl = new URL(redirectUrl); | ||
const redirectUrlParams = new SearchParams(newRedirectUrl.search); | ||
for (const [key, val] of params.entries()) { | ||
if (!redirectUrlParams.has(key)) { | ||
redirectUrlParams.set(key, val); | ||
} | ||
} | ||
newRedirectUrl.search = redirectUrlParams.toString(); | ||
return newRedirectUrl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { SearchParams } from '../../../src/ui'; | ||
import { constructRedirectUrl } from '../../../src/ui/tools/urlutils'; | ||
|
||
describe('constructRedirectUrl', () => { | ||
it('include answers params', () => { | ||
const userRedirectUrl = 'https://answers.yext.com/'; | ||
const params = new SearchParams('?answers-param=rose'); | ||
const newRedirectUrl = constructRedirectUrl(userRedirectUrl, params); | ||
expect(newRedirectUrl.search).toEqual('?answers-param=rose'); | ||
}); | ||
|
||
it('handle duplicate params', () => { | ||
const userRedirectUrl = 'https://answers.yext.com/?query=test'; | ||
const params = new SearchParams('?query=rose'); | ||
const newRedirectUrl = constructRedirectUrl(userRedirectUrl, params); | ||
expect(newRedirectUrl.search).toEqual('?query=test'); | ||
}); | ||
|
||
it('handle a mix of user-specified and answers params', () => { | ||
const userRedirectUrl = 'https://answers.yext.com/?query=test&context=abc'; | ||
const params = new SearchParams('?query=rose&filter=manager'); | ||
const newRedirectUrl = constructRedirectUrl(userRedirectUrl, params); | ||
expect(newRedirectUrl.search).toEqual('?query=test&context=abc&filter=manager'); | ||
}); | ||
}); |