From 11695f84ac3618e6bc647f11f144a81c24f416f1 Mon Sep 17 00:00:00 2001 From: Charlie Tomsett Date: Mon, 5 Mar 2018 10:16:24 +0000 Subject: [PATCH] Default the page size to the one supplied in the get collection action meta if the server does not supply a page number in its response --- package.json | 2 +- src/collections/reducers.ts | 4 ++-- tests/collections.ts | 28 ++++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 421bf75..2bd20d6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dabapps/redux-api-collections", - "version": "0.2.2", + "version": "0.2.3", "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 a10c495..f69c912 100644 --- a/src/collections/reducers.ts +++ b/src/collections/reducers.ts @@ -30,7 +30,7 @@ function updateCollectionItemsFromResponse( ordering, reverseOrdering, } = action.meta; - const { count, next, results, page } = action.payload; + const { count, next, results } = action.payload; const oldCollectionItems = (collectionData[subgroup || ''] || { results: [] }) .results; @@ -44,7 +44,7 @@ function updateCollectionItemsFromResponse( filters, next, ordering, - page: page || 1, + page: action.meta.page || action.payload.page || 1, results: newCollectionResults, immutableResults: useImmutable ? List(newCollectionResults) : null, reverseOrdering, diff --git a/tests/collections.ts b/tests/collections.ts index 31def2d..91f3c79 100644 --- a/tests/collections.ts +++ b/tests/collections.ts @@ -48,13 +48,14 @@ describe('Collections', () => { results: ReadonlyArray, shouldAppend: boolean, count?: number, + metaPage?: number, next?: string ) { return { - meta: { tag, shouldAppend, subgroup }, + meta: { tag, shouldAppend, subgroup, page: metaPage }, payload: { count, - page: 1, + page: metaPage ? undefined : 1, next, results, }, @@ -319,6 +320,29 @@ describe('Collections', () => { expect(results[0].furLength).toBe(5); }); + it('should update the page from the GET_COLLECTION request meta if the server does not return the page number', () => { + const data = collections.reducers.collectionsReducer( + undefined, + getCollectionSuccess( + 'llamas', + '', + [ + { + furLength: 5, + id: '1', + name: 'Drama', + }, + ], + false, + 12, + 6 + ) + ); + const subCollection = getCollectionByName(data, 'llamas'); + expect(subCollection.page).toBe(6); + expect(subCollection.count).toBe(12); + }); + it('should add an item on ADD_TO_COLLECTION responses', () => { const data = collections.reducers.collectionsReducer( undefined,