Skip to content

Commit

Permalink
Add some controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jabrah committed Nov 16, 2023
1 parent 4cd9c48 commit b41ff17
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 78 deletions.
101 changes: 61 additions & 40 deletions tests/unit/controllers/grants/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,82 @@ import Service from '@ember/service';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

class MockConfigService extends Service {
getStaticConfig() {
return Promise.resolve({ branding: { stylesheet: '', pages: { faqUrl: '' } } });
}
addCss() {}
}

module('Unit | Controller | grants/index', (hooks) => {
setupTest(hooks);

hooks.beforeEach(function () {
const mockStaticConfig = Service.extend({
getStaticConfig: () =>
Promise.resolve({
branding: {
stylesheet: '',
pages: {
faqUrl: '',
},
},
}),
addCss: () => {},
});
this.owner.register('service:app-static-config', MockConfigService);

this.owner.register('service:app-static-config', mockStaticConfig);
this.controller = this.owner.lookup('controller:grants/index');
this.controller.currentUser = { user: { id: 0, isAdmin: false, isSubmitter: true } };
});

// Replace this with your real tests.
test('it exists', function (assert) {
let controller = this.owner.lookup('controller:grants/index');
assert.ok(controller);
test('properly returns admin roles', function (assert) {
this.controller.currentUser = { user: { id: 0, isAdmin: true, isSubmitter: true } };
assert.strictEqual(
this.controller.get('adminColumns'),
this.controller.get('columns'),
'Should return admin columns'
);
});

test('properly returns admin roles', function (assert) {
this.owner.register(
'service:current-user',
EmberObject.extend({
user: { isAdmin: true },
})
test('properly returns submitter roles', function (assert) {
assert.strictEqual(
this.controller.get('piColumns'),
this.controller.get('columns'),
'Should return submitter columns'
);
});

let controller = this.owner.lookup('controller:grants/index');
test('displayAction updates tracked query params', function (assert) {
assert.equal(this.controller.page, 1, 'Page param should have default value');
assert.equal(this.controller.pageSize, 10, 'Page size param should have default value');

controller.set(
'currentUser.user',
EmberObject.create({
isAdmin: true,
})
);
this.controller.send('displayAction', { currentPageNumber: 10, pageSize: 2 });

assert.strictEqual(controller.get('adminColumns'), controller.get('columns'));
assert.equal(this.controller.page, 10, 'Page param should be updated');
assert.equal(this.controller.pageSize, 2, 'Page size param should be updated');
});

test('properly returns submitter roles', function (assert) {
let controller = this.owner.lookup('controller:grants/index');
controller.set(
'currentUser.user',
EmberObject.create({
isSubmitter: true,
})
);
test('doQuery', async function (assert) {
assert.expect(8);

this.controller.store = {
query: (model, query) => {
// assert.equal(model, 'grant', 'Only grants should be queried');
switch (model) {
case 'grant':
assert.ok(query.page, 'Query should have pagination info');
return Promise.resolve([{ id: 10 }, { id: 11 }]);
case 'submission':
assert.notOk(query.page, 'Query should not have pagination info');
return Promise.resolve([
{ id: 20, grants: [{ id: 11 }] },
{ id: 21, grants: [{ id: 11 }] },
]);
default:
assert.ok(false, 'Only submissions and grants should be queried here');
}
return Promise.resolve({});
},
};

assert.notOk(this.controller.queuedModel, 'Queued model should be empty');

await this.controller.doQuery({});

assert.strictEqual(controller.get('piColumns'), controller.get('columns'));
const result = this.controller.queuedModel;
assert.ok(result.grantMap, 'Grant mapping should exist');
assert.notOk(result.meta, 'No paging info present due to mocking');
assert.equal(result.grantMap.length, 2, 'grantMap should have 2 grants');
assert.equal(result.grantMap[0].submissions.length, 0, 'First grant should have 0 submissions');
assert.equal(result.grantMap[1].submissions.length, 2, 'Second grant should have 2 submissions');
});
});
85 changes: 47 additions & 38 deletions tests/unit/controllers/submissions/index-test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,63 @@
/* eslint-disable ember/no-classic-classes */
import EmberObject from '@ember/object';
import Service from '@ember/service';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Controller | submissions/index', (hooks) => {
class MockConfigService extends Service {
getStaticConfig() {
return Promise.resolve({ branding: { stylesheet: '', pages: { faqUrl: '' } } });
}
addCss() {}
}

module('Unit | Controller | submissions/index', function (hooks) {
setupTest(hooks);

hooks.beforeEach(function () {
const mockStaticConfig = Service.extend({
getStaticConfig: () =>
Promise.resolve({
branding: {
stylesheet: '',
pages: {
faqUrl: '',
},
},
}),
addCss: () => {},
});

this.owner.register('service:app-static-config', mockStaticConfig);
});
this.owner.register('service:app-static-config', MockConfigService);

// Replace this with your real tests.
test('it exists', function (assert) {
let controller = this.owner.lookup('controller:submissions/index');
assert.ok(controller);
this.controller = this.owner.lookup('controller:submissions/index');
this.controller.currentUser = { user: { id: 0, isAdmin: false, isSubmitter: true } };
});

test('properly returns admin roles', function (assert) {
let controller = this.owner.lookup('controller:submissions/index');
controller.set(
'currentUser.user',
EmberObject.create({
isAdmin: true,
})
);
assert.strictEqual(controller.get('columns.length'), 6);
this.controller.currentUser = { user: { id: 0, isAdmin: true, isSubmitter: true } };
assert.strictEqual(this.controller.columns.length, 6, 'Should get admin columns (with 6 cols)');
});

test('properly returns submitter roles', function (assert) {
let controller = this.owner.lookup('controller:submissions/index');
controller.set(
'currentUser.user',
EmberObject.create({
isSubmitter: true,
})
);
assert.strictEqual(controller.get('columns.length'), 7);
assert.strictEqual(this.controller.get('columns.length'), 7, 'Should get submitter columns (with 7 cols)');
});

/**
* Would be more helpful in an Acceptance test in order to check
* that URL query params are also updated
*/
test('displayAction updates tracked query params', function (assert) {
assert.equal(this.controller.page, 1, 'Page param should have default value');
assert.equal(this.controller.pageSize, 10, 'Page size param should have default value');

this.controller.send('displayAction', { currentPageNumber: 10, pageSize: 2 });

assert.equal(this.controller.page, 10, 'Page param should be updated');
assert.equal(this.controller.pageSize, 2, 'Page size param should be updated');
});

test('doQuery makes a store query', async function (assert) {
assert.expect(4);

this.controller.store = {
query: (model, query) => {
assert.equal(model, 'submission', 'Only submissions should be queried');
assert.ok(query.page, 'Query should have pagination info');
return Promise.resolve({});
},
};

// Not called from a route's model hook, so no queued model
assert.notOk(this.controller.queuedModel, 'Queued model undefined');
// Don't actually need any params sent
await this.controller.doQuery({});

assert.deepEqual(this.controller.queuedModel, { submissions: {}, meta: undefined }, 'Queued model updated');
});
});

0 comments on commit b41ff17

Please sign in to comment.