Skip to content

Commit

Permalink
Add field tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skaptox committed Dec 1, 2021
1 parent 36ec8ed commit 600747a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
32 changes: 31 additions & 1 deletion src/components/primitives/Field/Field.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { shallowMount } from '@vue/test-utils'
import { shallowMount, mount } from '@vue/test-utils'
import Field from './Field.vue'
import Input from '../Input/Input.vue'

let wrapper

Expand All @@ -15,4 +16,33 @@ describe('Field', () => {
it('render correctly', () => {
expect(wrapper.html()).toMatchSnapshot()
})

it('adds a help <p> element in the root div.field when "message" prop is passed', async () => {
wrapper = mount(Field, {
slots: {
default: [Input],
},
props: {
type: 'is-danger',
},
})

const message = 'This is a message'

expect(wrapper.findAll('p')).toEqual([])

wrapper.setProps({ message })
await wrapper.vm.$nextTick()
expect(wrapper.find('p').classes()).toEqual(['help', 'is-danger'])
expect(wrapper.find('p').text()).toBe(message)
})

it('adds a label element under the root div.field when "label" prop is passed', () => {
wrapper = shallowMount(Field, {
propsData: {
label: 'Some label',
},
})
expect(wrapper.find('label').text()).toBe('Some label')
})
})
4 changes: 2 additions & 2 deletions src/components/primitives/Field/Field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export default {
<label v-else-if="label" class="label" :for="labelFor">{{ label }}</label>
<div v-if="horizontal" class="field-body">
<div class="field" :class="{'is-grouped': grouped}">
<slot />
<slot :rounded="type" />
</div>
</div>
<slot v-else />
<slot :color="type" v-else />
<p v-if="message" class="help" :class="type">
{{ message }}
</p>
Expand Down
33 changes: 19 additions & 14 deletions src/components/primitives/Input/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ export default {
rounded: Boolean,
loading: Boolean,
expanded: Boolean,
modelValue: [String, Number]
modelValue: [String, Number],
},
emits: ['update:modelValue'],
setup(props, { emit, slots }) {
const hasLeftIcon = computed(() => Boolean(slots.leftIcon));
const hasRightIcon = computed(() => Boolean(slots.rightIcon));
setup(props, { emit, slots, attrs }) {
const hasLeftIcon = computed(() => Boolean(slots.leftIcon))
const hasRightIcon = computed(() => Boolean(slots.rightIcon))
const value = ref(props.modelValue)
watchEffect(() => {
value.value = props.modelValue
})
Expand All @@ -37,17 +38,21 @@ export default {
{
'is-loading': loading,
'has-icons-left': hasLeftIcon,
'has-icons-right': hasRightIcon
}
'has-icons-right': hasRightIcon,
},
]">
<input class="input" v-bind="$attrs" v-model="value" :class="[
color,
size,
{
'is-rounded': rounded,
'is-expanded': expanded
}
]" />
<input
class="input"
v-bind="$attrs"
v-model="value"
:class="[
color,
size,
{
'is-rounded': rounded,
'is-expanded': expanded,
},
]" />
<span class="icon is-left" v-if="hasLeftIcon">
<slot name="leftIcon" />
</span>
Expand Down

0 comments on commit 600747a

Please sign in to comment.