Skip to content

Commit 600747a

Browse files
committed
Add field tests
1 parent 36ec8ed commit 600747a

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

src/components/primitives/Field/Field.spec.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { shallowMount } from '@vue/test-utils'
1+
import { shallowMount, mount } from '@vue/test-utils'
22
import Field from './Field.vue'
3+
import Input from '../Input/Input.vue'
34

45
let wrapper
56

@@ -15,4 +16,33 @@ describe('Field', () => {
1516
it('render correctly', () => {
1617
expect(wrapper.html()).toMatchSnapshot()
1718
})
19+
20+
it('adds a help <p> element in the root div.field when "message" prop is passed', async () => {
21+
wrapper = mount(Field, {
22+
slots: {
23+
default: [Input],
24+
},
25+
props: {
26+
type: 'is-danger',
27+
},
28+
})
29+
30+
const message = 'This is a message'
31+
32+
expect(wrapper.findAll('p')).toEqual([])
33+
34+
wrapper.setProps({ message })
35+
await wrapper.vm.$nextTick()
36+
expect(wrapper.find('p').classes()).toEqual(['help', 'is-danger'])
37+
expect(wrapper.find('p').text()).toBe(message)
38+
})
39+
40+
it('adds a label element under the root div.field when "label" prop is passed', () => {
41+
wrapper = shallowMount(Field, {
42+
propsData: {
43+
label: 'Some label',
44+
},
45+
})
46+
expect(wrapper.find('label').text()).toBe('Some label')
47+
})
1848
})

src/components/primitives/Field/Field.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ export default {
3838
<label v-else-if="label" class="label" :for="labelFor">{{ label }}</label>
3939
<div v-if="horizontal" class="field-body">
4040
<div class="field" :class="{'is-grouped': grouped}">
41-
<slot />
41+
<slot :rounded="type" />
4242
</div>
4343
</div>
44-
<slot v-else />
44+
<slot :color="type" v-else />
4545
<p v-if="message" class="help" :class="type">
4646
{{ message }}
4747
</p>

src/components/primitives/Input/Input.vue

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ export default {
1010
rounded: Boolean,
1111
loading: Boolean,
1212
expanded: Boolean,
13-
modelValue: [String, Number]
13+
modelValue: [String, Number],
1414
},
1515
emits: ['update:modelValue'],
16-
setup(props, { emit, slots }) {
17-
const hasLeftIcon = computed(() => Boolean(slots.leftIcon));
18-
const hasRightIcon = computed(() => Boolean(slots.rightIcon));
16+
setup(props, { emit, slots, attrs }) {
17+
const hasLeftIcon = computed(() => Boolean(slots.leftIcon))
18+
const hasRightIcon = computed(() => Boolean(slots.rightIcon))
1919
const value = ref(props.modelValue)
2020
21+
2122
watchEffect(() => {
2223
value.value = props.modelValue
2324
})
@@ -37,17 +38,21 @@ export default {
3738
{
3839
'is-loading': loading,
3940
'has-icons-left': hasLeftIcon,
40-
'has-icons-right': hasRightIcon
41-
}
41+
'has-icons-right': hasRightIcon,
42+
},
4243
]">
43-
<input class="input" v-bind="$attrs" v-model="value" :class="[
44-
color,
45-
size,
46-
{
47-
'is-rounded': rounded,
48-
'is-expanded': expanded
49-
}
50-
]" />
44+
<input
45+
class="input"
46+
v-bind="$attrs"
47+
v-model="value"
48+
:class="[
49+
color,
50+
size,
51+
{
52+
'is-rounded': rounded,
53+
'is-expanded': expanded,
54+
},
55+
]" />
5156
<span class="icon is-left" v-if="hasLeftIcon">
5257
<slot name="leftIcon" />
5358
</span>

0 commit comments

Comments
 (0)