|
| 1 | +import { describe, test, expect, vi, afterEach } from "vitest"; |
| 2 | +import { mountSuspended, mockNuxtImport } from "@nuxt/test-utils/runtime"; |
| 3 | +import SearchBar from "@/components/SearchBar.vue"; |
| 4 | + |
| 5 | +describe("search bar toggle", () => { |
| 6 | + test("renders button", async () => { |
| 7 | + const wrapper = await mountSuspended(SearchBar); |
| 8 | + expect(wrapper.find("svg[class~=fa-magnifying-glass]").exists()).toBe(true); |
| 9 | + expect(wrapper.find("button").attributes("aria-label")).toBe("Toggle search bar open/closed"); |
| 10 | + }); |
| 11 | + |
| 12 | + // test("calls toggleAndFocus when the button is clicked", async () => { |
| 13 | + // const wrapper = await mountSuspended(SearchBar); |
| 14 | + // const toggleAndFocus = vi.spyOn(wrapper.vm, "toggleAndFocus")); |
| 15 | + // await wrapper.find("button").trigger("click"); |
| 16 | + // // https://github.com/nuxt/test-utils/issues/620 |
| 17 | + // expect(toggleAndFocus).toHaveBeenCalled(); |
| 18 | + // }); |
| 19 | +}); |
| 20 | + |
| 21 | +describe("input", () => { |
| 22 | + mockNuxtImport("navigateTo", () => vi.fn()); // don't navigate |
| 23 | + mockNuxtImport("useRoute", () => vi.fn()); // fix route |
| 24 | + const path = "/foo/bar"; |
| 25 | + useRoute.mockReturnValue({ path: path }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + vi.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + test("renders input box", async () => { |
| 32 | + const wrapper = await mountSuspended(SearchBar); |
| 33 | + expect(wrapper.find("input").attributes("aria-label")).toBe("Search"); |
| 34 | + }); |
| 35 | + |
| 36 | + test("tries to navigate to home page w/ search query when input is updated", async () => { |
| 37 | + const wrapper = await mountSuspended(SearchBar); |
| 38 | + const text = "toto"; |
| 39 | + await wrapper.find("input").setValue(text); |
| 40 | + expect(navigateTo).toHaveBeenCalledWith(`/?search=${text}`); |
| 41 | + }); |
| 42 | + |
| 43 | + test("resets query when input is cleared", async () => { |
| 44 | + const wrapper = await mountSuspended(SearchBar); |
| 45 | + const input = wrapper.find("input"); |
| 46 | + await input.setValue("toto"); |
| 47 | + navigateTo.mockClear(); |
| 48 | + await input.setValue(""); |
| 49 | + expect(navigateTo).toHaveBeenCalledWith(path, { query: {}, replace: true }); |
| 50 | + }); |
| 51 | + |
| 52 | + test("handles special characters in input", async () => { |
| 53 | + const wrapper = await mountSuspended(SearchBar); |
| 54 | + // cf. https://www.rfc-editor.org/rfc/rfc3986#section-2 |
| 55 | + await wrapper.find("input").setValue("-._~:/?#[]@!$&'()*+"); |
| 56 | + expect(navigateTo).toHaveBeenCalledWith("/?search=-._~%3A%2F%3F%23%5B%5D%40!%24%26'()*%2B"); |
| 57 | + }); |
| 58 | +}); |
0 commit comments