diff --git a/package.json b/package.json index 1f759f0..421bf75 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/collections/reducers.ts b/src/collections/reducers.ts index dc8d441..a10c495 100644 --- a/src/collections/reducers.ts +++ b/src/collections/reducers.ts @@ -40,9 +40,7 @@ function updateCollectionItemsFromResponse( ? oldCollectionItems.concat(newCollectionItems) : newCollectionItems; const newCollection = { - count: shouldAppend - ? newCollectionResults.length - : count || newCollectionResults.length, + count: count || newCollectionResults.length, filters, next, ordering, diff --git a/tests/collections.ts b/tests/collections.ts index 633d878..31def2d 100644 --- a/tests/collections.ts +++ b/tests/collections.ts @@ -47,12 +47,13 @@ describe('Collections', () => { subgroup: string, results: ReadonlyArray, shouldAppend: boolean, + count?: number, next?: string ) { return { meta: { tag, shouldAppend, subgroup }, payload: { - count: results.length, + count, page: 1, next, results, @@ -263,7 +264,8 @@ describe('Collections', () => { name: 'Drama', }, ], - false + false, + 2 ) ); const data2 = collections.reducers.collectionsReducer( @@ -278,7 +280,8 @@ describe('Collections', () => { name: 'Pajama', }, ], - true + true, + 2 ) ); const subCollection = getCollectionByName(data2, 'llamas'); @@ -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, @@ -617,12 +645,13 @@ describe('Collections, immutably-backed', () => { subgroup: string, results: ReadonlyArray, shouldAppend: boolean, + count?: number, next?: string ) { return { meta: { tag, shouldAppend, subgroup }, payload: { - count: results.length, + count, page: 1, next, results, @@ -708,7 +737,8 @@ describe('Collections, immutably-backed', () => { name: 'Drama', }, ], - false + false, + 2 ) ); const data2 = collections.reducers.collectionsReducer( @@ -723,7 +753,8 @@ describe('Collections, immutably-backed', () => { name: 'Pajama', }, ], - true + true, + 2 ) ); const subCollection = getCollectionByName(data2, 'llamas');