Skip to content

Commit

Permalink
v0.7.4
Browse files Browse the repository at this point in the history
* fix(query/reducer): prevent updates to ordered when non-existent doc is queried - @bcgilliom
* fix(enhancer): use Dispatch from redux to prevent compatibility issues with react-redux-firebase - @jareqpl
  • Loading branch information
prescottprue authored and Scott Prue committed May 14, 2019
2 parents cd9eb5c + 5b2778b commit aa992c9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function firestoreReducer(state: object, action: object): any;
export function createFirestoreInstance(
firebaseInstance: typeof Firebase,
configs: Partial<Config>,
dispatch: () => object,
dispatch: Dispatch,
): object;

/**
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-firestore",
"version": "0.7.3",
"version": "0.7.4",
"description": "Redux bindings for Firestore.",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
4 changes: 4 additions & 0 deletions src/reducers/orderedReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ function writeCollection(collectionState, action) {
}

if (meta.doc && collectionStateSize) {
// don't update ordered if the doc doesn't exist
if (!size(action.payload.ordered)) {
return collectionState;
}
// Update item in array
return updateItemInArray(collectionState, meta.doc, item =>
mergeObjects(item, action.payload.ordered[0]),
Expand Down
4 changes: 2 additions & 2 deletions src/utils/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ export function orderedFromSnap(snap) {
*/
export function dataByIdSnapshot(snap) {
const data = {};
if (snap.data && snap.exists) {
data[snap.id] = snap.data();
if (snap.data) {
data[snap.id] = snap.exists ? snap.data() : null;
} else if (snap.forEach) {
snap.forEach(doc => {
data[doc.id] = doc.data() || doc;
Expand Down
30 changes: 30 additions & 0 deletions test/unit/reducers/orderedReducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,36 @@ describe('orderedReducer', () => {
);
});

it('sets ordered to empty when doc does not exist', () => {
action = {
meta: { collection: 'testing', doc: 'doc' },
merge: {},
type: actionTypes.LISTENER_RESPONSE,
payload: { ordered: [] },
};
state = {};
const result = orderedReducer(state, action);

expect(result).to.have.property('testing');
// Value is an empty array
expect(result.testing).to.be.an('array');
expect(result.testing).to.be.empty;
});

it('does not modify existing state when doc does not exist', () => {
action = {
meta: { collection: 'testing', doc: 'doc' },
merge: {},
type: actionTypes.LISTENER_RESPONSE,
payload: { ordered: [] },
};
state = { testing: [{ id: 'testing2' }] };
const result = orderedReducer(state, action);
expect(result).to.have.property('testing');
// State not modified
expect(result.testing).to.equal(state.testing);
});

it('updates doc already within state', () => {
const id = 'doc';
const someField = 'a thing';
Expand Down
16 changes: 13 additions & 3 deletions test/unit/utils/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,10 +817,20 @@ describe('query utils', () => {
expect(result).to.have.property(id, fakeData);
});

it('returns null if no data returned', () => {
const id = 'someId';
result = dataByIdSnapshot({ id, data: () => ({}) });
it('returns null if no data returned for collection', () => {
const forEach = () => ({});
const empty = true;
result = dataByIdSnapshot({ forEach, empty });
expect(result).to.be.null;
});

it('returns object with null id if no data returned for a doc', () => {
const id = 'someId';
const data = () => ({});
const exists = false;
result = dataByIdSnapshot({ id, exists, data });
expect(result).to.be.an('object');
expect(result).to.have.property(id, null);
});
});
});

0 comments on commit aa992c9

Please sign in to comment.