|
| 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