Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Add abstract controller unit tests and translations (#900)
Browse files Browse the repository at this point in the history
* Add abstract-delete tests and ember-sinon-qunit

- Add unit tests for controller abstract-delete-controller
- Add dependency(devDependency) for ember-sinon-qunit for use in
  stubbing methods for proper test coverage

* Add unit tests abstract-paged-controller

- Add unit tests from abstract-paged-controller
- Update documentation with `@todo` about possible unintended behavior
  of the `sortByKey` action

The tests in this commit preserve the behavior as implemented. It is
possible that `sortByKey` should also set `nextStartKey` and `startKey`
to `null` as well.

* Add unit tests for abstract-report-controller

- Add unit tests for abstract-report-controller

* Add translations for _notifyReportError

- Add translations for `_notifyReportError` in
  abstract-report-controller
- Add unit test for `_notifyReportError`

* Add translations for _setReportTitle

- Add translations for `_setReportTitle` in abstract-report-controller
- Add unit tests for `_setReportTitle` with translations
  • Loading branch information
mkly authored and jkleinsc committed Dec 21, 2016
1 parent f7abcb9 commit ca37bf7
Show file tree
Hide file tree
Showing 6 changed files with 469 additions and 7 deletions.
21 changes: 17 additions & 4 deletions app/controllers/abstract-report-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ export default Ember.Controller.extend(DateFormat, ModalHelper, NumberFormat, Pa
},

_notifyReportError(errorMessage) {
let alertMessage = 'An error was encountered while generating the requested report. Please let your system administrator know that you have encountered an error.';
let i18n = this.get('i18n');
this.closeProgressModal();
this.displayAlert('Error Generating Report', alertMessage);
this.displayAlert(i18n.t('alerts.reportError'), i18n.t('messages.reportError'));
throw new Error(errorMessage);
},

Expand Down Expand Up @@ -144,10 +144,23 @@ export default Ember.Controller.extend(DateFormat, ModalHelper, NumberFormat, Pa

let reportDesc = reportTypes.findBy('value', reportType);
if (Ember.isEmpty(startDate)) {
this.set('reportTitle', `${reportDesc.name} Report ${formattedEndDate}`);
this.set('reportTitle', this.get('i18n').t(
'inventory.reports.titleSingleDate',
{
name: reportDesc.name,
date: formattedEndDate
}
));
} else {
formattedStartDate = moment(startDate).format('l');
this.set('reportTitle', `${reportDesc.name} Report ${formattedStartDate} - ${formattedEndDate}`);
this.set('reportTitle', this.get('i18n').t(
'inventory.reports.titleDateRange',
{
name: reportDesc.name,
startDate: formattedStartDate,
endDate: formattedEndDate
}
));
}
},

Expand Down
10 changes: 7 additions & 3 deletions app/locales/en/translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,14 @@ export default {
sorry: 'Sorry, something went wrong...',
forAuthorizedPersons: 'This report is for authorized persons only.',
requiredFieldsCorrectErrors: 'Please fill in required fields (marked with *) and correct the errors before saving.',
saveActionException: 'An error occurred while attempting to save: {{message}}'
saveActionException: 'An error occurred while attempting to save: {{message}}',
reportError: 'An error was encountered while generating the requested report. Please let your system administrator know that you have encountered an error.'
},
alerts: {
pleaseWait: 'Please Wait',
warningExclamation: 'Warning!!!!',
errorExclamation: 'Error!!!!'
errorExclamation: 'Error!!!!',
reportError: 'Error Generating Report'
},
headings: {
chargedItems: 'Charged Items'
Expand Down Expand Up @@ -602,7 +604,9 @@ export default {
stockTransferDetail: 'Detailed Stock Transfer',
stockTransferSum: 'Summary Stock Transfer',
stockUsageDetail: 'Detailed Stock Usage',
stockUsageSum: 'Summary Stock Usage'
stockUsageSum: 'Summary Stock Usage',
titleSingleDate: '{{name}} Report {{date}}',
titleDateRange: '{{name}} Report {{startDate}} - {{endDate}}'
},
titles: {
addPurchase: 'Add Purchase',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"ember-resolver": "^2.0.3",
"ember-select-list": "0.9.5",
"ember-simple-auth": "^1.1.0",
"ember-sinon-qunit": "^1.4.0",
"ember-truth-helpers": "1.2.0",
"ember-validations": "2.0.0-alpha.5",
"eslint-plugin-ember-suave": "^1.0.0",
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/controllers/abstract-delete-controller-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { moduleFor } from 'ember-qunit';
import test from 'ember-sinon-qunit/test-support/test';
import Ember from 'ember';
import DS from 'ember-data';

moduleFor('controller:abstract-delete-controller', 'Unit | Controller | abstract-delete-controller', {
unit: true,
testModel(attrs) {
return Ember.run(() => {
this.register('model:test', DS.Model);
return this.store().createRecord('test', attrs);
});
},
store() {
return this.container.lookup('service:store');
},
sendStub(controller) {
let once = false;
let originalSend = controller.send.bind(controller);
return (arg) => {
if (once) {
return;
}

once = true;
originalSend(arg);
};
}
});

test('actions.cancel', function(assert) {
let controller = this.subject();
let send = this.stub(controller, 'send', this.sendStub(controller));

controller.send('cancel');

assert.equal(send.getCall(0).args[0], 'cancel');
assert.equal(send.getCall(1).args[0], 'closeModal', 'Should close modal');
});

test('actions.delete', function(assert) {
let controller = this.subject({
model: this.testModel({
save: () => {},
unloadRecord: () => {}
})
});
let send = this.stub(controller, 'send', this.sendStub(controller));
let save = this.stub(controller.get('model'), 'save', () => {
return new Ember.RSVP.Promise((resolve) => resolve());
});
let unloadRecord = this.stub(controller.get('model'), 'unloadRecord');

Ember.run(() => controller.send('delete'));

assert.equal(send.getCall(0).args[0], 'delete');
assert.ok(save.calledOnce, 'Should save model');
assert.strictEqual(controller.get('model.archived'), true, 'Should archive model');
assert.ok(unloadRecord.calledOnce, 'Should unload record of model');
assert.equal(send.getCall(1).args[0], 'closeModal', 'Should close modal');
});
154 changes: 154 additions & 0 deletions tests/unit/controllers/abstract-paged-controller-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { moduleFor, test } from 'ember-qunit';
import sinonTest from 'ember-sinon-qunit/test-support/test';
import Ember from 'ember';
import DS from 'ember-data';

moduleFor('controller:abstract-paged-controller', 'Unit | Controller | abstract-paged-controller', {
unit: true,
testModel(attrs) {
return Ember.run(() => {
this.register('model:test', DS.Model);
return this.store().createRecord('test', attrs);
});
},
store() {
return this.container.lookup('service:store');
}
});

test('showActions', function(assert) {
let controller = this.subject({
canAdd: false,
canEdit: true,
canDelete: false
});

assert.strictEqual(controller.get('showActions'), true);
});

test('disablePreviousPage', function(assert) {
let controller = this.subject();

assert.strictEqual(controller.get('disablePreviousPage'), true);
});

test('disablePreviousPage false', function(assert) {
let controller = this.subject({
previousStartKey: 'test'
});

assert.strictEqual(controller.get('disablePreviousPage'), false);
});

test('disableNextPage', function(assert) {
let controller = this.subject();

assert.strictEqual(controller.get('disableNextPage'), true);
});

test('disableNextPage false', function(assert) {
let controller = this.subject({
nextStartKey: 'test'
});

assert.strictEqual(controller.get('disableNextPage'), false);
});

test('showPagination', function(assert) {
let controller = this.subject({
previousStartKey: 'test'
});

assert.strictEqual(controller.get('showPagination'), true);
});

test('showPagination false', function(assert) {
let controller = this.subject();

assert.strictEqual(controller.get('showPagination'), false);
});

test('hasRecords', function(assert) {
let controller = this.subject({
model: this.testModel({
length: 1
})
});

assert.strictEqual(controller.get('hasRecords'), true);
});

test('hasRecords false', function(assert) {
let controller = this.subject({
model: this.testModel({
length: 0
})
});

assert.strictEqual(controller.get('hasRecords'), false);
});

test('hasRecords false empty', function(assert) {
assert.strictEqual(this.subject().get('hasRecords'), false);
});

sinonTest('actions.nextPage', function(assert) {
let controller = this.subject({
nextStartKey: 'next',
previousStartKeys: ['prev1', 'prev2'],
firstKey: 'first'
});
let showProgressModal = this.stub(controller, 'showProgressModal');

controller.send('nextPage');

assert.strictEqual(controller.get('previousStartKey'), 'first', 'Should set previousStartKey');
assert.deepEqual(controller.get('previousStartKeys'), ['prev1', 'prev2', 'first'], 'Should set previousStartKeys');
assert.strictEqual(controller.get('startKey'), 'next', 'Should set startKey');
assert.ok(showProgressModal.calledOnce, 'Should show progress modal');
});

sinonTest('actions.previousPage', function(assert) {
let controller = this.subject({
previousStartKey: 'prev',
previousStartKeys: ['prev1', 'prev2', 'prev3']
});
let showProgressModal = this.stub(controller, 'showProgressModal');

controller.send('previousPage');

assert.strictEqual(controller.get('startKey'), 'prev', 'Should set startKey');
assert.strictEqual(controller.get('previousStartKey'), 'prev2', 'Should set previousStartKey');
assert.deepEqual(controller.get('previousStartKeys'), ['prev1'], 'Should set previousStartKey');
assert.ok(showProgressModal.calledOnce, 'Should show progress modal');
});

/**
* @todo verify that not clearing `nextStartKey` and `firstKey`
* is the intended behavior
*/
sinonTest('actions.sortByKey', function(assert) {
let controller = this.subject({
nextStartKey: 'next',
previousStartKey: 'prev',
previousStartKeys: ['prev1', 'prev2'],
firstKey: 'first',
startKey: 'start'
});
let showProgressModal = this.stub(controller, 'showProgressModal');

controller.send('sortByKey', 'sort', 'desc');

// These two assertions preserve current implementation
assert.strictEqual(controller.get('nextStartKey'), 'next', 'Should not change nextStartKey');
assert.strictEqual(controller.get('firstKey'), 'first', 'Should not change firstKey');

assert.strictEqual(controller.get('startKey'), null, 'Should clear startKey');
assert.deepEqual(controller.get('previousStartKeys'), [], 'Should clear previousStartKeys');
assert.strictEqual(controller.get('previousStartKey'), null, 'Should clear previousStartKey');

assert.strictEqual(controller.get('sortDesc'), 'desc', 'Should set sortDesc');
assert.strictEqual(controller.get('sortKey'), 'sort', 'Should set sortKey');

assert.ok(showProgressModal.calledOnce, 'Should show progress modal');
});
Loading

0 comments on commit ca37bf7

Please sign in to comment.