-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.spec.js
149 lines (116 loc) · 3.64 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {expect} from 'chai';
import Vue from 'vue';
import VueFormly from 'src/index';
import Util, {addType, getTypes, setError, addValidationMessage, parseValidationString, set} from 'src/util';
describe('module', () => {
it('module props', () => {
expect(VueFormly).to.exist;
expect(VueFormly.install).to.be.a('function');
expect(VueFormly.addType).to.be.a('function');
expect(VueFormly.getTypes).to.be.a('function');
expect(VueFormly.addValidationMessage).to.be.a('function');
expect(addType).to.be.a('function');
expect(getTypes).to.be.a('function');
expect(setError).to.be.a('function');
expect(Util.formlyFields).to.be.a('object');
});
it('addType()', () => {
//mock vue
window.Vue = {
component(){},
filter(){}
};
VueFormly.install(Vue);
let newField = {
foo: 'bar'
};
addType('test', newField);
expect(getTypes().formly_test).to.deep.equal(newField);
});
it('setError()',()=>{
let form = {
$errors: {}
};
setError(form, 'fname', 'required', true);
expect(form.$errors.fname.required).to.be.true;
setError(form, 'fname', 'required', false);
expect(form.$errors.fname.required).to.be.false;
setError(form, 'fname', 'required', true, 'Hey there');
expect(form.$errors.fname.required).to.equal('Hey there');
setError(form, 'fname', 'required', false, 'Hey there');
expect(form.$errors.fname.required).to.be.false;
});
it('addValidationMessage()', () => {
addValidationMessage('test', 'testing');
expect(Util.validationMessages.test).to.equal('testing');
});
it('parseValidationString()', () => {
addValidationMessage('parseTest', '%l just %v testing');
let message = '%l just %v testing';
let expected = 'label just value testing';
let output = parseValidationString('parseTest', false, 'label', 'value');
expect(output).to.equal(expected);
output = parseValidationString(false, message, 'label', 'value');
expect(output).to.equal(expected);
output = parseValidationString('blahblahblah', false, '', '');
expect(output).to.be.false;
});
describe('set()', () => {
it('should be present on the Vue instance', () => {
const test = new Vue();
expect(test.$formlySet).to.be.a('function');
});
it('should take a nested field', () => {
const test = new Vue({
data: {
deeply: {
nested: {
child: 'foo'
}
}
}
});
expect(test.deeply.nested.child).to.equal('foo');
test.$formlySet(test.deeply, 'nested.child', 'bar');
expect(test.deeply.nested.child).to.equal('bar');
});
it('should set dotted properties', () => {
const test = new Vue({
data: {
deeply: {
'nested.child': 'foo'
}
}
});
test.$formlySet(test.deeply, 'nested.child', 'bar');
expect(test.deeply['nested.child']).to.equal('bar');
});
});
describe("Directives", () => {
it('formly-atts', () => {
let atts = {
placeholder: 'testing',
foo: 'bar'
};
let el = document.createElement('div');
let vm = new Vue({
data: {
atts: atts
},
template: '<div v-formly-atts="atts"></div>'
}).$mount(el);
expect(vm.$el.getAttribute('placeholder')).to.equal(atts.placeholder);
expect(vm.$el.getAttribute('foo')).to.equal(atts.foo);
});
it('formly-input-type', () => {
let el = document.createElement('div');
let vm = new Vue({
data: {
type: 'email'
},
template: '<input type="text" v-formly-input-type="type">'
}).$mount(el);
expect(vm.$el.getAttribute('type')).to.equal('email');
});
});
});