Skip to content

Commit

Permalink
Refactored getInvoiceData helper
Browse files Browse the repository at this point in the history
+ Make sure to retrieve the _rev data when updating the doc
+ Updated corresponding test
  • Loading branch information
hql287 committed Mar 2, 2018
1 parent 4fbd754 commit 9eb0f14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
31 changes: 31 additions & 0 deletions app/helpers/__tests__/form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import faker from 'faker';
import uuidv4 from 'uuid/v4';
import i18n from '../../../i18n/i18n';
import omit from 'lodash';

// Helpers to test
import {
Expand Down Expand Up @@ -267,6 +268,36 @@ describe('getInvoiceData', () => {
const invoiceData = getInvoiceData(newFormData);
expect(invoiceData.invoiceID).toEqual('Invoice: 123-456-789');
});

it('should return correct metadata on editMode', () => {
const invoiceID = uuidv4();
const invoiceRev = uuidv4();
const createdDate = Date.now();
const newFormData = Object.assign({}, formData, {
settings: Object.assign({}, formData.settings, {
editMode: Object.assign({}, formData.settings.editMode, {
active: true,
data: Object.assign({}, omit(formData, ['settings, savedSettings']),
{
_id: invoiceID,
_rev: invoiceRev,
created_at: createdDate
}
)
}),
}),
});
const invoiceData = getInvoiceData(newFormData);
expect(invoiceData._id).toEqual(invoiceID);
expect(invoiceData._rev).toEqual(invoiceRev);
expect(invoiceData.created_at).toEqual(createdDate);
});

// TODO
it('set status as pending when creating a new invoice');
it('always generate _id when creating a new invoice');
it('does not include _rev when creating a new invoice');
it('always recalculate subTotal and grandTotal');
});

describe('validateFormData', () => {
Expand Down
5 changes: 3 additions & 2 deletions app/helpers/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ function getInvoiceData(formData) {

// Return final value
return Object.assign({}, invoiceData, {
// Reuse existing data
// Metadata
_id: editMode.active ? editMode.data._id : uuidv4(),
_rev: editMode.active ? editMode.data._rev : null,
created_at: editMode.active ? editMode.data.created_at : Date.now(),
status: editMode.active ? editMode.data.status: 'pending',
// Calculate subtotal & grandTotal
// Alway calculate subtotal & grandTotal
subtotal: getInvoiceValue(invoiceData).subtotal,
grandTotal: getInvoiceValue(invoiceData).grandTotal,
});
Expand Down

0 comments on commit 9eb0f14

Please sign in to comment.