From 49dffc424a93eeab46bce6b28c0b6dd9b26eeb5a Mon Sep 17 00:00:00 2001 From: John Abrahams Date: Wed, 24 Jan 2024 14:52:31 -0500 Subject: [PATCH 1/2] UI shouldn't send empty requests to metadata schema service --- app/services/metadata-schema.js | 11 ++++++++++- .../integration/components/workflow-metadata-test.js | 2 +- tests/unit/services/metadata-schema-test.js | 11 ++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/services/metadata-schema.js b/app/services/metadata-schema.js index b1e2b244..f5f9703f 100644 --- a/app/services/metadata-schema.js +++ b/app/services/metadata-schema.js @@ -58,6 +58,10 @@ export default class MetadataSchemaService extends Service { * @returns {array} list of schemas relevant to the given repositories */ async getMetadataSchemas(repositories) { + if (!repositories) { + return []; + } + const areObjects = repositories.map((repos) => typeof repos).includes('object'); if (areObjects) { // If we've gotten repository objects, map them to their IDs @@ -71,6 +75,10 @@ export default class MetadataSchemaService extends Service { }, }; + if (!Array.isArray(repositories) || repositories.length === 0) { + return []; + } + try { let response = await this._fetchSchemas(this.url(repositories, true), options); if (response.status >= 400) { @@ -407,7 +415,8 @@ export default class MetadataSchemaService extends Service { 'volume', ]; - const schemas = await this.getMetadataSchemas(await submission.repositories); + const repos = await submission.repositories.toArray(); + const schemas = await this.getMetadataSchemas(repos); const titleMap = this.getFieldTitleMap(schemas); const metadata = JSON.parse(submission.metadata); diff --git a/tests/integration/components/workflow-metadata-test.js b/tests/integration/components/workflow-metadata-test.js index 64b50508..7a5346c5 100644 --- a/tests/integration/components/workflow-metadata-test.js +++ b/tests/integration/components/workflow-metadata-test.js @@ -16,7 +16,7 @@ module('Integration | Component | workflow-metadata', (hooks) => { setupMirage(hooks); hooks.beforeEach(function () { - const repositories = A(); + const repositories = A([{ id: 0 }]); const journal = EmberObject.create({ issns: [], }); diff --git a/tests/unit/services/metadata-schema-test.js b/tests/unit/services/metadata-schema-test.js index 7a9aa27e..db005c23 100644 --- a/tests/unit/services/metadata-schema-test.js +++ b/tests/unit/services/metadata-schema-test.js @@ -216,7 +216,7 @@ module('Unit | Service | metadata-schema', (hooks) => { test('Should format complex metadata', async function (assert) { const service = this.owner.lookup('service:metadata-schema'); const submission = EmberObject.create({ - repositories: [], + repositories: [{ id: 0 }], metadata: JSON.stringify({ issns: [{ issn: '123-moo' }, { issn: 'moo-321', pubType: 'Print' }], }), @@ -270,4 +270,13 @@ module('Unit | Service | metadata-schema', (hooks) => { assert.deepEqual(service.mergeBlobs(b1, b2, list), expected); }); + + test('No backend request if no repositories are passed', async function (assert) { + const service = this.owner.lookup('service:metadata-schema'); + const serviceFetchFake = sinon.replace(service, '_fetchSchemas', sinon.fake()); + + service.getMetadataSchemas([]); + + assert.ok(serviceFetchFake.notCalled, '_fetchSchemas should have been called'); + }); }); From 587725ff2c1a374db24e6c316e0ae347ced38cb1 Mon Sep 17 00:00:00 2001 From: John Abrahams Date: Wed, 24 Jan 2024 15:21:29 -0500 Subject: [PATCH 2/2] Fix bad assert message --- tests/unit/services/metadata-schema-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/services/metadata-schema-test.js b/tests/unit/services/metadata-schema-test.js index db005c23..d9f499c9 100644 --- a/tests/unit/services/metadata-schema-test.js +++ b/tests/unit/services/metadata-schema-test.js @@ -277,6 +277,6 @@ module('Unit | Service | metadata-schema', (hooks) => { service.getMetadataSchemas([]); - assert.ok(serviceFetchFake.notCalled, '_fetchSchemas should have been called'); + assert.ok(serviceFetchFake.notCalled, '_fetchSchemas should not have been called'); }); });