When repo registry fails, error to console (#544)

Some slight changes were needed to the other test code to avoid spurious
errors. Specifically, we now always set up a mocked fetch response in
non-failure cases, even if we don't wait for it to resolve.

Test plan: I manually tested it, also see the new unit tests.
This commit is contained in:
Dandelion Mané 2018-07-27 15:42:18 -07:00 committed by GitHub
parent 126fdb69dc
commit 0b0815eb12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

@ -90,6 +90,7 @@ export async function loadStatus(localStore: LocalStore): Promise<Status> {
try {
const response = await fetch(REPO_REGISTRY_API);
if (!response.ok) {
console.error(response);
return {type: "FAILURE"};
}
const json = await response.json();
@ -105,6 +106,7 @@ export async function loadStatus(localStore: LocalStore): Promise<Status> {
const sortedRepos = sortBy(availableRepos, (r) => r.owner, (r) => r.name);
return {type: "VALID", availableRepos: sortedRepos, selectedRepo};
} catch (e) {
console.error(e);
return {type: "FAILURE"};
}
}
@ -178,7 +180,10 @@ export class PureRepositorySelect extends React.PureComponent<
case "NO_REPOS":
return this.renderError("Error: No repositories found.");
case "FAILURE":
return this.renderError("Error: Unable to load repository registry.");
return this.renderError(
"Error: Unable to load repository registry. " +
"See console for details."
);
default:
throw new Error((status.type: empty));
}

View File

@ -58,7 +58,9 @@ describe("app/credExplorer/RepositorySelect", () => {
<PureRepositorySelect status={{type: "FAILURE"}} onChange={jest.fn()} />
);
const span = e.find("span");
expect(span.text()).toBe("Error: Unable to load repository registry.");
expect(span.text()).toBe(
"Error: Unable to load repository registry. See console for details."
);
});
it("renders a select with all available repos as options", () => {
const availableRepos = [
@ -147,16 +149,22 @@ describe("app/credExplorer/RepositorySelect", () => {
});
it("returns FAILURE on invalid fetch response", () => {
fetch.mockResponseOnce(JSON.stringify(["hello"]));
expect.assertions(3);
expect.assertions(4);
return loadStatus(testLocalStore()).then((status) => {
expect(status).toEqual({type: "FAILURE"});
expect(console.error).toHaveBeenCalledTimes(1);
// $ExpectFlowError
console.error = jest.fn();
});
});
it("returns FAILURE on fetch failure", () => {
fetch.mockReject(new Error("some failure"));
expect.assertions(3);
expect.assertions(4);
return loadStatus(testLocalStore()).then((status) => {
expect(status).toEqual({type: "FAILURE"});
expect(console.error).toHaveBeenCalledTimes(1);
// $ExpectFlowError
console.error = jest.fn();
});
});
it("loads selectedRepo from localStore, if available", () => {
@ -258,6 +266,7 @@ describe("app/credExplorer/RepositorySelect", () => {
describe("RepositorySelect", () => {
it("initially renders a LocalStoreRepositorySelect with status LOADING", () => {
mockRegistry([{owner: "irrelevant", name: "unused"}]);
const e = shallow(
<RepositorySelect onChange={jest.fn()} localStore={testLocalStore()} />
);
@ -319,15 +328,19 @@ describe("app/credExplorer/RepositorySelect", () => {
);
await waitForUpdate(e);
expect(onChange).toHaveBeenCalledTimes(0);
expect(console.error).toHaveBeenCalledTimes(1);
// $ExpectFlowError
console.error = jest.fn();
});
it("child onChange triggers parent onChange", () => {
const onChange = jest.fn();
const repo = {owner: "foo", name: "bar"};
mockRegistry([repo]);
const e = mount(
<RepositorySelect onChange={onChange} localStore={testLocalStore()} />
);
const child = e.find(PureRepositorySelect);
const repo = {owner: "foo", name: "bar"};
child.props().onChange(repo);
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(repo);