Skip to content

Commit 254164b

Browse files
Add example using props
Add example using props in reference to testing-library#1 in testing-library/vue-testing-library#216 (comment)
1 parent 90d3b24 commit 254164b

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

docs/vue-testing-library/examples.mdx

+84
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,90 @@ test('properly handles v-model', async () => {
9292
})
9393
```
9494

95+
## Example using `props` and [`stubs`](https://vue-test-utils.vuejs.org/api/options.html#stubs) for thrid party compoents like `bootstrap-vue`:
96+
97+
```html
98+
<template>
99+
<b-row class="mb-1">
100+
<b-col>
101+
<label class="form-label mb-md-1">{{ label }}</label>
102+
103+
<b-form-input
104+
type="text"
105+
list="clist"
106+
:disabled="isDisabled"
107+
:placeholder="placeholder"
108+
:value="value"
109+
@input="$emit('input', $event)"
110+
/>
111+
112+
<datalist v-if="isSearchable" id="clist">
113+
<option
114+
v-for="(option, index) in searchOptions"
115+
:key="index"
116+
:value="option"
117+
>
118+
{{ option.suggestion }}
119+
</option>
120+
</datalist>
121+
</b-col>
122+
</b-row>
123+
</template>
124+
125+
<script>
126+
export default {
127+
name: 'FormInput',
128+
props: {
129+
label: { type: String, default: 'Default label' },
130+
value: { type: String, default: '' },
131+
inputType: { type: String, default: 'text' },
132+
placeholder: { type: String, default: 'Default placeholder' },
133+
searchOptions: {
134+
type: Array,
135+
default() {
136+
return []
137+
},
138+
},
139+
isSearchable: { type: Boolean, default: false },
140+
},
141+
}
142+
</script>
143+
```
144+
145+
```js
146+
import { render } from '@testing-library/vue'
147+
import { BRow, BCol, BFormInput } from 'bootstrap-vue'
148+
import FormInput from './FormInput.vue'
149+
const stubs = {
150+
// used to register custom or third-party components
151+
'b-form-input': BFormInput,
152+
'b-row': BRow,
153+
'b-col': BCol,
154+
}
155+
156+
it('renders the correct default state', () => {
157+
const { getByText } = render(FormInput, {
158+
stubs,
159+
})
160+
161+
// Asserts initial state.
162+
getByText('Default label')
163+
})
164+
165+
it('renders the label prop', () => {
166+
const props = {
167+
label: 'The new label',
168+
}
169+
const { getByText } = render(FormInput, {
170+
stubs,
171+
props,
172+
})
173+
174+
getByText(props.label)
175+
})
176+
177+
```
178+
95179
## More examples
96180

97181
You'll find examples of testing with different libraries in

0 commit comments

Comments
 (0)