});
} else {
this.setState({
- tokenIsValid,
loading: false
});
}
return checkPersonalAccessTokenIsValid(settings.key).catch(() => false);
};
- fetchProjects = (pageIndex = 1, query?: string) => {
+ handleError = () => {
+ if (this.mounted) {
+ this.setState({ tokenIsValid: false });
+ }
+
+ return undefined;
+ };
+
+ fetchProjects = async (pageIndex = 1, query?: string) => {
const { settings } = this.state;
if (!settings) {
return Promise.resolve(undefined);
}
- return getGitlabProjects({
- almSetting: settings.key,
- page: pageIndex,
- pageSize: GITLAB_PROJECTS_PAGESIZE,
- query
- }).catch(() => undefined);
+ try {
+ return await getGitlabProjects({
+ almSetting: settings.key,
+ page: pageIndex,
+ pageSize: GITLAB_PROJECTS_PAGESIZE,
+ query
+ });
+ } catch (_) {
+ return this.handleError();
+ }
};
- handleImport = async (gitlabProjectId: string) => {
+ doImport = async (gitlabProjectId: string) => {
const { settings } = this.state;
if (!settings) {
- return;
+ return Promise.resolve(undefined);
+ }
+
+ try {
+ return await importGitlabProject({
+ almSetting: settings.key,
+ gitlabProjectId
+ });
+ } catch (_) {
+ return this.handleError();
}
+ };
+ handleImport = async (gitlabProjectId: string) => {
this.setState({ importingGitlabProjectId: gitlabProjectId });
- const result = await importGitlabProject({
- almSetting: settings.key,
- gitlabProjectId
- }).catch(() => undefined);
+ const result = await this.doImport(gitlabProjectId);
if (this.mounted) {
this.setState({ importingGitlabProjectId: undefined });
it('should correctly handle a valid PAT', async () => {
(checkPersonalAccessTokenIsValid as jest.Mock).mockResolvedValueOnce(true);
+ (getGitlabProjects as jest.Mock).mockResolvedValueOnce({
+ projects: [mockGitlabProject()],
+ projectsPaging: {
+ pageIndex: 1,
+ pageSize: 10,
+ total: 1
+ }
+ });
const wrapper = shallowRender();
await waitAndUpdate(wrapper);
expect(wrapper.state().tokenIsValid).toBe(true);
expect(setAlmPersonalAccessToken).not.toHaveBeenCalled();
});
+it('should handle errors when fetching projects', async () => {
+ (getGitlabProjects as jest.Mock).mockRejectedValueOnce({});
+
+ const wrapper = shallowRender();
+ await waitAndUpdate(wrapper);
+
+ expect(wrapper.state().tokenIsValid).toBe(false);
+});
+
+it('should handle errors when importing a project', async () => {
+ (importGitlabProject as jest.Mock).mockRejectedValueOnce({});
+ (getGitlabProjects as jest.Mock).mockResolvedValueOnce({
+ projects: [mockGitlabProject()],
+ projectsPaging: {
+ pageIndex: 1,
+ pageSize: 10,
+ total: 1
+ }
+ });
+
+ const wrapper = shallowRender();
+ await waitAndUpdate(wrapper);
+
+ expect(wrapper.state().tokenIsValid).toBe(true);
+
+ await wrapper.instance().handleImport('gitlabId');
+ await waitAndUpdate(wrapper);
+
+ expect(wrapper.state().tokenIsValid).toBe(false);
+});
+
function shallowRender(props: Partial<GitlabProjectCreate['props']> = {}) {
return shallow<GitlabProjectCreate>(
<GitlabProjectCreate