Skip to content

Commit dc57f75

Browse files
chore: [M3-10082] - Remove ramda from the utilities package (#12313)
* remove ramda and add a unit test * add a commment * improve test by reordering * improve test comment * Added changeset: Ramda dependency * Added changeset: Unit test for `getAll` --------- Co-authored-by: Banks Nussman <[email protected]>
1 parent 86f7d58 commit dc57f75

File tree

6 files changed

+91
-20
lines changed

6 files changed

+91
-20
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@linode/utilities": Added
3+
---
4+
5+
Unit test for `getAll` ([#12313](https://github.com/linode/manager/pull/12313))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@linode/utilities": Removed
3+
---
4+
5+
Ramda dependency ([#12313](https://github.com/linode/manager/pull/12313))

packages/utilities/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"dependencies": {
3434
"@linode/api-v4": "workspace:*",
3535
"luxon": "3.4.4",
36-
"ramda": "~0.25.0",
3736
"react": "^18.2.0",
3837
"react-dom": "^18.2.0"
3938
},
@@ -43,7 +42,6 @@
4342
"@testing-library/jest-dom": "~6.4.2",
4443
"@testing-library/react": "~16.0.0",
4544
"@types/luxon": "3.4.2",
46-
"@types/ramda": "0.25.16",
4745
"@types/react": "^18.2.55",
4846
"@types/react-dom": "^18.2.18",
4947
"factory.ts": "^0.5.1"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
3+
import { linodeFactory } from '../factories';
4+
import { getAll } from './getAll';
5+
6+
describe('getAll', () => {
7+
it('should fetch the first page, then all other pages', async () => {
8+
const getLinodes = vi.fn();
9+
const getAllLinodes = getAll(getLinodes, 25);
10+
11+
const firstPage = {
12+
data: linodeFactory.buildList(25),
13+
page: 1,
14+
pages: 3,
15+
results: 75,
16+
};
17+
18+
const secondPage = {
19+
data: linodeFactory.buildList(25),
20+
page: 2,
21+
pages: 3,
22+
results: 75,
23+
};
24+
25+
const thirdPage = {
26+
data: linodeFactory.buildList(25),
27+
page: 3,
28+
pages: 3,
29+
results: 75,
30+
};
31+
32+
getLinodes.mockImplementationOnce(async () => firstPage);
33+
34+
getLinodes.mockImplementationOnce(async () => secondPage);
35+
36+
getLinodes.mockImplementationOnce(async () => thirdPage);
37+
38+
const result = await getAllLinodes();
39+
40+
// Verify the getter function was called the correct number of times total
41+
expect(getLinodes).toHaveBeenCalledTimes(3);
42+
43+
// Verify each call to our "getter" function has the correct params
44+
45+
// The first call should not include a page number, but should include the given page size
46+
expect(getLinodes).toHaveBeenNthCalledWith(1, { page_size: 25 }, undefined);
47+
48+
// The second call should include the page number and page size
49+
expect(getLinodes).toHaveBeenNthCalledWith(
50+
2,
51+
{ page: 2, page_size: 25 },
52+
undefined,
53+
);
54+
55+
// The third call should include the page number and page size
56+
expect(getLinodes).toHaveBeenNthCalledWith(
57+
3,
58+
{ page: 3, page_size: 25 },
59+
undefined,
60+
);
61+
62+
// Verify the data is present and in the correct order
63+
expect(result.data).toStrictEqual([
64+
...firstPage.data,
65+
...secondPage.data,
66+
...thirdPage.data,
67+
]);
68+
69+
// Verify the result count is correct
70+
expect(result.results).toBe(75);
71+
});
72+
});

packages/utilities/src/helpers/getAll.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { range } from 'ramda';
2-
31
import { API_MAX_PAGE_SIZE } from '../constants';
42

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

77-
// Create an iterable list of the remaining pages.
78-
const remainingPages = range(page + 1, pages + 1);
79-
8075
const promises: Promise<any>[] = [];
81-
remainingPages.forEach((thisPage) => {
82-
const promise = getter(
83-
{ ...pagination, page: thisPage },
84-
filter,
85-
).then((response) => response.data);
76+
77+
// For all remaining pages, build a promise for each page that will be resolved in parallel.
78+
for (let i = page + 1; i < pages + 1; i++) {
79+
const promise = getter({ ...pagination, page: i }, filter).then(
80+
(response) => response.data,
81+
);
82+
8683
promises.push(promise);
87-
});
88-
//
84+
}
85+
8986
return (
9087
Promise.all(promises)
9188
/** We're given data[][], so we flatten that, and append the first page response. */

pnpm-lock.yaml

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)