};
}
- 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() {
}
};
- 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 });
};
*/
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);
}`;
}
-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)) {