fix: make error handling work; add tests for RepoUrlPickerRepoName

Signed-off-by: Benjamin Janssens <benji.janssens@gmail.com>
This commit is contained in:
Benjamin Janssens
2024-06-07 16:46:36 +02:00
parent be26d90de1
commit f0781a5394
2 changed files with 33 additions and 15 deletions
@@ -97,11 +97,7 @@ export const BitbucketRepoPicker = (props: {
}
};
try {
updateAvailableWorkspaces();
} catch {
setAvailableWorkspaces([]);
}
updateAvailableWorkspaces().catch(() => setAvailableWorkspaces([]));
},
500,
[client, host],
@@ -130,11 +126,9 @@ export const BitbucketRepoPicker = (props: {
}
};
try {
updateAvailableRepositories();
} catch {
onChange({ availableRepos: [] });
}
updateAvailableRepositories().catch(() =>
onChange({ availableRepos: [] }),
);
},
500,
[client, workspace, project, onChange],
@@ -161,11 +155,7 @@ export const BitbucketRepoPicker = (props: {
}
};
try {
updateAvailableProjects();
} catch {
setAvailableProjects([]);
}
updateAvailableProjects().catch(() => setAvailableProjects([]));
},
500,
[client, workspace],
@@ -16,6 +16,7 @@
import React from 'react';
import { RepoUrlPickerRepoName } from './RepoUrlPickerRepoName';
import { render, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
describe('RepoUrlPickerRepoName', () => {
it('should call onChange with the first allowed repo if there is none set already', async () => {
@@ -73,4 +74,31 @@ describe('RepoUrlPickerRepoName', () => {
expect(onChange).toHaveBeenCalledWith('foo');
});
it('should autocomplete with provided availableRepos', async () => {
const availableRepos = ['foo', 'bar'];
const onChange = jest.fn();
const { getByRole, getByText } = render(
<RepoUrlPickerRepoName
onChange={onChange}
availableRepos={availableRepos}
rawErrors={[]}
/>,
);
// Open the Autocomplete dropdown
const input = getByRole('textbox');
await userEvent.click(input);
// Verify that available repos are shown
for (const repo of availableRepos) {
expect(getByText(repo)).toBeInTheDocument();
}
// Verify that selecting an option calls onChange
await userEvent.click(getByText('foo'));
expect(onChange).toHaveBeenCalledWith('foo');
});
});