fix(tests): use async for fetches instead of setTimeout

This commit is contained in:
Łukasz Mierzwa
2018-09-08 21:28:19 +01:00
parent 4d64b0b3ea
commit 525cd250b5
2 changed files with 47 additions and 40 deletions

View File

@@ -18,7 +18,9 @@ const LabelNameInput = observer(
populateNameSuggestions = action(() => {
const { matcher } = this.props;
fetch(FormatUnseeBackendURI(`labelNames.json`))
this.nameSuggestionsFetch = fetch(
FormatUnseeBackendURI(`labelNames.json`)
)
.then(
result => result.json(),
err => {
@@ -31,13 +33,18 @@ const LabelNameInput = observer(
value: value
}));
})
.catch(err => console.error(err.message));
.catch(err => {
console.error(err.message);
matcher.suggestions.names = [];
});
});
populateValueSuggestions = action(() => {
const { matcher } = this.props;
fetch(FormatUnseeBackendURI(`labelValues.json?name=${matcher.name}`))
this.valueSuggestionsFetch = fetch(
FormatUnseeBackendURI(`labelValues.json?name=${matcher.name}`)
)
.then(
result => result.json(),
err => {
@@ -50,7 +57,10 @@ const LabelNameInput = observer(
value: value
}));
})
.catch(err => console.error(err.message));
.catch(err => {
console.error(err.message);
matcher.suggestions.values = [];
});
});
onChange = action((newValue, actionMeta) => {

View File

@@ -79,55 +79,52 @@ describe("<LabelNameInput />", () => {
expect(matcher.name).toBe("job");
});
it("populates suggestions on mount", done => {
it("populates suggestions on mount", async () => {
fetch
.once(JSON.stringify(["name1", "name2", "name3"]))
.once(JSON.stringify(["value1", "value2", "value3"]));
ShallowLabelNameInput(true);
// use timeout since mount will call fetch
setTimeout(() => {
expect(matcher.suggestions.names).toHaveLength(3);
for (let i = 0; i < 3; i++) {
expect(matcher.suggestions.names[i]).toMatchObject(
MatcherValueToObject(`name${i + 1}`)
);
expect(matcher.suggestions.values[i]).toMatchObject(
MatcherValueToObject(`value${i + 1}`)
);
}
done();
}, 100);
const tree = ShallowLabelNameInput(true);
const instance = tree.instance();
await expect(instance.nameSuggestionsFetch).resolves.toBeUndefined();
await expect(instance.valueSuggestionsFetch).resolves.toBeUndefined();
expect(matcher.suggestions.names).toHaveLength(3);
for (let i = 0; i < 3; i++) {
expect(matcher.suggestions.names[i]).toMatchObject(
MatcherValueToObject(`name${i + 1}`)
);
expect(matcher.suggestions.values[i]).toMatchObject(
MatcherValueToObject(`value${i + 1}`)
);
}
});
it("handles fetch errors when populating suggestions", done => {
it("handles fetch errors when populating suggestions", async () => {
fetch.mockReject("error");
ShallowLabelNameInput(true);
// use timeout since mount will call fetch
setTimeout(() => {
expect(matcher.suggestions.names).toHaveLength(0);
done();
}, 100);
const tree = ShallowLabelNameInput(true);
const instance = tree.instance();
await expect(instance.nameSuggestionsFetch).resolves.toBeUndefined();
await expect(instance.valueSuggestionsFetch).resolves.toBeUndefined();
expect(matcher.suggestions.names).toHaveLength(0);
});
it("handles invalid JSON when populating suggestions", done => {
it("handles invalid JSON when populating suggestions", async () => {
jest.spyOn(console, "error").mockImplementation(() => {});
fetch.mockResponse("this is not JSON");
ShallowLabelNameInput(true);
// use timeout since mount will call fetch
setTimeout(() => {
expect(matcher.suggestions.names).toHaveLength(0);
done();
}, 100);
const tree = ShallowLabelNameInput(true);
const instance = tree.instance();
await expect(instance.nameSuggestionsFetch).resolves.toBeUndefined();
await expect(instance.valueSuggestionsFetch).resolves.toBeUndefined();
expect(matcher.suggestions.names).toHaveLength(0);
expect(matcher.suggestions.values).toHaveLength(0);
});
it("suggestions are emptied on failed fetch", done => {
it("suggestions are emptied on failed fetch", async () => {
fetch.mockReject(new Error("fake error message"));
ShallowLabelNameInput(true);
// use timeout since mount will call fetch
setTimeout(() => {
expect(matcher.suggestions.names).toHaveLength(0);
done();
}, 100);
const tree = ShallowLabelNameInput(true);
const instance = tree.instance();
await expect(instance.nameSuggestionsFetch).resolves.toBeUndefined();
await expect(instance.valueSuggestionsFetch).resolves.toBeUndefined();
expect(matcher.suggestions.names).toHaveLength(0);
});
it("doesn't fetch suggestions if value is changed to empty string", () => {