Add test spec for components/ui/autocomplete.vue

This commit is contained in:
Andrew Bastin
2020-08-29 00:25:37 -04:00
parent 31cefd7dd0
commit 53684d6fbb

View File

@@ -0,0 +1,305 @@
import autocomplete from "../autocomplete"
import { mount } from "@vue/test-utils"
const props = {
placeholder: "",
value: "",
spellcheck: false,
source: ["app", "apple", "appliance", "brian", "bob", "alice"],
}
const factory = (props) =>
mount(autocomplete, {
propsData: props,
})
describe("autocomplete", () => {
test("mounts properly", () => {
const wrapper = factory(props)
expect(wrapper).toBeTruthy()
})
test("emits input event on text update [v-model compat]", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("testval")
await wrapper.vm.$nextTick()
expect(wrapper.emitted("input")).toBeTruthy()
expect(wrapper.emitted("input").length).toEqual(1)
})
test("shows matching suggestions", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("a")
await wrapper.vm.$nextTick()
const suggestions = wrapper.findAll("li").wrappers.map((el) => el.text())
expect(suggestions).toContain("pp")
expect(suggestions).toContain("pple")
expect(suggestions).toContain("ppliance")
expect(suggestions).toContain("lice")
})
test("doesnt show non-matching suggestions", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("b")
await wrapper.vm.$nextTick()
const suggestions = wrapper.findAll("li").wrappers.map((el) => el.text())
expect(suggestions).not.toContain("pp")
expect(suggestions).not.toContain("pple")
expect(suggestions).not.toContain("ppliance")
expect(suggestions).not.toContain("lice")
})
test("updates suggestions on input", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("b")
await wrapper.vm.$nextTick()
const suggestions = wrapper.findAll("li").wrappers.map((el) => el.text())
expect(suggestions).not.toContain("pp")
expect(suggestions).not.toContain("pple")
expect(suggestions).not.toContain("ppliance")
expect(suggestions).not.toContain("lice")
})
test("applies suggestion on clicking", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("b")
await wrapper.vm.$nextTick()
const selectedSuggestion = wrapper.findAll("li").at(0)
const selectedText = selectedSuggestion.text()
await selectedSuggestion.trigger("click")
await wrapper.vm.$nextTick()
expect(input.element.value).toEqual("b" + selectedText)
})
test("hide selection on pressing ESC", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("b")
await wrapper.vm.$nextTick()
await input.trigger("keyup", { code: "Escape" })
await wrapper.vm.$nextTick()
expect(wrapper.find("ul").exists()).toEqual(false)
})
test("pressing up when nothing is selected selects the first in the list", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("a")
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowUp",
})
await wrapper.vm.$nextTick()
expect(wrapper.findAll("li").at(0).element.classList.contains("active")).toEqual(true)
})
test("pressing down arrow when nothing is selected selects the first in the list", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("a")
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
expect(wrapper.findAll("li").at(0).element.classList.contains("active")).toEqual(true)
})
test("pressing down arrow moves down the selection list", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("a")
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
expect(wrapper.findAll("li").at(1).element.classList.contains("active")).toEqual(true)
})
test("pressing up arrow moves up the selection list", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("a")
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowUp",
})
await wrapper.vm.$nextTick()
expect(wrapper.findAll("li").at(0).element.classList.contains("active")).toEqual(true)
})
test("pressing down arrow at the end of the list doesn't update the selection", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("b")
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
expect(wrapper.findAll("li").at(1).element.classList.contains("active")).toEqual(true)
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
expect(wrapper.findAll("li").at(1).element.classList.contains("active")).toEqual(true)
})
test("pressing up arrow at the top of the list doesn't update the selection", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("b")
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowUp",
})
await wrapper.vm.$nextTick()
expect(wrapper.findAll("li").at(0).element.classList.contains("active")).toEqual(true)
await input.trigger("keydown", {
code: "ArrowUp",
})
await wrapper.vm.$nextTick()
expect(wrapper.findAll("li").at(0).element.classList.contains("active")).toEqual(true)
})
test("pressing tab performs the current completion", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("a")
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
const selectedSuggestion = wrapper.find("li.active").text()
await input.trigger("keydown", {
code: "Tab",
})
await wrapper.vm.$nextTick()
expect(input.element.value).toEqual("a" + selectedSuggestion)
})
test("pressing tab when nothing is selected selects the first suggestion", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("a")
await wrapper.vm.$nextTick()
const firstSuggestionText = wrapper.findAll("li").at(0).text()
await input.trigger("keydown", {
code: "Tab",
})
await wrapper.vm.$nextTick()
expect(input.element.value).toEqual("a" + firstSuggestionText)
})
test("pressing any non-special key doesn't do anything", async () => {
const wrapper = factory(props)
const input = wrapper.find("input")
await input.setValue("a")
await wrapper.vm.$nextTick()
await input.trigger("keydown", {
code: "ArrowDown",
})
await wrapper.vm.$nextTick()
const selectedSuggestion = wrapper.find("li.active").text()
await input.trigger("keydown", {
code: "Tab",
})
await wrapper.vm.$nextTick()
expect(input.element.value).toEqual("a" + selectedSuggestion)
})
})