diff --git a/apps/site/components/Downloads/Release/InstallationMethodDropdown.tsx b/apps/site/components/Downloads/Release/InstallationMethodDropdown.tsx index 478ead9f91ed1..8a493e9f7c99f 100644 --- a/apps/site/components/Downloads/Release/InstallationMethodDropdown.tsx +++ b/apps/site/components/Downloads/Release/InstallationMethodDropdown.tsx @@ -58,13 +58,16 @@ const InstallationMethodDropdown: FC = () => { // eslint-disable-next-line @eslint-react/exhaustive-deps }, [parsedInstallMethods, release.installMethod, release.os]); - // We set the Platform to the next available platform when the current - // one is not valid anymore due to OS or Version changes + // We select the first available method in display order when the current + // one is not valid anymore due to OS or Version changes. useEffect( () => { if (release.os !== 'LOADING' && release.installMethod !== '') { release.setInstallMethod( - nextItem(release.installMethod, parsedInstallMethods) + nextItem( + release.installMethod, + groupedMethods.flatMap(({ items }) => items) + ) ); } }, diff --git a/apps/site/components/Downloads/Release/__tests__/InstallationMethodDropdown.test.jsx b/apps/site/components/Downloads/Release/__tests__/InstallationMethodDropdown.test.jsx new file mode 100644 index 0000000000000..c7e0eb4ac2206 --- /dev/null +++ b/apps/site/components/Downloads/Release/__tests__/InstallationMethodDropdown.test.jsx @@ -0,0 +1,83 @@ +import assert from 'node:assert/strict'; +import { beforeEach, describe, it, mock } from 'node:test'; + +import { act, render } from '@testing-library/react'; +import { useState } from 'react'; + +import { ReleaseContext } from '#site/providers/releaseProvider'; + +const Select = mock.fn(() => null); +let setOS; +let setVersion; + +mock.module('@node-core/ui-components/Common/Select', { + defaultExport: Select, +}); + +const { default: InstallationMethodDropdown } = + await import('../InstallationMethodDropdown'); + +const renderDropdown = (initialOS, initialMethod = '') => { + const Dropdown = () => { + const [os, updateOS] = useState(initialOS); + const [version, updateVersion] = useState('v24.0.0'); + const [installMethod, setInstallMethod] = useState(initialMethod); + setOS = updateOS; + setVersion = updateVersion; + + return ( + + + + ); + }; + + render(); +}; + +const selectedValue = () => Select.mock.calls.at(-1).arguments[0].value; + +describe('InstallationMethodDropdown', () => { + beforeEach(() => { + Select.mock.resetCalls(); + }); + + it('selects the first available displayed method after an OS change', () => { + renderDropdown('MAC'); + assert.equal(selectedValue(), 'NVM'); + + act(() => setOS('WIN')); + + const { values, value } = Select.mock.calls.at(-1).arguments[0]; + assert.equal( + value, + values.flatMap(group => group.items).find(item => !item.disabled).value + ); + assert.equal(value, 'FNM'); + }); + + it('keeps a compatible community method when the OS changes', () => { + renderDropdown('MAC', 'BREW'); + + act(() => setOS('LINUX')); + + assert.equal(selectedValue(), 'BREW'); + }); + + it('selects the first available displayed method after a version change', () => { + renderDropdown('WIN', 'CHOCO'); + + act(() => setVersion('v27.0.0-alpha.1')); + + assert.equal(selectedValue(), 'FNM'); + }); +});