Skip to content

Commit b394053

Browse files
authored
v0.4.2
* fix(dataReducer): `DOCUMENT_ADDED` action type added to dataReducer - #84 * fix(query): `oneListenerPerPath` supports passing falsey values (before defining it at all would enable) - slight update to #77 * fix(query): prevent errors when combining string/object query config updated * update(deps): [sinon](http://sinonjs.org/) and [sinon-chai](https://github.com/domenic/sinon-chai) dependencies updated (fixes child dep warning)
2 parents 163c07d + 27451d5 commit b394053

File tree

8 files changed

+104
-215
lines changed

8 files changed

+104
-215
lines changed

package-lock.json

+57-204
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-firestore",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "Redux bindings for Firestore.",
55
"main": "lib/index.js",
66
"module": "es/index.js",
@@ -68,8 +68,8 @@
6868
"prettier": "^1.10.2",
6969
"redux": "^3.7.2",
7070
"rimraf": "^2.6.2",
71-
"sinon": "^3.3.0",
72-
"sinon-chai": "^2.14.0",
71+
"sinon": "^4.5.0",
72+
"sinon-chai": "^3.0.0",
7373
"webpack": "^3.11.0"
7474
},
7575
"license": "MIT",

src/actions/firestore.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ export function setListeners(firebase, dispatch, listeners) {
282282
}
283283

284284
const { config } = firebase._;
285-
const oneListenerPerPath = !!config.oneListenerPerPath;
286285

287-
if (oneListenerPerPath) {
286+
// Only attach one listener (count of matching listener path calls is tracked)
287+
if (config.oneListenerPerPath) {
288288
return listeners.forEach(listener => {
289289
const path = getQueryName(listener);
290290
const oldListenerCount = pathListenerCounts[path] || 0;
@@ -300,8 +300,7 @@ export function setListeners(firebase, dispatch, listeners) {
300300
}
301301

302302
return listeners.forEach(listener => {
303-
// Config for supporting attaching of multiple listeners
304-
303+
// Config for supporting attaching of multiple listener callbacks
305304
const multipleListenersEnabled = isFunction(config.allowMultipleListeners)
306305
? config.allowMultipleListeners(listener, firebase._.listeners)
307306
: config.allowMultipleListeners;
@@ -343,9 +342,9 @@ export function unsetListeners(firebase, dispatch, listeners) {
343342
);
344343
}
345344
const { config } = firebase._;
346-
const oneListenerPerPath = !!config.oneListenerPerPath;
347345

348-
if (oneListenerPerPath) {
346+
// Keep one listener path even when detaching
347+
if (config.oneListenerPerPath) {
349348
listeners.forEach(listener => {
350349
const path = getQueryName(listener);
351350
pathListenerCounts[path] -= 1;

src/reducers/dataReducer.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
LISTENER_RESPONSE,
1010
LISTENER_ERROR,
1111
DELETE_SUCCESS,
12+
DOCUMENT_ADDED,
1213
DOCUMENT_MODIFIED,
1314
DOCUMENT_REMOVED,
1415
} = actionTypes;
@@ -56,6 +57,7 @@ export default function dataReducer(state = {}, action) {
5657
// Set data to state (with merge) immutabily (lodash/fp's setWith creates copy)
5758
return setWith(Object, pathFromMeta(meta), mergedData, state);
5859
case DOCUMENT_MODIFIED:
60+
case DOCUMENT_ADDED:
5961
return setWith(
6062
Object,
6163
pathFromMeta(action.meta),

src/reducers/orderedReducer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const {
1818

1919
/**
2020
* Case reducer for adding a document to a collection.
21-
* @param {Array} collectionState - Redux state of current collection
21+
* @param {Array} [collectionState=[]] - Redux state of current collection
2222
* @param {Object} action - The action that was dispatched
2323
* @return {Array} State with document modified
2424
*/
25-
function addDoc(array, action) {
25+
function addDoc(array = [], action) {
2626
return [
2727
...array.slice(0, action.payload.ordered.newIndex),
2828
{ id: action.meta.doc, ...action.payload.data },

src/utils/query.js

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ function whereToStr(where) {
157157
* @return {String} String representing query settings
158158
*/
159159
export function getQueryName(meta) {
160+
if (isString(meta)) {
161+
return meta;
162+
}
160163
const { collection, doc, subcollections, where } = meta;
161164
if (!collection) {
162165
throw new Error('Collection is required to build query name');

test/unit/reducers/dataReducer.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ describe('dataReducer', () => {
2626
});
2727

2828
describe('actionTypes', () => {
29+
describe('DOCUMENT_ADDED', () => {
30+
it('throws for no collection', () => {
31+
const someDoc = {};
32+
payload = { data: { abc: someDoc } };
33+
meta = { collection, doc };
34+
action = { meta, payload, type: actionTypes.DOCUMENT_ADDED };
35+
result = dataReducer({}, action);
36+
expect(result).to.have.nested.property(
37+
`${collection}.${doc}.abc`,
38+
someDoc,
39+
);
40+
});
41+
});
42+
2943
describe('LISTENER_RESPONSE', () => {
3044
it('returns state if payload is not defined', () => {
3145
action = { meta: 'test', type: actionTypes.LISTENER_RESPONSE };

test/unit/reducers/orderedReducer.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ describe('orderedReducer', () => {
1616
});
1717

1818
describe('actionTypes', () => {
19+
describe('DOCUMENT_ADDED', () => {
20+
it('throws for no collection', () => {
21+
const collection = 'test1';
22+
const doc = 'test2';
23+
const someDoc = { id: doc };
24+
const payload = {
25+
ordered: { newIndex: 0, oldIndex: -1 },
26+
data: someDoc,
27+
};
28+
const meta = { collection, doc };
29+
action = { meta, payload, type: actionTypes.DOCUMENT_ADDED };
30+
const result = orderedReducer({}, action);
31+
expect(result).to.have.nested.property(
32+
`${collection}.0.id`,
33+
someDoc.id,
34+
);
35+
});
36+
});
1937
describe('LISTENER_RESPONSE', () => {
2038
it('returns state if payload is not defined', () => {
2139
action = { meta: 'test', type: actionTypes.LISTENER_RESPONSE };

0 commit comments

Comments
 (0)