-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(UrlHelpers): [#715] created safelyAppendUrlParameter method
- Loading branch information
1 parent
87ce764
commit 59585bd
Showing
2 changed files
with
29 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export function safelyAppendUrlParameter (url: string, parameterKey: string, parameterValue: string): string { | ||
const separator = url.includes('?') ? '&' : '?'; | ||
return `${url}${separator}${parameterKey}=${parameterValue}`; | ||
} |
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 { safelyAppendUrlParameter } from '../../../src/helpers/url'; | ||
|
||
describe('safelyAppendUrlParameter method', function () { | ||
let fixtureUrl: string; | ||
const fixtureParameterKey = 'parameterKey'; | ||
const fixtureParameterValue = 'parameterValue'; | ||
|
||
describe('given the url already has parameter', function () { | ||
it('should return the url with the parameter correctly appended', function () { | ||
fixtureUrl = 'https://www.domain.tld/path/to/page?key=value'; | ||
const assertionURL = `${fixtureUrl}&${fixtureParameterKey}=${fixtureParameterValue}`; | ||
expect(safelyAppendUrlParameter(fixtureUrl, fixtureParameterKey, fixtureParameterValue)) | ||
.toEqual(assertionURL); | ||
}); | ||
}); | ||
|
||
describe('given the url does not already have parameter', function () { | ||
it('should return the url with the parameter correctly appended', function () { | ||
fixtureUrl = 'https://www.domain.tld/path/to/page'; | ||
const assertionURL = `${fixtureUrl}?${fixtureParameterKey}=${fixtureParameterValue}`; | ||
expect(safelyAppendUrlParameter(fixtureUrl, fixtureParameterKey, fixtureParameterValue)) | ||
.toEqual(assertionURL); | ||
}); | ||
}); | ||
}); |