Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/utilities/.changeset/pr-12313-added-1748968238140.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/utilities": Added
---

Unit test for `getAll` ([#12313](https://github.com/linode/manager/pull/12313))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/utilities": Removed
---

Ramda dependency ([#12313](https://github.com/linode/manager/pull/12313))
2 changes: 0 additions & 2 deletions packages/utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"dependencies": {
"@linode/api-v4": "workspace:*",
"luxon": "3.4.4",
"ramda": "~0.25.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand All @@ -43,7 +42,6 @@
"@testing-library/jest-dom": "~6.4.2",
"@testing-library/react": "~16.0.0",
"@types/luxon": "3.4.2",
"@types/ramda": "0.25.16",
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.18",
"factory.ts": "^0.5.1"
Expand Down
72 changes: 72 additions & 0 deletions packages/utilities/src/helpers/getAll.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, expect, it, vi } from 'vitest';

import { linodeFactory } from '../factories';
import { getAll } from './getAll';

describe('getAll', () => {
it('should fetch the first page, then all other pages', async () => {
const getLinodes = vi.fn();
const getAllLinodes = getAll(getLinodes, 25);

const firstPage = {
data: linodeFactory.buildList(25),
page: 1,
pages: 3,
results: 75,
};

const secondPage = {
data: linodeFactory.buildList(25),
page: 2,
pages: 3,
results: 75,
};

const thirdPage = {
data: linodeFactory.buildList(25),
page: 3,
pages: 3,
results: 75,
};

getLinodes.mockImplementationOnce(async () => firstPage);

getLinodes.mockImplementationOnce(async () => secondPage);

getLinodes.mockImplementationOnce(async () => thirdPage);

const result = await getAllLinodes();

// Verify the getter function was called the correct number of times total
expect(getLinodes).toHaveBeenCalledTimes(3);

// Verify each call to our "getter" function has the correct params

// The first call should not include a page number, but should include the given page size
expect(getLinodes).toHaveBeenNthCalledWith(1, { page_size: 25 }, undefined);

// The second call should include the page number and page size
expect(getLinodes).toHaveBeenNthCalledWith(
2,
{ page: 2, page_size: 25 },
undefined,
);

// The third call should include the page number and page size
expect(getLinodes).toHaveBeenNthCalledWith(
3,
{ page: 3, page_size: 25 },
undefined,
);

// Verify the data is present and in the correct order
expect(result.data).toStrictEqual([
...firstPage.data,
...secondPage.data,
...thirdPage.data,
]);

// Verify the result count is correct
expect(result.results).toBe(75);
});
});
21 changes: 9 additions & 12 deletions packages/utilities/src/helpers/getAll.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { range } from 'ramda';

import { API_MAX_PAGE_SIZE } from '../constants';

import type { Filter, Params } from '@linode/api-v4';
Expand Down Expand Up @@ -74,18 +72,17 @@ export const getAll: <T>(
cb(results);
}

// Create an iterable list of the remaining pages.
const remainingPages = range(page + 1, pages + 1);

const promises: Promise<any>[] = [];
remainingPages.forEach((thisPage) => {
const promise = getter(
{ ...pagination, page: thisPage },
filter,
).then((response) => response.data);

// For all remaining pages, build a promise for each page that will be resolved in parallel.
for (let i = page + 1; i < pages + 1; i++) {
const promise = getter({ ...pagination, page: i }, filter).then(
(response) => response.data,
);

promises.push(promise);
});
//
}

return (
Promise.all(promises)
/** We're given data[][], so we flatten that, and append the first page response. */
Expand Down
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.