Skip to content

Commit

Permalink
feat(tests): unit test added for DOCUMENT_ADDED with subcollection - #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Prue committed May 20, 2018
1 parent 84c0a6e commit 267adfb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/reducers/orderedReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ const {
* @return {Array} State with document modified
*/
function modifyDoc(collectionState, action) {
const { meta, payload } = action;
if (!meta.subcollections || meta.storeAs) {
if (!action.meta.subcollections || action.meta.storeAs) {
return updateItemInArray(collectionState, action.meta.doc, item =>
// Merge is used to prevent the removal of existing subcollections
mergeObjects(item, action.payload.data),
);
}
const [, docId, subcollectionName, subDocId] = pathToArr(meta.path);

// TODO: make this recurisve so it will work multiple subcollections deep
const [, docId, subcollectionName, subDocId] = pathToArr(action.meta.path);

// Update document item within top arra
return updateItemInArray(collectionState, docId, item => ({
Expand All @@ -43,7 +44,7 @@ function modifyDoc(collectionState, action) {
get(item, subcollectionName, []),
subDocId,
// Merge with existing subcollection doc (only updates changed keys)
subitem => mergeObjects(subitem, payload.data),
subitem => mergeObjects(subitem, action.payload.data),
),
}));
}
Expand Down
43 changes: 40 additions & 3 deletions test/unit/reducers/orderedReducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,54 @@ describe('orderedReducer', () => {
it('adds a document when collection is empty', () => {
const collection = 'test1';
const doc = 'test2';
const someDoc = { id: doc };
const someDoc = { some: 'value' };
const payload = {
ordered: { newIndex: 0, oldIndex: -1 },
data: someDoc,
};
const meta = { collection, doc };
action = { meta, payload, type: actionTypes.DOCUMENT_ADDED };
const result = orderedReducer({}, action);
// Id is set
expect(result).to.have.nested.property(`${collection}.0.id`, doc);
// Value is set
expect(result).to.have.nested.property(
`${collection}.0.id`,
someDoc.id,
`${collection}.0.some`,
someDoc.some,
);
});

it('adds a subcollection document when collection is empty', () => {
const collection = 'test1';
const doc = 'test2';
const subcollection = 'test3';
const subdoc = 'test4';
const fakeDoc = { some: 'value' };
const payload = {
ordered: { newIndex: 0, oldIndex: -1 },
data: fakeDoc,
};
const meta = {
collection,
doc,
subcollections: [{ collection: subcollection }],
path: `${collection}/${doc}/${subcollection}/${subdoc}`,
};
action = {
meta,
payload,
type: actionTypes.DOCUMENT_ADDED,
};
const result = orderedReducer({}, action);
// Id is set
expect(result).to.have.nested.property(
`${collection}.0.${subcollection}.0.id`,
subdoc,
);
// Value is set
expect(result).to.have.nested.property(
`${collection}.0.${subcollection}.0.some`,
fakeDoc.some,
);
});
});
Expand Down

0 comments on commit 267adfb

Please sign in to comment.