From: Wouter Admiraal Date: Mon, 23 May 2022 10:23:02 +0000 (+0200) Subject: [NO JIRA] Remove dead code, fix 'initial token name' bug X-Git-Tag: 9.5.0.56709~111 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=914cef832bc5e781c560d6d476ccba93caf7eefc;p=sonarqube.git [NO JIRA] Remove dead code, fix 'initial token name' bug - Remove methods that are no longer used - Passing the initial token name as a prop was broken, and the name was always reset to an empty string. This is now fixed. - Passing an initial token name to `getUniqueTokenName()` should not be optional (doesn't make any sense to pass an empty string). --- diff --git a/server/sonar-web/src/main/js/components/tutorials/manual/TokenStep.tsx b/server/sonar-web/src/main/js/components/tutorials/manual/TokenStep.tsx index 495e3c3b1a0..328a306788d 100644 --- a/server/sonar-web/src/main/js/components/tutorials/manual/TokenStep.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/manual/TokenStep.tsx @@ -66,22 +66,21 @@ export default class TokenStep extends React.PureComponent { }; } - componentDidMount() { + async componentDidMount() { this.mounted = true; - getTokens(this.props.currentUser.login).then( - tokens => { - if (this.mounted) { - this.setState({ tokens }); - if ( - this.props.initialTokenName !== undefined && - this.props.initialTokenName === this.state.tokenName - ) { - this.setState({ tokenName: getUniqueTokenName(tokens) }); - } - } - }, - () => {} - ); + const { currentUser, initialTokenName } = this.props; + const { tokenName } = this.state; + + const tokens = await getTokens(currentUser.login).catch(() => { + /* noop */ + }); + + if (tokens && this.mounted) { + this.setState({ tokens }); + if (initialTokenName !== undefined && initialTokenName === tokenName) { + this.setState({ tokenName: getUniqueTokenName(tokens, initialTokenName) }); + } + } } componentWillUnmount() { @@ -145,16 +144,6 @@ export default class TokenStep extends React.PureComponent { } }; - handleGenerateClick = (event: React.MouseEvent) => { - event.preventDefault(); - this.setState({ selection: 'generate' }); - }; - - handleUseExistingClick = (event: React.MouseEvent) => { - event.preventDefault(); - this.setState({ selection: 'use-existing' }); - }; - handleModeChange = (mode: string) => { this.setState({ selection: mode }); }; diff --git a/server/sonar-web/src/main/js/components/tutorials/manual/__tests__/TokenStep-test.tsx b/server/sonar-web/src/main/js/components/tutorials/manual/__tests__/TokenStep-test.tsx index ae3ca36c353..b3b26676f0a 100644 --- a/server/sonar-web/src/main/js/components/tutorials/manual/__tests__/TokenStep-test.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/manual/__tests__/TokenStep-test.tsx @@ -19,16 +19,29 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; +import { getTokens } from '../../../../api/user-tokens'; import { mockLoggedInUser } from '../../../../helpers/testMocks'; import { change, click, submit, waitAndUpdate } from '../../../../helpers/testUtils'; import TokenStep from '../TokenStep'; jest.mock('../../../../api/user-tokens', () => ({ - getTokens: () => Promise.resolve([{ name: 'foo' }]), - generateToken: () => Promise.resolve({ token: 'abcd1234' }), - revokeToken: () => Promise.resolve() + getTokens: jest.fn().mockResolvedValue([{ name: 'foo' }]), + generateToken: jest.fn().mockResolvedValue({ token: 'abcd1234' }), + revokeToken: jest.fn().mockResolvedValue(null) })); +it('sets an initial token name', async () => { + (getTokens as jest.Mock).mockResolvedValueOnce([{ name: 'fôo' }]); + const wrapper = shallowRender({ initialTokenName: 'fôo' }); + await waitAndUpdate(wrapper); + expect( + wrapper + .dive() + .find('input') + .props().value + ).toBe('fôo 1'); +}); + it('generates token', async () => { const wrapper = shallowRender(); await waitAndUpdate(wrapper); diff --git a/server/sonar-web/src/main/js/components/tutorials/utils.ts b/server/sonar-web/src/main/js/components/tutorials/utils.ts index 6e040c3f09a..1e94fa59921 100644 --- a/server/sonar-web/src/main/js/components/tutorials/utils.ts +++ b/server/sonar-web/src/main/js/components/tutorials/utils.ts @@ -37,7 +37,7 @@ sonarqube { }`; } -export function getUniqueTokenName(tokens: UserToken[], initialTokenName = '') { +export function getUniqueTokenName(tokens: UserToken[], initialTokenName: string) { const hasToken = (name: string) => tokens.find(token => token.name === name) !== undefined; if (!hasToken(initialTokenName)) {