From 833d694e7a68ebb649397af5477f155d3512706a Mon Sep 17 00:00:00 2001 From: Jared Galanis Date: Tue, 9 Jul 2024 16:28:18 -0400 Subject: [PATCH] fix tests --- app/services/policies.js | 10 ++- tests/unit/services/metadata-schema-test.js | 4 +- tests/unit/services/policies-test.js | 79 ++++++++------------- 3 files changed, 36 insertions(+), 57 deletions(-) diff --git a/app/services/policies.js b/app/services/policies.js index 3b108896..8dc54599 100644 --- a/app/services/policies.js +++ b/app/services/policies.js @@ -133,12 +133,10 @@ export default class PoliciesService extends Service { @task(function* (repos) { return yield all( repos.map((repoInfo) => - get(this, 'store') - .findRecord('repository', repoInfo.url) - .then((repo) => { - repo.set('_selected', repoInfo.selected); - return repo; - }), + this.store.findRecord('repository', repoInfo.url).then((repo) => { + repo.set('_selected', repoInfo.selected); + return repo; + }), ), ); }) diff --git a/tests/unit/services/metadata-schema-test.js b/tests/unit/services/metadata-schema-test.js index edd8e35a..e8f87622 100644 --- a/tests/unit/services/metadata-schema-test.js +++ b/tests/unit/services/metadata-schema-test.js @@ -86,9 +86,7 @@ module('Unit | Service | metadata-schema', (hooks) => { const _fetchSchemasSpy = sinon.spy(service, '_fetchSchemas'); - service.getMetadataSchemas(['moo', 'too']); - - await settled(); + await service.getMetadataSchemas(['moo', 'too']); assert.ok(_fetchSchemasSpy.calledTwice, 'fetch schema was called twice'); }); diff --git a/tests/unit/services/policies-test.js b/tests/unit/services/policies-test.js index f3c5278b..131be9cc 100644 --- a/tests/unit/services/policies-test.js +++ b/tests/unit/services/policies-test.js @@ -8,16 +8,6 @@ module('Unit | Service | policies', (hooks) => { setupTest(hooks); setupMirage(hooks); - let tmp; - hooks.before(() => { - tmp = QUnit.onUncaughtException; - QUnit.onUncaughtException = () => {}; - }); - - hooks.after(() => { - QUnit.onUncaughtException = tmp; - }); - test('good response returns array of Promises of Policy objects', async function (assert) { assert.expect(5); @@ -43,37 +33,32 @@ module('Unit | Service | policies', (hooks) => { const sub = { id: '0' }; - service - .get('getRepositories') - .perform(sub) - .then((rules) => { - assert.ok(Array.isArray(rules.required), 'rules.required should be an array'); - assert.ok(Array.isArray(rules.optional), 'rules.optional should be an array'); - assert.ok(Array.isArray(rules['one-of']), "rules['one-of'] should be an array"); - - assert.strictEqual(rules.required.length, 1, 'Unexpected number of required repos'); - assert.strictEqual(rules.optional.length, 1, 'Unexpected number of optional repos'); - - assert.strictEqual(rules['one-of'].length, 1, 'Unexpected number of choice groups'); - assert.strictEqual(rules['one-of'][0].length, 2, 'Unexpected number of repos in choice group 1'); - - rules.required.forEach((repo) => - assert.strictEqual(repo.name, 'PubMed Central - NATIONAL INSTITUTE OF HEALTH'), - ); - rules.optional.forEach((repo) => assert.strictEqual(repo.name, 'JScholarship')); - assert.ok( - rules['one-of'][0].some((repo) => { - return repo.name === 'JScholarship' || repo.name === 'PubMed Central - NATIONAL INSTITUTE OF HEALTH'; - }), - ); - }); + const rules = await service.getRepositories.perform(sub); + + assert.ok(Array.isArray(rules.required), 'rules.required should be an array'); + assert.ok(Array.isArray(rules.optional), 'rules.optional should be an array'); + assert.ok(Array.isArray(rules['one-of']), "rules['one-of'] should be an array"); + + assert.strictEqual(rules.required.length, 1, 'Unexpected number of required repos'); + assert.strictEqual(rules.optional.length, 1, 'Unexpected number of optional repos'); + + assert.strictEqual(rules['one-of'].length, 1, 'Unexpected number of choice groups'); + assert.strictEqual(rules['one-of'][0].length, 2, 'Unexpected number of repos in choice group 1'); + + rules.required.forEach((repo) => assert.strictEqual(repo.name, 'PubMed Central - NATIONAL INSTITUTE OF HEALTH')); + rules.optional.forEach((repo) => assert.strictEqual(repo.name, 'JScholarship')); + assert.ok( + rules['one-of'][0].some((repo) => { + return repo.name === 'JScholarship' || repo.name === 'PubMed Central - NATIONAL INSTITUTE OF HEALTH'; + }), + ); }); /** * Make sure that invoking the getPolicies function invokes 'fetch' and that * a non-2xx response code triggers an error */ - test('policy endpoint should throw error on non-200 response', function (assert) { + test('policy endpoint should throw error on non-200 response', async function (assert) { assert.expect(1); this.server.get('/policy/policies', () => new Response(404)); @@ -82,19 +67,18 @@ module('Unit | Service | policies', (hooks) => { const sub = EmberObject.create({ id: 'moo' }); - service - .get('getPolicies') - .perform(sub) - .catch((e) => { - assert.ok(e.message.includes('Recieved response 404')); - }); + try { + await service.getPolicies.perform(sub); + } catch (e) { + assert.ok(e.message.includes('Recieved response 404')); + } }); /** * Make sure that invoking getRepositories function also invokes 'fetch' and that * a non-2xx response code triggers an error */ - test('repo endpoint should throw error on non-200 response', function (assert) { + test('repo endpoint should throw error on non-200 response', async function (assert) { assert.expect(1); this.server.get('/policy/repositories', () => new Response(404)); @@ -103,11 +87,10 @@ module('Unit | Service | policies', (hooks) => { const sub = EmberObject.create({ id: 'moo' }); - service - .get('getRepositories') - .perform(sub) - .catch((e) => { - assert.ok(e.message.includes('Recieved response 404')); - }); + try { + await service.getRepositories.perform(sub); + } catch (e) { + assert.ok(e.message.includes('Recieved response 404')); + } }); });