Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 454503e

Browse files
committed
test(text-area): unit test and e2e
1 parent 07ab065 commit 454503e

File tree

6 files changed

+427
-2
lines changed

6 files changed

+427
-2
lines changed

demos/vue-3/src/router/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ const routes: Array<RouteRecordRaw> = [
5252
component: () =>
5353
import(/* webpackChunkName: "text-fields" */ '../views/SelectFields.vue'),
5454
},
55+
{
56+
path: '/textarea-fields',
57+
name: 'TextArea Fields',
58+
meta: {
59+
title: 'TextArea Fields',
60+
},
61+
component: () =>
62+
import(
63+
/* webpackChunkName: "text-fields" */ '../views/TextAreaFields.vue'
64+
),
65+
},
5566
{
5667
path: '/login',
5768
name: 'Login',

demos/vue-3/src/views/Home.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export default defineComponent({
6161
route: '/select-fields',
6262
tags: ['select-field'],
6363
},
64+
{
65+
name: 'Textarea Fields',
66+
route: '/textarea-fields',
67+
tags: ['textarea-field'],
68+
},
6469
{
6570
name: 'Login',
6671
route: '/login',
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<template>
2+
<div class="page container">
3+
<div class="mx-auto w-full sm:w-2/3 relative mb-24">
4+
<div
5+
class="absolute inset-0 bg-gradient-to-r from-blue-400 to-green-200 shadow-xl transform -skew-y-3 sm:skew-y-0 sm:-rotate-3 sm:rounded-3xl"
6+
></div>
7+
<div class="relative card p-6 bg-white">
8+
<dynamic-form
9+
:form="form"
10+
@submitted="handleSubmit"
11+
@change="valueChanged"
12+
@error="handleError"
13+
/>
14+
<button
15+
data-cy="submit"
16+
class="btn bg-green-500 text-white hover:bg-green-700 mt-4"
17+
submit="true"
18+
:form="form?.id"
19+
>
20+
Submit
21+
</button>
22+
</div>
23+
</div>
24+
<div class="mx-auto w-full sm:w-2/3"><Console :content="formValues" /></div>
25+
</div>
26+
</template>
27+
28+
<script lang="ts">
29+
import {
30+
FormValidator,
31+
Validator,
32+
TextAreaField,
33+
required,
34+
FormOptions,
35+
minLength,
36+
maxLength,
37+
} from '@/';
38+
import { computed, defineComponent, reactive } from 'vue';
39+
import Console from '../components/Console.vue';
40+
41+
const components = {
42+
Console,
43+
};
44+
/* } from '../../dist/as-dynamic-forms.esm'; */
45+
export default defineComponent({
46+
name: 'TextAreaFieldsDemo',
47+
components,
48+
setup() {
49+
const formValues = reactive({});
50+
const minLengthValidator: FormValidator = Validator({
51+
validator: minLength(10),
52+
text: 'Text must be greater than 10 characters long',
53+
});
54+
55+
const form = computed(() => ({
56+
id: 'text-fields-demo',
57+
fields: {
58+
bio: TextAreaField({
59+
label: 'Bio',
60+
}),
61+
aboutMe: TextAreaField({
62+
label: 'About Me',
63+
validations: [
64+
Validator({ validator: required, text: 'This field is required' }),
65+
],
66+
}),
67+
interests: TextAreaField({
68+
label: 'Interests',
69+
col: 5,
70+
rows: 10,
71+
validations: [Validator(minLengthValidator)],
72+
}),
73+
disabled: TextAreaField({
74+
label: 'Disabled',
75+
value:
76+
'Arepa ipsum dolor amet risus pretium polar ni lava ni presta la batea ¿Vas a seguir Abigail? ron!',
77+
disabled: true,
78+
}),
79+
},
80+
}));
81+
82+
function handleSubmit(values) {
83+
console.log('Values Submitted', values);
84+
}
85+
86+
function valueChanged(values) {
87+
Object.assign(formValues, values);
88+
console.log('Values', values);
89+
}
90+
91+
function handleError(errors) {
92+
console.error('Errors', errors);
93+
}
94+
95+
return {
96+
form,
97+
formValues,
98+
handleSubmit,
99+
valueChanged,
100+
handleError,
101+
};
102+
},
103+
});
104+
</script>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { mount } from '@vue/test-utils';
2+
import TextAreaInput from './TextAreaInput.vue';
3+
import { FieldControl, TextAreaField, Validator, required } from '../../index';
4+
5+
describe('TextAreaInput', () => {
6+
let cmp;
7+
8+
beforeEach(() => {
9+
cmp = mount(TextAreaInput, {
10+
props: {
11+
control: FieldControl({
12+
name: 'test-input',
13+
...TextAreaField({
14+
label: 'Test Textarea',
15+
}),
16+
}),
17+
},
18+
});
19+
});
20+
21+
test('renders an textarea element', () => {
22+
const textarea = cmp.find('textarea');
23+
expect(textarea.exists()).toBe(true);
24+
});
25+
26+
test(`renders an textarea with class 'form-control'`, () => {
27+
const textarea = cmp.find('textarea');
28+
expect(textarea.classes()).toContain('form-control');
29+
});
30+
31+
test(`renders an textarea with id equal to field name`, () => {
32+
const textarea = cmp.find('textarea');
33+
expect(textarea.attributes('id')).toBe('test-input');
34+
});
35+
36+
test(`textarea gets disabled when form control does it`, async () => {
37+
await cmp.setProps({
38+
control: FieldControl({
39+
name: 'test-input',
40+
...TextAreaField({
41+
label: 'Test Textarea',
42+
disabled: true,
43+
}),
44+
}),
45+
});
46+
const textarea = cmp.find('textarea');
47+
expect(textarea.attributes('disabled')).toBe('');
48+
});
49+
50+
test(`renders a required textarea`, async () => {
51+
await cmp.setProps({
52+
control: FieldControl({
53+
name: 'test-input',
54+
...TextAreaField({
55+
label: 'Test Textarea',
56+
validations: [
57+
Validator({ validator: required, text: 'This field is required' }),
58+
],
59+
}),
60+
}),
61+
});
62+
const textarea = cmp.find('textarea');
63+
expect(textarea.attributes('required')).toBe('');
64+
});
65+
66+
test(`sets ariaRequired when required`, async () => {
67+
await cmp.setProps({
68+
control: FieldControl({
69+
name: 'test-input',
70+
...TextAreaField({
71+
label: 'Test Textarea',
72+
validations: [
73+
Validator({ validator: required, text: 'This field is required' }),
74+
],
75+
}),
76+
}),
77+
});
78+
const textarea = cmp.find('textarea');
79+
expect(textarea.attributes('ariarequired')).toBeTruthy;
80+
});
81+
82+
test(`renders a readonly textarea`, async () => {
83+
await cmp.setProps({
84+
control: FieldControl({
85+
name: 'test-input',
86+
...TextAreaField({
87+
label: 'Test Textarea',
88+
readonly: true,
89+
}),
90+
}),
91+
});
92+
const textarea = cmp.find('textarea');
93+
expect(textarea.attributes('readonly')).toBe('');
94+
});
95+
96+
test(`renders an textarea with aria labels`, async () => {
97+
await cmp.setProps({
98+
control: FieldControl({
99+
name: 'test-input',
100+
...TextAreaField({
101+
label: 'Test Textarea',
102+
ariaLabel: 'Im a test input',
103+
}),
104+
}),
105+
});
106+
const textarea = cmp.find('textarea');
107+
expect(textarea.attributes('arialabel')).toBe('Im a test input');
108+
});
109+
110+
test('emits an event when blur', async () => {
111+
const textarea = cmp.find('textarea');
112+
await textarea.trigger('blur');
113+
114+
expect(cmp.emitted()).toHaveProperty('blur');
115+
});
116+
117+
test('emits an event when focus', async () => {
118+
const textarea = cmp.find('textarea');
119+
await textarea.trigger('focus');
120+
121+
expect(cmp.emitted()).toHaveProperty('focus');
122+
});
123+
});

src/components/text-area-input/TextAreaInput.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export default defineComponent({
1616
inheritAttrs: false,
1717
props,
1818
setup(props, { emit }) {
19-
const { onInput, onChange, onFocus, onBlur } = useInputEvents(props, emit);
19+
const { onInput, onChange, onFocus, onBlur, getClasses } = useInputEvents(
20+
props,
21+
emit,
22+
);
2023
const {
2124
isRequired,
2225
errorMessages,
@@ -27,7 +30,7 @@ export default defineComponent({
2730
h('textarea', {
2831
id: props.control.name,
2932
name: props.control.name || '',
30-
class: ['form-control'],
33+
class: getClasses.value,
3134
value: props.control.value,
3235
rows: props.control.rows,
3336
cols: props.control.cols,

0 commit comments

Comments
 (0)