Skip to content

Commit

Permalink
refactor(UrlHelpers): [#715] created safelyAppendUrlParameter method
Browse files Browse the repository at this point in the history
  • Loading branch information
raiseandfall committed Sep 4, 2020
1 parent 87ce764 commit 59585bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/helpers/url.ts
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}`;
}
25 changes: 25 additions & 0 deletions test/application/helpers/url.test.ts
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);
});
});
});

0 comments on commit 59585bd

Please sign in to comment.