diff --git a/web/e.cash/data/__tests__/blog.test.js b/web/e.cash/data/__tests__/blog.test.js index 9ee9eb178..8ff54f5f0 100644 --- a/web/e.cash/data/__tests__/blog.test.js +++ b/web/e.cash/data/__tests__/blog.test.js @@ -1,120 +1,126 @@ // Copyright (c) 2023 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. import { getPageCount, getBlogPosts } from '../blog.js'; import { mockBlogPosts1, mockBlogPosts2, mockBlogPosts3, } from '../__mocks__/blogMock.js'; describe('getPageCount', () => { beforeEach(() => { global.fetch = jest.fn(); }); afterEach(() => { global.fetch.mockClear(); delete global.fetch; }); it('should call an api endpoint and return a number from the response', async () => { const mockResponse = { meta: { pagination: { pageCount: 5, }, }, }; global.fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockResponse), }); const result = await getPageCount(); expect(global.fetch).toHaveBeenCalledWith( 'https://strapi.fabien.cash/api/posts', ); expect(result).toEqual(5); }); it('should throw an error when fetch fails', async () => { global.fetch.mockImplementation(() => { throw new Error('Failed to fetch api'); }); await expect(getPageCount()).rejects.toThrow('Failed to fetch api'); }); it('should throw an error if api returns wrong shape', async () => { const mockBadResponse = { meta: {}, }; global.fetch.mockResolvedValue({ json: jest.fn().mockResolvedValue(mockBadResponse), }); await expect(getPageCount()).rejects.toThrow( "TypeError: Cannot read properties of undefined (reading 'pageCount')", ); }); }); describe('getBlogPosts', () => { beforeEach(() => { global.fetch = jest.fn(); }); afterEach(() => { global.fetch.mockClear(); delete global.fetch; }); it('should fetch each page for the blog posts api and return the responses', async () => { fetch.mockResolvedValueOnce({ json: jest.fn().mockResolvedValue({ meta: { pagination: { pageCount: 3 } }, }), }); fetch.mockResolvedValueOnce({ json: jest.fn().mockResolvedValue({ data: mockBlogPosts1 }), }); fetch.mockResolvedValueOnce({ json: jest.fn().mockResolvedValue({ data: mockBlogPosts2 }), }); fetch.mockResolvedValueOnce({ json: jest.fn().mockResolvedValue({ data: mockBlogPosts3 }), }); const result = await getBlogPosts(); expect(fetch).toHaveBeenNthCalledWith( 1, 'https://strapi.fabien.cash/api/posts', ); expect(fetch).toHaveBeenNthCalledWith( 2, 'https://strapi.fabien.cash/api/posts?pagination[page]=1&populate=*&sort=id:desc', ); expect(fetch).toHaveBeenNthCalledWith( 3, 'https://strapi.fabien.cash/api/posts?pagination[page]=2&populate=*&sort=id:desc', ); expect(fetch).toHaveBeenNthCalledWith( 4, 'https://strapi.fabien.cash/api/posts?pagination[page]=3&populate=*&sort=id:desc', ); expect(result).toEqual({ - props: [...mockBlogPosts1, ...mockBlogPosts2, ...mockBlogPosts3], + props: { + posts: [ + ...mockBlogPosts1, + ...mockBlogPosts2, + ...mockBlogPosts3, + ], + }, }); }); it('should throw an error when fetch fails', async () => { global.fetch.mockImplementation(() => { throw new Error('Failed to fetch api'); }); await expect(getBlogPosts()).rejects.toThrow('Failed to fetch api'); }); }); diff --git a/web/e.cash/data/blog.js b/web/e.cash/data/blog.js index ae205a088..c0d2bfa98 100644 --- a/web/e.cash/data/blog.js +++ b/web/e.cash/data/blog.js @@ -1,49 +1,49 @@ // Copyright (c) 2023 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. /** * Fetch blog posts page count * The API has a response limit * Need to determine page count in order to fetch all posts * @returns {number} the number of blog post pages */ export async function getPageCount() { let response; try { response = await fetch('https://strapi.fabien.cash/api/posts').then( res => res.json(), ); return response.meta.pagination.pageCount; } catch (err) { throw new Error(err); } } /** * Fetch blog posts and return array of combined responses * Use the response from getPageAmount to call each page * Add each page response to a responses array * return the responses in a props object to be used with getStaticProps * @returns {object} props object containing blog posts responses */ export async function getBlogPosts() { let response, - responses = [], + posts = [], propsObj; let pageCount = await getPageCount(); for (let pageNumber = 1; pageNumber <= pageCount; pageNumber++) { try { response = await fetch( `https://strapi.fabien.cash/api/posts?pagination[page]=${pageNumber}&populate=*&sort=id:desc`, ).then(res => res.json()); - responses = [...responses, ...response.data]; + posts = [...posts, ...response.data]; } catch (err) { throw new Error(err); } } propsObj = { - props: responses, + props: { posts }, }; return propsObj; }