Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add default placeholder for VSearchBar (#854)

sarayourfriendb305f3d0f5cc
diff --git a/src/components/VHeader/VSearchBar/VSearchBar.vue b/src/components/VHeader/VSearchBar/VSearchBar.vue
index a2c583a7b..cfddb6619 100644
--- a/src/components/VHeader/VSearchBar/VSearchBar.vue
+++ b/src/components/VHeader/VSearchBar/VSearchBar.vue
@@ -4,6 +4,7 @@
@submit.prevent="handleSearch"
>
<VInputField
+ :placeholder="placeholder || $t('hero.search.placeholder')"
v-bind="$attrs"
class="flex-grow search-field"
:class="{ 'border-transparent': isHomeRoute }"
@@ -52,6 +53,10 @@ const VSearchBar = defineComponent({
required: true,
validator: (v) => ['small', 'medium', 'large', 'standalone'].includes(v),
},
+ placeholder: {
+ type: String,
+ required: false,
+ },
},
emits: ['input', 'submit'],
setup(props, { emit }) {
diff --git a/src/pages/index.vue b/src/pages/index.vue
index bb3b74c0e..a5f7b0522 100644
--- a/src/pages/index.vue
+++ b/src/pages/index.vue
@@ -45,7 +45,6 @@
v-model.trim="searchTerm"
class="max-w-[40rem] mt-4 lg:mt-8 group"
size="standalone"
- :placeholder="$t('hero.search.placeholder')"
@submit="handleSearch"
>
<ClientOnly>
diff --git a/test/unit/specs/components/VHeader/SearchBar/search-bar.spec.js b/test/unit/specs/components/VHeader/SearchBar/search-bar.spec.js
index a7cc09bfa..4434647cc 100644
--- a/test/unit/specs/components/VHeader/SearchBar/search-bar.spec.js
+++ b/test/unit/specs/components/VHeader/SearchBar/search-bar.spec.js
@@ -7,12 +7,13 @@ jest.mock('~/composables/use-match-routes', () => ({
}))
const sizes = ['small', 'medium', 'large', 'standalone']
+const defaultPlaceholder = 'Enter search query'
describe('SearchBar', () => {
let options
beforeEach(() => {
options = {
- attrs: { placeholder: 'Enter search query' },
+ props: { placeholder: defaultPlaceholder, size: 'standalone' },
}
})
@@ -20,14 +21,14 @@ describe('SearchBar', () => {
'renders an input field with placeholder and type="search" (%s size)',
(size) => {
useMatchHomeRoute.mockImplementation(() => false)
- options.props = { size: size }
+ options.props.size = size
render(SearchBar, options)
- const inputElement = screen.getByPlaceholderText('Enter search query')
+ const inputElement = screen.getByPlaceholderText(defaultPlaceholder)
expect(inputElement.tagName).toBe('INPUT')
expect(inputElement).toHaveAttribute('type', 'search')
- expect(inputElement).toHaveAttribute('placeholder', 'Enter search query')
+ expect(inputElement).toHaveAttribute('placeholder', defaultPlaceholder)
}
)
@@ -35,7 +36,7 @@ describe('SearchBar', () => {
'renders a button with type="submit", ARIA label and SR text (%s size)',
(size) => {
useMatchHomeRoute.mockImplementation(() => false)
- options.props = { size: size }
+ options.props.size = size
render(SearchBar, options)
const btnElement = screen.getByLabelText('search.search')
@@ -45,4 +46,21 @@ describe('SearchBar', () => {
expect(btnElement).toHaveAttribute('aria-label', 'search.search')
}
)
+
+ describe('placeholder', () => {
+ it('should default to hero.search.placeholder', () => {
+ delete options.props.placeholder
+ render(SearchBar, options)
+ expect(
+ screen.queryByPlaceholderText('hero.search.placeholder')
+ ).not.toBeNull()
+ })
+
+ it('should use the prop when provided', () => {
+ const placeholder = 'This is a different placeholder from the default'
+ options.props.placeholder = placeholder
+ render(SearchBar, options)
+ expect(screen.queryByPlaceholderText(placeholder)).not.toBeNull()
+ })
+ })
})