Skip to content

Commit f2f452e

Browse files
Add testing for Vue component VocabularySearch #298
1 parent c11984d commit f2f452e

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// @vitest-environment jsdom
2+
import { describe, it, expect, beforeEach } from "vitest"
3+
import { mount } from "@vue/test-utils"
4+
import VocabularySearch from "../../vue/components/VocabularySearch.vue"
5+
6+
const FormRowStub = {
7+
template: "<div><slot /></div>",
8+
}
9+
10+
function mountSearch(props = {}) {
11+
return mount(VocabularySearch, {
12+
props,
13+
global: {
14+
stubs: {
15+
FormRow: FormRowStub,
16+
},
17+
},
18+
})
19+
}
20+
21+
describe("VocabularySearch Component", () => {
22+
beforeEach(() => {
23+
// jsdom has a real window.location, but assigning href can be annoying in tests.
24+
// Here we mock only what the component needs.
25+
delete window.location
26+
window.location = { href: "" }
27+
})
28+
29+
it("shows the schemes count when available", () => {
30+
const wrapper = mountSearch({
31+
schemesCount: 144,
32+
})
33+
34+
expect(wrapper.text()).toContain("Search in metadata about")
35+
expect(wrapper.text()).toContain("144")
36+
expect(wrapper.text()).toContain("terminologies")
37+
})
38+
39+
it("does not show the schemes count when it is null or zero", () => {
40+
const withNull = mountSearch({
41+
schemesCount: null,
42+
})
43+
44+
expect(withNull.text()).not.toContain("Search in metadata about")
45+
46+
const withZero = mountSearch({
47+
schemesCount: 0,
48+
})
49+
50+
expect(withZero.text()).not.toContain("Search in metadata about")
51+
})
52+
53+
it("initializes search and selected field from query", () => {
54+
const wrapper = mountSearch({
55+
query: {
56+
search: "dfg",
57+
field: "title_search",
58+
},
59+
})
60+
61+
const input = wrapper.get("input")
62+
const select = wrapper.get("select")
63+
64+
expect(input.element.value).toBe("dfg")
65+
expect(select.element.value).toBe("title_search")
66+
})
67+
68+
it("uses allfields as default field", () => {
69+
const wrapper = mountSearch({
70+
query: {
71+
search: "dfg",
72+
},
73+
})
74+
75+
expect(wrapper.get("select").element.value).toBe("allfields")
76+
})
77+
78+
it("redirects to vocabularies search on submit with Title field", async () => {
79+
const wrapper = mountSearch({
80+
query: {
81+
search: "dfg",
82+
field: "title_search",
83+
},
84+
})
85+
86+
await wrapper.get("form").trigger("submit")
87+
88+
expect(window.location.href).toBe(
89+
"/vocabularies?search=dfg&field=title_search",
90+
)
91+
})
92+
93+
it("does not include empty search parameter", async () => {
94+
const wrapper = mountSearch({
95+
query: {
96+
search: "",
97+
field: "publisher_en",
98+
},
99+
})
100+
101+
await wrapper.get("form").trigger("submit")
102+
103+
expect(window.location.href).toBe(
104+
"/vocabularies?field=publisher_en",
105+
)
106+
})
107+
})

0 commit comments

Comments
 (0)