diff --git a/ui/app/adapters/job.js b/ui/app/adapters/job.js index 1d5919195..e13047815 100644 --- a/ui/app/adapters/job.js +++ b/ui/app/adapters/job.js @@ -4,15 +4,6 @@ import Watchable from './watchable'; export default Watchable.extend({ system: service(), - buildQuery() { - const namespace = this.get('system.activeNamespace.id'); - - if (namespace && namespace !== 'default') { - return { namespace }; - } - return {}; - }, - findAll() { const namespace = this.get('system.activeNamespace'); return this._super(...arguments).then(data => { @@ -30,22 +21,22 @@ export default Watchable.extend({ return this._super(store, type, id, snapshot, namespaceQuery); }, + urlForFindAll() { + const url = this._super(...arguments); + const namespace = this.get('system.activeNamespace.id'); + return associateNamespace(url, namespace); + }, + urlForFindRecord(id, type, hash) { const [name, namespace] = JSON.parse(id); let url = this._super(name, type, hash); - if (namespace && namespace !== 'default') { - url += `?namespace=${namespace}`; - } - return url; + return associateNamespace(url, namespace); }, xhrKey(url, method, options = {}) { const plainKey = this._super(...arguments); const namespace = options.data && options.data.namespace; - if (namespace) { - return `${plainKey}?namespace=${namespace}`; - } - return plainKey; + return associateNamespace(plainKey, namespace); }, relationshipFallbackLinks: { @@ -53,7 +44,7 @@ export default Watchable.extend({ }, fetchRawDefinition(job) { - const url = this.buildURL('job', job.get('id'), job, 'findRecord'); + const url = this.urlForFindRecord(job.get('id'), 'job'); return this.ajax(url, 'GET'); }, @@ -70,6 +61,13 @@ export default Watchable.extend({ }, }); +function associateNamespace(url, namespace) { + if (namespace && namespace !== 'default') { + url += `?namespace=${namespace}`; + } + return url; +} + function addToPath(url, extension = '') { const [path, params] = url.split('?'); let newUrl = `${path}${extension}`; diff --git a/ui/tests/unit/adapters/job-test.js b/ui/tests/unit/adapters/job-test.js index aa6827d5a..c38d20036 100644 --- a/ui/tests/unit/adapters/job-test.js +++ b/ui/tests/unit/adapters/job-test.js @@ -22,6 +22,7 @@ moduleForAdapter('job', 'Unit | Adapter | Job', { ], beforeEach() { window.sessionStorage.clear(); + window.localStorage.clear(); this.server = startMirage(); this.server.create('node'); @@ -33,7 +34,7 @@ moduleForAdapter('job', 'Unit | Adapter | Job', { }, }); -test('The job summary is stitched into the job request', function(assert) { +test('The job endpoint is the only required endpoint for fetching a job', function(assert) { const { pretender } = this.server; const jobName = 'job-1'; const jobNamespace = 'default'; @@ -43,8 +44,25 @@ test('The job summary is stitched into the job request', function(assert) { assert.deepEqual( pretender.handledRequests.mapBy('url'), - ['/v1/namespaces', `/v1/job/${jobName}`], - 'The two requests made are /namespaces and /job/:id' + [`/v1/job/${jobName}`], + 'The only request made is /job/:id' + ); +}); + +test('When a namespace is in localStorage and the requested job is in the default namespace, the namespace query param is left out', function(assert) { + window.localStorage.nomadActiveNamespace = 'red-herring'; + + const { pretender } = this.server; + const jobName = 'job-1'; + const jobNamespace = 'default'; + const jobId = JSON.stringify([jobName, jobNamespace]); + + this.subject().findRecord(null, { modelName: 'job' }, jobId); + + assert.deepEqual( + pretender.handledRequests.mapBy('url'), + [`/v1/job/${jobName}`], + 'The request made is /job/:id with no namespace query param' ); }); @@ -58,8 +76,8 @@ test('When the job has a namespace other than default, it is in the URL', functi assert.deepEqual( pretender.handledRequests.mapBy('url'), - ['/v1/namespaces', `/v1/job/${jobName}?namespace=${jobNamespace}`], - 'The two requests made are /namespaces and /job/:id?namespace=:namespace' + [`/v1/job/${jobName}?namespace=${jobNamespace}`], + 'The only request made is /job/:id?namespace=:namespace' ); }); @@ -137,11 +155,6 @@ test('findRecord can be watched', function(assert) { request(); assert.equal( pretender.handledRequests[0].url, - '/v1/namespaces', - 'First request is for namespaces' - ); - assert.equal( - pretender.handledRequests[1].url, '/v1/job/job-1?index=1', 'Second request is a blocking request for job-1' ); @@ -149,7 +162,7 @@ test('findRecord can be watched', function(assert) { return wait().then(() => { request(); assert.equal( - pretender.handledRequests[2].url, + pretender.handledRequests[1].url, '/v1/job/job-1?index=2', 'Third request is a blocking request with an incremented index param' );