Commit 44fa842f authored by Bryce Johnson's avatar Bryce Johnson

Improve logic for stringified approvers, get tests passing.

parent 917496be
...@@ -13,12 +13,24 @@ ...@@ -13,12 +13,24 @@
approverNamesStringified() { approverNamesStringified() {
const approvers = this.suggestedApprovers; const approvers = this.suggestedApprovers;
if (approvers && approvers.length) { if (approvers && approvers.length) {
const lastIdx = approvers.length - 1; if (approvers.length === 1) {
return approvers[0].name;
}
const lastIndex = approvers.length - 1;
const nextToLastIndex = approvers.length - 2;
return approvers.reduce((memo, curr, index) => { return approvers.reduce((memo, curr, index) => {
const userDisplayName = curr.name; let suffix;
const newList = index !== lastIdx ? `${memo} ${userDisplayName}, ` :
`${memo} or ${userDisplayName}`; if (index === nextToLastIndex) {
return newList; suffix = ' or ';
} else if (index === lastIndex) {
suffix = '';
} else {
suffix = ', ';
}
const nameToAdd = `${curr.name}${suffix}`;
return `${memo}${nameToAdd}`;
}, ''); }, '');
} }
return null; return null;
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
`); `);
this.initialData = { this.initialData = {
suggestedApprovers: ['Approver 1'], suggestedApprovers: [ { name: 'Approver 1' }],
userCanApprove: false, userCanApprove: false,
userHasApproved: true, userHasApproved: true,
approvedBy: [], approvedBy: [],
...@@ -65,7 +65,7 @@ ...@@ -65,7 +65,7 @@
it('should display the correct string for 2 possible approver', function (done) { it('should display the correct string for 2 possible approver', function (done) {
this.approvalsBody.approvalsLeft = 2; this.approvalsBody.approvalsLeft = 2;
this.approvalsBody.suggestedApprovers.push('Approver 2'); this.approvalsBody.suggestedApprovers.push({ name: 'Approver 2' });
Vue.nextTick(() => { Vue.nextTick(() => {
const correctText = '2 more approvals'; const correctText = '2 more approvals';
...@@ -77,14 +77,17 @@ ...@@ -77,14 +77,17 @@
describe('approverNamesStringified', function () { describe('approverNamesStringified', function () {
// Preceded by: Requires {one more approval} required from _____ // Preceded by: Requires {one more approval} required from _____
it('should display the correct string for 1 possible approver name', function () { it('should display the correct string for 1 possible approver name', function (done) {
const correctText = 'Approver 1'; const correctText = 'Approver 1';
expect(this.approvalsBody.approverNamesStringified).toBe(correctText); Vue.nextTick(() => {
expect(this.approvalsBody.approverNamesStringified).toBe(correctText);
done();
});
}); });
it('should display the correct string for 2 possible approver names', function (done) { it('should display the correct string for 2 possible approver names', function (done) {
this.approvalsBody.approvalsLeft = 2; this.approvalsBody.approvalsLeft = 2;
this.approvalsBody.suggestedApprovers.push('Approver 2'); this.approvalsBody.suggestedApprovers.push({ name: 'Approver 2' });
Vue.nextTick(() => { Vue.nextTick(() => {
const correctText = 'Approver 1 or Approver 2'; const correctText = 'Approver 1 or Approver 2';
...@@ -95,8 +98,8 @@ ...@@ -95,8 +98,8 @@
it('should display the correct string for 3 possible approver names', function (done) { it('should display the correct string for 3 possible approver names', function (done) {
this.approvalsBody.approvalsLeft = 3; this.approvalsBody.approvalsLeft = 3;
this.approvalsBody.suggestedApprovers.push('Approver 2'); this.approvalsBody.suggestedApprovers.push({ name: 'Approver 2' });
this.approvalsBody.suggestedApprovers.push('Approver 3'); this.approvalsBody.suggestedApprovers.push({ name: 'Approver 3' });
Vue.nextTick(() => { Vue.nextTick(() => {
const correctText = 'Approver 1, Approver 2 or Approver 3'; const correctText = 'Approver 1, Approver 2 or Approver 3';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment