Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredgalanis committed Jul 9, 2024
1 parent ad44ce7 commit 833d694
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 57 deletions.
10 changes: 4 additions & 6 deletions app/services/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}),
),
);
})
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/services/metadata-schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
79 changes: 31 additions & 48 deletions tests/unit/services/policies-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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'));
}
});
});

0 comments on commit 833d694

Please sign in to comment.