Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/renderer/components/dialog-token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface TokenDialogState {
verifying: boolean;
error: boolean;
errorMessage?: string;
isTokenUpdateAction?: boolean;
}

const TOKEN_SCOPES = ['gist'].join();
Expand All @@ -40,6 +41,7 @@ export const TokenDialog = observer(
error: false,
errorMessage: undefined,
tokenInput: '',
isTokenUpdateAction: props.appState.gitHubToken !== '',
};

this.onSubmitToken = this.onSubmitToken.bind(this);
Expand Down Expand Up @@ -101,7 +103,9 @@ export const TokenDialog = observer(
errorMessage:
'Invalid GitHub token. Please check your token and try again.',
});
this.props.appState.gitHubToken = null;
if (!this.state.isTokenUpdateAction) {
this.props.appState.gitHubToken = null;
}
return;
}

Expand All @@ -113,7 +117,9 @@ export const TokenDialog = observer(
errorMessage:
'Token is missing the "gist" scope. Please generate a new token with gist permissions.',
});
this.props.appState.gitHubToken = null;
if (!this.state.isTokenUpdateAction) {
this.props.appState.gitHubToken = null;
}
return;
}

Expand Down Expand Up @@ -144,6 +150,7 @@ export const TokenDialog = observer(
error: false,
errorMessage: undefined,
tokenInput: '',
isTokenUpdateAction: false,
});
}

Expand Down
14 changes: 12 additions & 2 deletions src/renderer/components/settings-general-github.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as React from 'react';

import { Button, Callout, Checkbox, FormGroup } from '@blueprintjs/core';
import {
Button,
ButtonGroup,
Callout,
Checkbox,
FormGroup,
} from '@blueprintjs/core';
import { observer } from 'mobx-react';

import { AppState } from '../state';
Expand Down Expand Up @@ -55,7 +61,11 @@ export const GitHubSettings = observer(
personal access token you gave us, we logged you into GitHub as{' '}
<code>{gitHubLogin}</code>.
</p>
<Button onClick={signOut} icon="log-out" text="Sign out" />

<ButtonGroup>
<Button onClick={this.signIn} icon="log-in" text="Update token" />
<Button onClick={signOut} icon="log-out" text="Sign out" />
</ButtonGroup>
</Callout>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,18 @@ exports[`GitHubSettings component > renders when signed in 1`] = `
</code>
.
</p>
<Blueprint3.Button
icon="log-out"
onClick={[MockFunction spy]}
text="Sign out"
/>
<Blueprint3.ButtonGroup>
<Blueprint3.Button
icon="log-in"
onClick={[Function]}
text="Update token"
/>
<Blueprint3.Button
icon="log-out"
onClick={[MockFunction spy]}
text="Sign out"
/>
</Blueprint3.ButtonGroup>
</Blueprint3.Callout>
<Blueprint3.Callout>
<Blueprint3.FormGroup>
Expand Down
18 changes: 18 additions & 0 deletions tests/renderer/components/dialog-token-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('TokenDialog component', () => {
expect(wrapper.state()).toEqual({
verifying: false,
error: false,
isTokenUpdateAction: false,
errorMessage: undefined,
tokenInput: '',
});
Expand All @@ -94,6 +95,7 @@ describe('TokenDialog component', () => {
expect(wrapper.state()).toEqual({
verifying: false,
error: false,
isTokenUpdateAction: false,
errorMessage: undefined,
tokenInput: '',
});
Expand Down Expand Up @@ -186,6 +188,22 @@ describe('TokenDialog component', () => {
expect(store.gitHubToken).toEqual(null);
});

it('handles the invalid token update', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also check that a valid token update works as expected!

vi.mocked(mockOctokit.users.getAuthenticated).mockRejectedValue(
new Error('Bad credentials'),
);

store.gitHubToken = mockValidToken;
const wrapper = shallow(<TokenDialog appState={store} />);
wrapper.setState({ tokenInput: mockInvalidToken });
const instance: any = wrapper.instance();

expect(store.gitHubToken).toEqual(mockValidToken);
expect(wrapper.state('isTokenUpdateAction')).toBe(true);
await instance.onSubmitToken();
expect(store.gitHubToken).toEqual(mockValidToken);
});

it('handles missing gist scope', async () => {
vi.mocked(mockOctokit.users.getAuthenticated).mockResolvedValue({
data: mockUser,
Expand Down
Loading