From 4226f204d97fb6069500834e0ea5fd6c3692a268 Mon Sep 17 00:00:00 2001 From: Antoine Sein Date: Tue, 30 Apr 2024 12:17:19 +0200 Subject: [PATCH] Add tests --- .../client/api-client-pagination-helper.ts | 52 ++-- .../api-client-pagination-helpers.test.ts | 249 ++++++++++++++++++ 2 files changed, 282 insertions(+), 19 deletions(-) create mode 100644 packages/sdk-client/tests/client/api-client-pagination-helpers.test.ts diff --git a/packages/sdk-client/src/client/api-client-pagination-helper.ts b/packages/sdk-client/src/client/api-client-pagination-helper.ts index 55666eba..8a720d02 100644 --- a/packages/sdk-client/src/client/api-client-pagination-helper.ts +++ b/packages/sdk-client/src/client/api-client-pagination-helper.ts @@ -263,68 +263,82 @@ interface WithPagination_PAGE2 { const checkIfThereAreMorePages = ( response: WithPagination, - requestedPageSize: number | undefined, - paginationType: PaginationEnum, + requestedPageSize: number, + paginationType: PaginationEnum.PAGE | PaginationEnum.PAGE2, ): boolean => { const lastPageNumber = calculateLastPageValue(response, requestedPageSize, paginationType); - return response.page! < lastPageNumber; + switch (paginationType) { + case PaginationEnum.PAGE: + return response.page! < lastPageNumber; + case PaginationEnum.PAGE2: + return response.pageNumber! < lastPageNumber; + } }; const calculateLastPageValue = ( response: WithPagination, - requestedPageSize: number | undefined, - paginationType: PaginationEnum, + pageSize: number, + paginationType: PaginationEnum.PAGE | PaginationEnum.PAGE2, ): number => { if (invalidatePaginationDataFromResponse(response, paginationType)) { throw new Error(`Impossible to calculate the last page value with the following parameters: count=${response.count}, page=${response.page}, page_size=${response.page_size}`); } - if (response.page_size === 0) { + if (response.page_size === 0 || response.pageSize === 0) { // If there are no elements on the current page, there are no more pages return response.page!; } // The elements in the response are not enough to determine if the current page is the last one - const pageSize: number = requestedPageSize || response.page_size!; - const itemCount: number = response.count!; - return Math.ceil(itemCount / pageSize) - 1; + switch (paginationType) { + case PaginationEnum.PAGE: + return Math.ceil(response.count! / pageSize) - 1; + case PaginationEnum.PAGE2: + return Math.ceil(response.totalItems! / pageSize) - 1; + } }; -const invalidatePaginationDataFromResponse = (response: WithPagination, paginationType: PaginationEnum): boolean => { +const invalidatePaginationDataFromResponse = ( + response: WithPagination, + paginationType: PaginationEnum.PAGE | PaginationEnum.PAGE2, +): boolean => { const currentPage = getCurrentPage(response, paginationType); const pageSize = getPageSize(response, paginationType); const itemCount = getItemCount(response, paginationType); return !isNumber(currentPage) || !isNumber(pageSize) || !isNumber(itemCount); }; -const getCurrentPage = (response: WithPagination, paginationType: PaginationEnum): number | undefined => { +const getCurrentPage = ( + response: WithPagination, + paginationType: PaginationEnum.PAGE | PaginationEnum.PAGE2, +): number | undefined => { switch (paginationType) { case PaginationEnum.PAGE: return response.page; case PaginationEnum.PAGE2: return response.pageNumber; - default: - return undefined; } }; -const getPageSize = (response: WithPagination, paginationType: PaginationEnum): number | undefined => { +const getPageSize = ( + response: WithPagination, + paginationType: PaginationEnum.PAGE | PaginationEnum.PAGE2, +): number | undefined => { switch (paginationType) { case PaginationEnum.PAGE: return response.page_size; case PaginationEnum.PAGE2: return response.pageSize; - default: - return undefined; } }; -const getItemCount = (response: WithPagination, paginationType: PaginationEnum): number | undefined => { +const getItemCount = ( + response: WithPagination, + paginationType: PaginationEnum.PAGE | PaginationEnum.PAGE2, +): number | undefined => { switch (paginationType) { case PaginationEnum.PAGE: return response.count; case PaginationEnum.PAGE2: return response.totalItems; - default: - return undefined; } }; diff --git a/packages/sdk-client/tests/client/api-client-pagination-helpers.test.ts b/packages/sdk-client/tests/client/api-client-pagination-helpers.test.ts new file mode 100644 index 00000000..ec436e82 --- /dev/null +++ b/packages/sdk-client/tests/client/api-client-pagination-helpers.test.ts @@ -0,0 +1,249 @@ +import { + calculateNextPage, + hasMore, + PaginationContext, + PaginationEnum, +} from '../../src'; +import { Headers } from 'node-fetch'; + +describe('API Client Pagination Helper', () => { + + const paginationTokenProperties = { + apiName: '', + operationId: '', + dataKey: 'elements', + requestOptions: { + headers: new Headers(), + hostname: 'example.com', + }, + }; + const paginationContextToken: PaginationContext = { + pagination: PaginationEnum.TOKEN, + ...paginationTokenProperties, + }; + const paginationContextPage: PaginationContext = { + pagination: PaginationEnum.PAGE, + ...paginationTokenProperties, + }; + const paginationContextPage2: PaginationContext = { + pagination: PaginationEnum.PAGE2, + ...paginationTokenProperties, + }; + const paginationContextPage3: PaginationContext = { + pagination: PaginationEnum.PAGE3, + ...paginationTokenProperties, + }; + + + describe('hasMore', () => { + + it('should return "true" when the PaginationContext is "TOKEN" and there are more elements', async () => { + // Given + const response = { + elements: ['H', 'He'], + nextPageToken: 'nextPageToken', + totalSize: 10, + }; + const paginationContext = { ...paginationContextToken }; + + // When + const hasMoreElements = hasMore(response, paginationContext); + + // Then + expect(hasMoreElements).toBeTruthy(); + }); + + it('should return "false" when the PaginationContext is "TOKEN" and there are no more elements', async () => { + // Given + const response = { + elements: ['H', 'He'], + nextPageToken: '', + totalSize: 2, + }; + const paginationContext = { ...paginationContextToken }; + + // When + const hasMoreElements = hasMore(response, paginationContext); + + // Then + expect(hasMoreElements).toBeFalsy(); + }); + + it('should return "true" when the PaginationContext is "PAGE" and there are more elements', async () => { + // Given + const response = { + elements: ['H', 'He'], + page: 0, + page_size: 2, + count: 10, + }; + const paginationContext = { ...paginationContextPage }; + + // When + const hasMoreElements = hasMore(response, paginationContext); + + // Then + expect(hasMoreElements).toBeTruthy(); + }); + + it('should return "false" when the PaginationContext is "PAGE" and there are no more elements', async () => { + // Given + const response = { + elements: ['H', 'He'], + page: 0, + page_size: 2, + count: 2, + }; + const paginationContext = { ...paginationContextPage }; + + // When + const hasMoreElements = hasMore(response, paginationContext); + + // Then + expect(hasMoreElements).toBeFalsy(); + }); + + it('should return "true" when the PaginationContext is "PAGE2" and there are more elements', async () => { + // Given + const response = { + elements: ['H', 'He'], + pageNumber: 1, + pageSize: 2, + totalItems: 10, + }; + const paginationContext = { ...paginationContextPage2 }; + + // When + const hasMoreElements = hasMore(response, paginationContext); + + // Then + expect(hasMoreElements).toBeTruthy(); + }); + + it('should return "false" when the PaginationContext is "PAGE2" and there are no more elements', async () => { + // Given + const response = { + elements: ['H', 'He'], + pageNumber: 1, + pageSize: 2, + totalItems: 2, + }; + const paginationContext = { ...paginationContextPage2 }; + + // When + const hasMoreElements = hasMore(response, paginationContext); + + // Then + expect(hasMoreElements).toBeFalsy(); + }); + + it('should return "true" when the PaginationContext is "PAGE3" and there are more elements', async () => { + // Given + const response = { + elements: ['H', 'He'], + pageNumber: 1, + pageSize: 2, + totalItems: 10, + totalPages: 5, + }; + const paginationContext = { ...paginationContextPage3 }; + + // When + const hasMoreElements = hasMore(response, paginationContext); + + // Then + expect(hasMoreElements).toBeTruthy(); + }); + + it('should return "false" when the PaginationContext is "PAGE3" and there are no more elements', async () => { + // Given + const response = { + elements: ['H', 'He'], + pageNumber: 1, + pageSize: 2, + totalItems: 2, + totalPages: 1, + }; + const paginationContext = { ...paginationContextPage3 }; + + // When + const hasMoreElements = hasMore(response, paginationContext); + + // Then + expect(hasMoreElements).toBeFalsy(); + }); + + }); + + describe('calculateNextPage', () => { + + it('should return the next page token when the PaginationContext is "TOKEN"', () => { + // Given + const response = { + elements: ['H', 'He'], + nextPageToken: 'nextPageToken', + totalSize: 10, + }; + const paginationContext = { ...paginationContextToken }; + + // When + const nextPage = calculateNextPage(response, paginationContext); + + // Then + expect(nextPage).toBe('nextPageToken'); + }); + + it('should return the next page value when the PaginationContext is "PAGE"', () => { + // Given + const response = { + elements: ['H', 'He'], + page: 0, + page_size: 2, + count: 10, + }; + const paginationContext = { ...paginationContextPage }; + + // When + const nextPage = calculateNextPage(response, paginationContext); + + // Then + expect(nextPage).toBe('1'); + }); + + it('should return the next page value when the PaginationContext is "PAGE2"', () => { + // Given + const response = { + elements: ['H', 'He'], + pageNumber: 1, + pageSize: 2, + totalItems: 10, + }; + const paginationContext = { ...paginationContextPage2 }; + + // When + const nextPage = calculateNextPage(response, paginationContext); + + // Then + expect(nextPage).toBe('2'); + }); + + it('should return the next page value when the PaginationContext is "PAGE3"', () => { + // Given + const response = { + elements: ['H', 'He'], + pageNumber: 1, + pageSize: 2, + totalItems: 2, + totalPages: 1, + }; + const paginationContext = { ...paginationContextPage3 }; + + // When + const nextPage = calculateNextPage(response, paginationContext); + + // Then + expect(nextPage).toBe('2'); + }); + }); + +});