Skip to content

Commit

Permalink
fix(props) allow integer values in searchInput prop
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
darrenjennings authored Jul 17, 2018
2 parents df85dd2 + 6eff625 commit 23c5b52
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
51 changes: 48 additions & 3 deletions __tests__/autosuggest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe("Autosuggest", () => {
const input = wrapper.find("input");
input.trigger("click");
wrapper.setData({ searchInput: "G" });

input.trigger("keydown.up"); // Check it doesn't offset the selection by going up first when nothing is selected.

// TODO: test these keys are actually returning early.
Expand Down Expand Up @@ -385,7 +385,12 @@ describe("Autosuggest", () => {
props.suggestions = [
{
data: [
{ id: 1, name: "Frodo", avatar: "https://upload.wikimedia.org/wikipedia/en/4/4e/Elijah_Wood_as_Frodo_Baggins.png" }
{
id: 1,
name: "Frodo",
avatar:
"https://upload.wikimedia.org/wikipedia/en/4/4e/Elijah_Wood_as_Frodo_Baggins.png"
}
]
}
];
Expand All @@ -400,7 +405,7 @@ describe("Autosuggest", () => {
const input = wrapper.find("input");
input.trigger("click");
wrapper.setData({ searchInput: "F" });

input.trigger("keydown.down");
input.trigger("keydown.enter");

Expand All @@ -424,4 +429,44 @@ describe("Autosuggest", () => {
const input = wrapper.find("input");
expect(input.attributes()["name"]).toBe("my-input");
});

it("search input prop type handles string and integers only", async () => {
let props = Object.assign({}, defaultProps);

const mockConsole = jest.fn();
console.error = mockConsole;

const blurred = () => {};
props.inputProps.onBlur = blurred;

const wrapper = mount(Autosuggest, {
propsData: props
});

const input = wrapper.find("input");

// Integers
input.trigger("click");
wrapper.setData({ searchInput: 1 });
await wrapper.vm.$nextTick(() => {});
input.trigger("blur");

// Strings
input.trigger("click");
wrapper.setData({ searchInput: "Hello" });
await wrapper.vm.$nextTick(() => {});
input.trigger("blur");

// Should not throw any errors
expect(mockConsole).toHaveBeenCalledTimes(0);

// Functions
input.trigger("click");
wrapper.setData({ searchInput: () => { /* BAD */ } });
await wrapper.vm.$nextTick(() => {});
input.trigger("blur");

// Should throw validation error
expect(mockConsole).toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion src/parts/DefaultSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const DefaultSection = {
section: { type: Object, required: true },
currentIndex: { type: Number, required: false, default: Infinity },
updateCurrentIndex: { type: Function, required: true },
searchInput: { type: String, required: false, default: "" },
searchInput: { type: [String, Number], required: false, default: "" },
renderSuggestion: { type: Function, required: true },
normalizeItemFunction: { type: Function, required: true }
},
Expand Down

0 comments on commit 23c5b52

Please sign in to comment.