Skip to content

Commit 17d2a0e

Browse files
committed
fix(downloads): respect display order when switching method
1 parent 6e7b066 commit 17d2a0e

2 files changed

Lines changed: 89 additions & 3 deletions

File tree

‎apps/site/components/Downloads/Release/InstallationMethodDropdown.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ const InstallationMethodDropdown: FC = () => {
5858
// eslint-disable-next-line @eslint-react/exhaustive-deps
5959
}, [parsedInstallMethods, release.installMethod, release.os]);
6060

61-
// We set the Platform to the next available platform when the current
62-
// one is not valid anymore due to OS or Version changes
61+
// We select the first available method in display order when the current
62+
// one is not valid anymore due to OS or Version changes.
6363
useEffect(
6464
() => {
6565
if (release.os !== 'LOADING' && release.installMethod !== '') {
6666
release.setInstallMethod(
67-
nextItem(release.installMethod, parsedInstallMethods)
67+
nextItem(
68+
release.installMethod,
69+
groupedMethods.flatMap(({ items }) => items)
70+
)
6871
);
6972
}
7073
},
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import assert from 'node:assert/strict';
2+
import { beforeEach, describe, it, mock } from 'node:test';
3+
4+
import { act, render } from '@testing-library/react';
5+
import { useState } from 'react';
6+
7+
import { ReleaseContext } from '#site/providers/releaseProvider';
8+
9+
const Select = mock.fn(() => null);
10+
let setOS;
11+
let setVersion;
12+
13+
mock.module('@node-core/ui-components/Common/Select', {
14+
defaultExport: Select,
15+
});
16+
17+
const { default: InstallationMethodDropdown } =
18+
await import('../InstallationMethodDropdown');
19+
20+
const renderDropdown = (initialOS, initialMethod = '') => {
21+
const Dropdown = () => {
22+
const [os, updateOS] = useState(initialOS);
23+
const [version, updateVersion] = useState('v24.0.0');
24+
const [installMethod, setInstallMethod] = useState(initialMethod);
25+
setOS = updateOS;
26+
setVersion = updateVersion;
27+
28+
return (
29+
<ReleaseContext
30+
value={{
31+
os,
32+
version,
33+
installMethod,
34+
platform: 'x64',
35+
release: { status: version.includes('alpha') ? 'Alpha' : 'LTS' },
36+
setInstallMethod,
37+
}}
38+
>
39+
<InstallationMethodDropdown />
40+
</ReleaseContext>
41+
);
42+
};
43+
44+
render(<Dropdown />);
45+
};
46+
47+
const selectedValue = () => Select.mock.calls.at(-1).arguments[0].value;
48+
49+
describe('InstallationMethodDropdown', () => {
50+
beforeEach(() => {
51+
Select.mock.resetCalls();
52+
});
53+
54+
it('selects the first available displayed method after an OS change', () => {
55+
renderDropdown('MAC');
56+
assert.equal(selectedValue(), 'NVM');
57+
58+
act(() => setOS('WIN'));
59+
60+
const { values, value } = Select.mock.calls.at(-1).arguments[0];
61+
assert.equal(
62+
value,
63+
values.flatMap(group => group.items).find(item => !item.disabled).value
64+
);
65+
assert.equal(value, 'FNM');
66+
});
67+
68+
it('keeps a compatible community method when the OS changes', () => {
69+
renderDropdown('MAC', 'BREW');
70+
71+
act(() => setOS('LINUX'));
72+
73+
assert.equal(selectedValue(), 'BREW');
74+
});
75+
76+
it('selects the first available displayed method after a version change', () => {
77+
renderDropdown('WIN', 'CHOCO');
78+
79+
act(() => setVersion('v27.0.0-alpha.1'));
80+
81+
assert.equal(selectedValue(), 'FNM');
82+
});
83+
});

0 commit comments

Comments
 (0)