forked from ecamp/ecamp3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEParseField.spec.js
108 lines (89 loc) · 2.66 KB
/
EParseField.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import Vue from 'vue'
import Vuetify from 'vuetify'
import formBaseComponents from '@/plugins/formBaseComponents'
import EParseField from '@/components/form/base/EParseField.vue'
import { mount as mountComponent } from '@vue/test-utils'
import { screen } from '@testing-library/vue'
Vue.use(Vuetify)
Vue.use(formBaseComponents)
describe('An EParseField', () => {
let vuetify
const mount = (options) => {
const app = Vue.component('App', {
components: { EParseField },
data: function () {
return {
data: null,
}
},
methods: {
parse: (value) => {
return value === 'true' ? true : value === 'false' ? false : null
},
format: (value) => {
return value === null ? '' : `${value}`
},
},
template: `
<div data-app>
<e-parse-field label="test" :parse="parse" :format="format" v-model="data" :value="data">
${options?.children}
</e-parse-field>
</div>
`,
})
return mountComponent(app, { vuetify, attachTo: document.body, ...options })
}
beforeEach(() => {
vuetify = new Vuetify()
})
test('looks like a textfield', async () => {
const wrapper = mount()
expect(wrapper).toMatchSnapshot('empty')
await wrapper.setData({ data: true })
expect(wrapper).toMatchSnapshot('with text')
})
test('updates text when vModel changes', async () => {
const wrapper = mount()
const input = wrapper.find('input').element
expect(input.value).toBeDefined()
const firstValue = true
await wrapper.setData({ data: firstValue })
expect(input.value).toBe(`${firstValue}`)
const secondValue = false
await wrapper.setData({ data: secondValue })
expect(input.value).toBe(`${secondValue}`)
})
test('updates vModel when value of input field changes', async () => {
const wrapper = mount()
const input = wrapper.find('input')
const value = true
input.element.value = `${value}`
await input.trigger('input')
expect(wrapper.vm.data).toBe(value)
})
test.each([
['true', true],
['false', false],
['', null],
['s', null],
['0', null],
['1', null],
])('parses "%s" as "%s"', async (string, expected) => {
const wrapper = mount()
const input = wrapper.find('input')
input.element.value = `${string}`
await input.trigger('input')
expect(wrapper.vm.data).toBe(expected)
})
test('allows to use the append slot', async () => {
mount({
children: `
<template #append>
<span>append</span>
</template>
`,
})
expect(await screen.findByText('append')).toBeVisible()
})
})