Skip to content

Commit

Permalink
Merge pull request #37 from dabapps/fix-appending-collections-result-…
Browse files Browse the repository at this point in the history
…count

Fix collection count being overwritten by results length when appending results
  • Loading branch information
lCharlie123l authored Mar 5, 2018
2 parents 0a75405 + 2077e07 commit 9110b60
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dabapps/redux-api-collections",
"version": "0.2.1",
"version": "0.2.2",
"description": "Type-safe helpers for dealing with Rest-Framework backed collections in Typescript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 1 addition & 3 deletions src/collections/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ function updateCollectionItemsFromResponse<T extends IdKeyed>(
? oldCollectionItems.concat(newCollectionItems)
: newCollectionItems;
const newCollection = {
count: shouldAppend
? newCollectionResults.length
: count || newCollectionResults.length,
count: count || newCollectionResults.length,
filters,
next,
ordering,
Expand Down
43 changes: 37 additions & 6 deletions tests/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ describe('Collections', () => {
subgroup: string,
results: ReadonlyArray<any>,
shouldAppend: boolean,
count?: number,
next?: string
) {
return {
meta: { tag, shouldAppend, subgroup },
payload: {
count: results.length,
count,
page: 1,
next,
results,
Expand Down Expand Up @@ -263,7 +264,8 @@ describe('Collections', () => {
name: 'Drama',
},
],
false
false,
2
)
);
const data2 = collections.reducers.collectionsReducer(
Expand All @@ -278,7 +280,8 @@ describe('Collections', () => {
name: 'Pajama',
},
],
true
true,
2
)
);
const subCollection = getCollectionByName(data2, 'llamas');
Expand All @@ -291,6 +294,31 @@ describe('Collections', () => {
expect(results[1].furLength).toBe(10);
});

it('should default to the results length if no count param is returned from the server on GET_COLLECTION responses', () => {
const data = collections.reducers.collectionsReducer(
undefined,
getCollectionSuccess(
'llamas',
'',
[
{
furLength: 5,
id: '1',
name: 'Drama',
},
],
false
)
);
const subCollection = getCollectionByName(data, 'llamas');
expect(subCollection.page).toBe(1);
expect(subCollection.count).toBe(1);
const results = getCollectionResultsByName(data, 'llamas');
expect(results).toBe(subCollection.results);
expect(results.length).toBe(subCollection.count);
expect(results[0].furLength).toBe(5);
});

it('should add an item on ADD_TO_COLLECTION responses', () => {
const data = collections.reducers.collectionsReducer(
undefined,
Expand Down Expand Up @@ -617,12 +645,13 @@ describe('Collections, immutably-backed', () => {
subgroup: string,
results: ReadonlyArray<any>,
shouldAppend: boolean,
count?: number,
next?: string
) {
return {
meta: { tag, shouldAppend, subgroup },
payload: {
count: results.length,
count,
page: 1,
next,
results,
Expand Down Expand Up @@ -708,7 +737,8 @@ describe('Collections, immutably-backed', () => {
name: 'Drama',
},
],
false
false,
2
)
);
const data2 = collections.reducers.collectionsReducer(
Expand All @@ -723,7 +753,8 @@ describe('Collections, immutably-backed', () => {
name: 'Pajama',
},
],
true
true,
2
)
);
const subCollection = getCollectionByName(data2, 'llamas');
Expand Down

0 comments on commit 9110b60

Please sign in to comment.