diff --git a/ui/app/adapters/job.js b/ui/app/adapters/job.js index a75da89d3..9bb5a2485 100644 --- a/ui/app/adapters/job.js +++ b/ui/app/adapters/job.js @@ -1,5 +1,6 @@ import { inject as service } from '@ember/service'; import Watchable from './watchable'; +import addToPath from 'nomad-ui/utils/add-to-path'; export default Watchable.extend({ system: service(), @@ -118,14 +119,3 @@ function associateNamespace(url, namespace) { } return url; } - -function addToPath(url, extension = '') { - const [path, params] = url.split('?'); - let newUrl = `${path}${extension}`; - - if (params) { - newUrl += `?${params}`; - } - - return newUrl; -} diff --git a/ui/app/utils/add-to-path.js b/ui/app/utils/add-to-path.js new file mode 100644 index 000000000..7b76e263b --- /dev/null +++ b/ui/app/utils/add-to-path.js @@ -0,0 +1,11 @@ +// Adds a string to the end of a URL path while being mindful of query params +export default function addToPath(url, extension = '') { + const [path, params] = url.split('?'); + let newUrl = `${path}${extension}`; + + if (params) { + newUrl += `?${params}`; + } + + return newUrl; +} diff --git a/ui/tests/unit/utils/add-to-path-test.js b/ui/tests/unit/utils/add-to-path-test.js new file mode 100644 index 000000000..6317cfad0 --- /dev/null +++ b/ui/tests/unit/utils/add-to-path-test.js @@ -0,0 +1,32 @@ +import { module, test } from 'qunit'; +import addToPath from 'nomad-ui/utils/add-to-path'; + +const testCases = [ + { + name: 'Only domain', + in: ['https://domain.com', '/path'], + out: 'https://domain.com/path', + }, + { + name: 'Deep path', + in: ['https://domain.com/a/path', '/to/nowhere'], + out: 'https://domain.com/a/path/to/nowhere', + }, + { + name: 'With Query Params', + in: ['https://domain.com?interesting=development', '/this-is-an'], + out: 'https://domain.com/this-is-an?interesting=development', + }, +]; + +module('Unit | Util | addToPath', function() { + testCases.forEach(testCase => { + test(testCase.name, function(assert) { + assert.equal( + addToPath.apply(null, testCase.in), + testCase.out, + `[${testCase.in.join(', ')}] => ${testCase.out}` + ); + }); + }); +});