diff options
author | Wouter Admiraal <wouter.admiraal@sonarsource.com> | 2022-05-23 12:23:02 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-05-24 20:10:14 +0000 |
commit | 914cef832bc5e781c560d6d476ccba93caf7eefc (patch) | |
tree | 7666bed4bb937f2f40f6704140909461ecb5bb80 /server | |
parent | 632b007c0de38c21b57154035d6f2a1f9d923ff7 (diff) | |
download | sonarqube-914cef832bc5e781c560d6d476ccba93caf7eefc.tar.gz sonarqube-914cef832bc5e781c560d6d476ccba93caf7eefc.zip |
[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).
Diffstat (limited to 'server')
3 files changed, 31 insertions, 29 deletions
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<Props, State> { }; } - 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<Props, State> { } }; - handleGenerateClick = (event: React.MouseEvent<HTMLAnchorElement>) => { - event.preventDefault(); - this.setState({ selection: 'generate' }); - }; - - handleUseExistingClick = (event: React.MouseEvent<HTMLAnchorElement>) => { - 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)) { |