Skip to content

Commit

Permalink
Fixed Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hql287 committed Jan 18, 2018
1 parent 5f88d20 commit 3cd5be6
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 10 deletions.
15 changes: 15 additions & 0 deletions app/components/invoices/__tests__/Invoice.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const invoice = {
},
};

const editInvoice = jest.fn();
const deleteInvoice = jest.fn();
const setInvoiceStatus = jest.fn();
const dateFormat = 'MM/DD/YY';
Expand All @@ -43,6 +44,7 @@ describe('Renders correctly to the DOM', () => {
wrapper = shallow(
<Invoice
invoice={invoice}
editInvoice={editInvoice}
deleteInvoice={deleteInvoice}
setInvoiceStatus={setInvoiceStatus}
dateFormat={dateFormat}
Expand All @@ -54,6 +56,7 @@ describe('Renders correctly to the DOM', () => {
const mountWrapper = mount(
<Invoice
invoice={invoice}
editInvoice={editInvoice}
deleteInvoice={deleteInvoice}
setInvoiceStatus={setInvoiceStatus}
dateFormat={dateFormat}
Expand Down Expand Up @@ -90,6 +93,7 @@ describe('Renders correctly to the DOM', () => {
const paidInvoiceWapper = shallow(
<Invoice
invoice={paidInvoice}
editInvoice={editInvoice}
deleteInvoice={deleteInvoice}
setInvoiceStatus={setInvoiceStatus}
dateFormat={dateFormat}
Expand All @@ -110,6 +114,7 @@ describe('Renders correctly to the DOM', () => {
const cancelledInvoiceWapper = shallow(
<Invoice
invoice={cancelledInvoice}
editInvoice={editInvoice}
deleteInvoice={deleteInvoice}
setInvoiceStatus={setInvoiceStatus}
dateFormat={dateFormat}
Expand All @@ -128,6 +133,7 @@ describe('Renders correctly to the DOM', () => {
const refundedInvoiceWapper = shallow(
<Invoice
invoice={refundedInvoice}
editInvoice={editInvoice}
deleteInvoice={deleteInvoice}
setInvoiceStatus={setInvoiceStatus}
dateFormat={dateFormat}
Expand Down Expand Up @@ -156,6 +162,7 @@ describe('Renders correctly to the DOM', () => {
.create(
<Invoice
invoice={invoice}
editInvoice={editInvoice}
deleteInvoice={deleteInvoice}
setInvoiceStatus={setInvoiceStatus}
dateFormat={dateFormat}
Expand All @@ -174,6 +181,7 @@ describe('handle clicks correctly', () => {
wrapper = shallow(
<Invoice
invoice={invoice}
editInvoice={editInvoice}
deleteInvoice={deleteInvoice}
setInvoiceStatus={setInvoiceStatus}
dateFormat={dateFormat}
Expand All @@ -190,6 +198,13 @@ describe('handle clicks correctly', () => {
deleteBtn = wrapper.find(Button).first();
});

// TODO
it('handle edit action correctly', () => {
editBtn.simulate('click');
expect(editInvoice).toHaveBeenCalled();
expect(editInvoice).toHaveBeenCalledWith(invoice);
});

it('handle delete action correctly', () => {
deleteBtn.simulate('click');
expect(deleteInvoice).toHaveBeenCalled();
Expand Down
12 changes: 12 additions & 0 deletions app/helpers/__mocks__/pouchDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ const saveDoc = jest.fn(
})
);

const updateDoc = jest.fn(
(dbName, doc) =>
new Promise((resolve, reject) => {
!dbName && reject(new Error('No database found!'));
!doc && reject(new Error('No doc found!'));
dbName === 'contacts' && resolve([...mockData.contactsRecords]);
dbName === 'invoices' && resolve([...mockData.invoicesRecords]);
})
);

const deleteDoc = jest.fn(
(dbName, docId) =>
new Promise((resolve, reject) => {
Expand All @@ -70,9 +80,11 @@ const deleteDoc = jest.fn(
})
);


module.exports = {
getAllDocs,
saveDoc,
updateDoc,
deleteDoc,
mockData,
};
2 changes: 1 addition & 1 deletion app/middlewares/InvoicesMW.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const InvoicesMW = ({ dispatch }) => next => action => {
type: ACTION_TYPES.UI_NOTIFICATION_NEW,
payload: {
type: 'success',
message: 'Updated Successfully',
message: 'Invoice Updated Successfully',
},
});
})
Expand Down
64 changes: 64 additions & 0 deletions app/middlewares/__tests__/FormMW.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ describe('Form Middleware', () => {
form: {
validation: true,
recipient: { newRecipient: true },
settings: {
editMode: {
active: false,
}
}
},
}));
const middleware = FormMW({ dispatch, getState })(next);
Expand All @@ -40,6 +45,11 @@ describe('Form Middleware', () => {
form: {
validation: true,
recipient: { newRecipient: false },
settings: {
editMode: {
active: false,
}
}
},
}));
const middleware = FormMW({ dispatch, getState })(next);
Expand All @@ -55,6 +65,60 @@ describe('Form Middleware', () => {
// No Calling Next
expect(next.mock.calls.length).toBe(0);
});

it('should update Invocie, save Contact, Clear the Form and Switch Tab', () => {
// Setup
getState = jest.fn(() => ({
form: {
validation: true,
recipient: { newRecipient: true },
settings: {
editMode: {
active: true,
data: { _id: 'invoice-uuid' }
}
}
},
}));
const middleware = FormMW({ dispatch, getState })(next);
const action = Actions.saveFormData();
// Action
middleware(action);
// Expect
expect(getState.mock.calls.length).toBe(1);
// Update the Invoice, Save new contact, Clear the Form & Change Tab
expect(dispatch.mock.calls.length).toBe(4);
// No Calling Next
expect(next.mock.calls.length).toBe(0);
});

it('should update Invocie, NOT add new Contact, Clear the Form and Switch Tab', () => {
// Setup
getState = jest.fn(() => ({
form: {
validation: true,
recipient: { newRecipient: false },
settings: {
editMode: {
active: true,
data: { _id: 'invoice-uuid' }
}
}
},
}));
const middleware = FormMW({ dispatch, getState })(next);

// Action
const action = Actions.saveFormData();
middleware(action);

// Expect
expect(getState.mock.calls.length).toBe(1);
// Update the Invoice, Clear the Form and Switch Tab
expect(dispatch.mock.calls.length).toBe(3);
// No Calling Next
expect(next.mock.calls.length).toBe(0);
});
});

it('should NOT pass validation', () => {
Expand Down
117 changes: 117 additions & 0 deletions app/middlewares/__tests__/InvoicesMW.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import uuidv4 from 'uuid/v4';
// Mock Functions
const {
getAllDocs,
updateDoc,
saveDoc,
deleteDoc,
mockData,
Expand Down Expand Up @@ -278,6 +279,122 @@ describe('Invoices Middleware', () => {
});
});

describe('should handle INVOICE_EDIT action', () => {
it('should call next and dispatch change Tab action', () => {
// Setup
const currentInvoice = {
recipient: {
fullname: faker.name.findName(),
email: faker.internet.email(),
},
currency: {
code: 'USD',
symbol: '$',
},
rows: [
{
id: uuidv4(),
description: faker.commerce.productName(),
price: faker.commerce.price(),
quantity: faker.random.number(10),
},
],
};
// Execute
const action = Actions.editInvoice(currentInvoice);
middleware(action);
// Call next
expect(next.mock.calls.length).toBe(1);
expect(next).toHaveBeenCalledWith(action);
// Dispatch change Tab action
expect(dispatch.mock.calls.length).toBe(1);
expect(dispatch).toHaveBeenCalledWith({
type: ACTION_TYPES.UI_TAB_CHANGE,
payload: 'form'
});
});
})

// TODO
describe('should handle INVOICE_UDPATE action', () => {
let currentInvoice;
beforeEach(() => {
currentInvoice = {
recipient: {
fullname: faker.name.findName(),
email: faker.internet.email(),
},
currency: {
code: 'USD',
symbol: '$',
},
rows: [
{
id: uuidv4(),
description: faker.commerce.productName(),
price: faker.commerce.price(),
quantity: faker.random.number(10),
},
],
};
});

it('should update the invoice', () => {
middleware(Actions.updateInvoice(currentInvoice)).then(() =>
updateDoc('invoices', currentInvoice).then(data => {
expect(data).toEqual(mockData.invoicesRecords);
})
);
});

it('should call next and dispatch notification', () => {
middleware(Actions.updateInvoice(currentInvoice)).then(() =>
updateDoc('invoices', currentInvoice).then(data => {
// Call next after the promised is returned
expect(next.mock.calls.length).toBe(1);
expect(next).toHaveBeenCalledWith({
type: ACTION_TYPES.INVOICE_UPDATE,
payload: mockData.invoicesRecords,
});
// Dispatch success notification
expect(dispatch.mock.calls.length).toBe(1);
expect(dispatch).toHaveBeenCalledWith({
type: ACTION_TYPES.UI_NOTIFICATION_NEW,
payload: {
type: 'success',
message: 'Invoice Updated Successfully',
},
});
})
);
});

it('should handle syntax error correctly', () => {
middleware(Actions.updateInvoice(currentInvoice)).then(() => {
const dbError = new Error('No database found!');
const docError = new Error('No doc found!');
expect(updateDoc()).rejects.toEqual(dbError);
expect(updateDoc('invoices')).rejects.toEqual(docError);
});
});

it('should handle unkown error correctly', () => {
const expectedError = new Error('Something Broken!');
updateDoc.mockImplementationOnce(() => Promise.reject(expectedError));
middleware(Actions.updateInvoice(currentInvoice)).then(() => {
expect(next).toHaveBeenCalled();
expect(next).toHaveBeenCalledWith({
type: ACTION_TYPES.UI_NOTIFICATION_NEW,
payload: {
type: 'warning',
message: expectedError.message,
},
});
});
});
})


describe('should handle INVOICE_DELETE action', () => {
it('should remove record from DB correctly', () => {
const invoiceID = 'jon-invoice';
Expand Down
24 changes: 15 additions & 9 deletions app/reducers/FormReducer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const initialState = {
settings: {
open: false,
editMode: {
active: false
active: false,
},
required_fields: invoiceSettings.required_fields,
},
Expand Down Expand Up @@ -123,14 +123,20 @@ const FormReducer = handleActions(
}),
rows,
currency,
dueDate: dueDate !== undefined ? Object.assign({}, state.dueDate, {
selectedDate: dueDate
}) : state.dueDate,
dueDate:
dueDate !== undefined
? Object.assign({}, state.dueDate, {
selectedDate: dueDate,
})
: state.dueDate,
discount: discount !== undefined ? discount : state.discount,
tax: tax !== undefined ? tax : state.tax,
note: note !== undefined ? Object.assign({}, state.note, {
content: note
}) : state.note,
note:
note !== undefined
? Object.assign({}, state.note, {
content: note,
})
: state.note,
// Update settings
settings: Object.assign({}, state.settings, {
editMode: {
Expand All @@ -143,7 +149,7 @@ const FormReducer = handleActions(
dueDate: dueDate !== undefined,
discount: discount !== undefined,
note: note !== undefined,
})
}),
}),
});
},
Expand All @@ -169,7 +175,7 @@ const FormReducer = handleActions(
settings: Object.assign({}, state.settings, {
open: false,
editMode: {
active: false
active: false,
},
required_fields: state.savedSettings.required_fields,
}),
Expand Down
Loading

0 comments on commit 3cd5be6

Please sign in to comment.