Skip to content

Commit 1782a3b

Browse files
author
Bobby
authored
Merge pull request #13 from TARAAI/u/bs/limit-fix
fix: corrected query limit
2 parents e3fc24f + 5bcfa32 commit 1782a3b

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/reducers/cacheReducer.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ const getCollectionTransducer = (collection) =>
138138
* @returns {xFormPartialFields} - transducer
139139
*/
140140
const fieldsTransducer = (fields) =>
141-
partialRight(map, (docs) => docs.map((doc) => pick(doc, fields)));
141+
partialRight(map, (docs) =>
142+
docs.map((doc) => pick(doc, ['id', 'path', ...fields])),
143+
);
142144

143145
/**
144146
* @name orderTransducer
@@ -169,7 +171,7 @@ const orderTransducer = (order) => {
169171
* limit from the firestore query
170172
* @returns {xFormLimiter} - transducer
171173
*/
172-
const limitTransducer = (limit) => partialRight(take, limit);
174+
const limitTransducer = (limit) => ([arr] = []) => [take(arr, limit)];
173175

174176
/**
175177
* @name filterTransducers
@@ -313,10 +315,12 @@ function buildTransducer(overrides, query) {
313315
const xfFilter =
314316
!useOverrides || filterTransducers(!where ? ['', '*', ''] : where);
315317
const xfOrder = !useOverrides || !order ? null : orderTransducer(order);
316-
const xfLimit = !useOverrides || limit ? null : limitTransducer(limit);
318+
const xfLimit = !limit ? null : limitTransducer(limit);
317319

318320
if (!useOverrides) {
319-
return flow(compact([xfPopulate, xfGetCollection, xfGetDoc, xfFields]));
321+
return flow(
322+
compact([xfPopulate, xfGetCollection, xfGetDoc, xfLimit, xfFields]),
323+
);
320324
}
321325

322326
return flow(

test/unit/reducers/cacheReducer.spec.js

+36-3
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,64 @@ describe('cacheReducer', () => {
7070
expect(pass2.cache.testStoreAs2.docs[0]).to.eql({
7171
other: 'test',
7272
id: 'testDocId1',
73+
path,
7374
});
7475
});
7576
});
7677

7778
describe('query fields', () => {
7879
it('query fields return partial document', () => {
7980
const doc1 = { key1: 'value1', other: 'test', id: 'testDocId1', path };
81+
const doc2 = { key1: 'value1', other: 'limit', id: 'testDocId2', path };
82+
const doc3 = { key1: 'value1', other: 'third', id: 'testDocId3', path };
83+
const doc4 = { key1: 'value1', other: 'fourth', id: 'testDocId4', path };
8084

8185
// Initial seed
8286
const action1 = {
8387
meta: {
8488
collection,
8589
storeAs: 'testStoreAs',
8690
where: [['key1', '==', 'value1']],
87-
orderBy: ['value1'],
91+
orderBy: ['key1'],
8892
fields: ['id', 'other'],
93+
limit: 2,
8994
},
9095
payload: {
91-
data: { [doc1.id]: doc1 },
92-
ordered: [doc1],
96+
data: { [doc1.id]: doc1, [doc2.id]: doc2, [doc3.id]: doc3 },
97+
ordered: [doc1, doc2, doc3],
9398
fromCache: true,
9499
},
95100
type: actionTypes.LISTENER_RESPONSE,
96101
};
102+
const action2 = {
103+
type: actionTypes.OPTIMISTIC_ADDED,
104+
meta: {
105+
collection,
106+
doc: doc4.id,
107+
},
108+
payload: { data: doc4 },
109+
};
97110

98111
const pass1 = reducer(initialState, action1);
112+
const pass2 = reducer(pass1, action2);
99113

100114
expect(pass1.cache.testStoreAs.docs[0]).to.eql({
101115
other: 'test',
102116
id: 'testDocId1',
117+
path,
118+
});
119+
expect(pass1.cache.testStoreAs.docs[1]).to.eql({
120+
other: 'limit',
121+
id: 'testDocId2',
122+
path,
103123
});
124+
expect(pass2.cache.testStoreAs.docs[1]).to.eql({
125+
other: 'limit',
126+
id: 'testDocId2',
127+
path,
128+
});
129+
130+
expect(pass2.cache.testStoreAs.docs[2]).to.eql(undefined);
104131
});
105132

106133
it('empty fields return entire document', () => {
@@ -175,11 +202,13 @@ describe('cacheReducer', () => {
175202
expect(pass1.cache.testStoreAs.docs[0]).to.eql({
176203
id: 'testDocId1',
177204
key1: 'value1',
205+
path,
178206
});
179207
expect(pass2.cache.testStoreAs.docs[0]).to.eql({
180208
anotherDocument: doc2,
181209
key1: 'value1',
182210
id: 'testDocId1',
211+
path,
183212
});
184213
});
185214

@@ -231,12 +260,14 @@ describe('cacheReducer', () => {
231260
expect(pass1.cache.testStoreAs.docs[0]).to.eql({
232261
id: 'testDocId1',
233262
key1: 'value1',
263+
path,
234264
});
235265

236266
expect(pass2.cache.testStoreAs.docs[0]).to.eql({
237267
others: [doc3, doc2],
238268
key1: 'value1',
239269
id: 'testDocId1',
270+
path,
240271
});
241272
});
242273

@@ -288,12 +319,14 @@ describe('cacheReducer', () => {
288319
expect(pass1.cache.testStoreAs.docs[0]).to.eql({
289320
id: 'testDocId1',
290321
key1: 'value1',
322+
path,
291323
});
292324

293325
expect(pass2.cache.testStoreAs.docs[0]).to.eql({
294326
others: [doc3, doc2],
295327
key1: 'value1',
296328
id: 'testDocId1',
329+
path,
297330
});
298331
});
299332
});

0 commit comments

Comments
 (0)