Skip to content

Commit 3893119

Browse files
authored
Merge pull request #235 from c4dt/110
add unit tests for search bar and related clear button component
2 parents 5d5b5c3 + dd84b6c commit 3893119

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

test/nuxt/clearButton.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { test, expect, vi } from "vitest";
2+
import { mountSuspended } from "@nuxt/test-utils/runtime";
3+
import ClearButton from "@/components/ClearButton.vue";
4+
5+
test("renders button", async () => {
6+
const wrapper = await mountSuspended(ClearButton, {
7+
props: {
8+
clearFunc: () => void 0
9+
}
10+
});
11+
expect(wrapper.find("svg[data-prefix='fas'][data-icon='xmark']").exists()).toBe(true);
12+
expect(wrapper.find("button").attributes("aria-label")).toBe("Clear all selections");
13+
});
14+
15+
test("calls clear function when button is clicked", async () => {
16+
const clearFunc = vi.fn();
17+
const wrapper = await mountSuspended(ClearButton, {
18+
props: {
19+
clearFunc: clearFunc
20+
}
21+
});
22+
await wrapper.find("button").trigger("click");
23+
expect(clearFunc).toHaveBeenCalled();
24+
});

test/nuxt/searchBar.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)