Skip to content

Commit c945e4c

Browse files
committed
added schema support
1 parent 529a841 commit c945e4c

File tree

4 files changed

+80
-9
lines changed

4 files changed

+80
-9
lines changed

src/components/FormlyField.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
2-
<component :is="schema.type" :model="model"></component>
2+
<component :is="field.type" :model="model"></component>
33
</template>
44

55
<script>
66
export default {
7-
props: ['schema', 'model']
7+
props: ['field', 'model']
88
}
99
</script>

src/components/FormlyForm.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<template>
22
<fieldset>
3-
3+
<formly-field v-for="field in fields" :field="field" :model.sync="model[field.key]" ></formly-field>
44
</fieldset>
55
</template>
66

77
<script>
88
export default{
9-
9+
props: ['fields', 'model']
1010
}
1111
</script>

test/unit/specs/FormlyField.spec.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const expect = chai.expect;
33
import sinonChai from 'sinon-chai';
44
import sinon from 'sinon';
55
import Vue from 'vue';
6-
import VueFormly from 'src/index';
7-
Vue.use(VueFormly);
6+
import FormlyField from 'src/components/FormlyField.vue';
87
chai.use(sinonChai);
98

109
let el, vm;
@@ -14,7 +13,10 @@ function createForm(template, data){
1413
el.innerHTML = template;
1514
vm = new Vue({
1615
el: el,
17-
data: data
16+
data: data,
17+
components: {
18+
'formly-field': FormlyField
19+
}
1820
});
1921

2022
return [el, vm];
@@ -34,7 +36,7 @@ describe('FormlyField', () => {
3436
}
3537
};
3638

37-
createForm('<formly-field :schema="schema"></formly-field>', data);
39+
createForm('<formly-field :field="schema"></formly-field>', data);
3840

3941
let innerElem = vm.$el.querySelector('#testComponent');
4042

@@ -56,7 +58,7 @@ describe('FormlyField', () => {
5658
testModel: 'foo'
5759
};
5860

59-
createForm('<formly-field :schema="schema" :model.sync="testModel"></formly-field>', data);
61+
createForm('<formly-field :field="schema" :model.sync="testModel"></formly-field>', data);
6062

6163
let input = vm.$el.querySelector('#testInput');
6264

test/unit/specs/FormlyForm.spec.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import chai from 'chai';
2+
const expect = chai.expect;
3+
import sinonChai from 'sinon-chai';
4+
import sinon from 'sinon';
5+
import Vue from 'vue';
6+
import FormlyForm from 'src/components/FormlyForm.vue';
7+
chai.use(sinonChai);
8+
9+
//mock our formly-field component
10+
let FormlyField = Vue.extend({
11+
template: '<div class="formly-field"><pre id="{{field.key}}_field">{{field | json}}</pre><pre id="{{field.key}}_model">{{model | json}}</pre></div>',
12+
props: ['field', 'model']
13+
});
14+
15+
let el, vm;
16+
17+
function createForm(template, data){
18+
el = document.createElement('div');
19+
el.innerHTML = template;
20+
vm = new Vue({
21+
el: el,
22+
data: data,
23+
components: {
24+
'formly-form': FormlyForm
25+
}
26+
});
27+
28+
return [el, vm];
29+
}
30+
31+
describe('FormlyForm', () => {
32+
33+
beforeEach( () => {
34+
//ensure that our mocked component is there
35+
Vue.component('formly-field', FormlyField);
36+
});
37+
38+
it('should create a subset of components with the right data', () => {
39+
40+
let data = {
41+
schema: [
42+
{
43+
key: 'fname',
44+
type: 'input'
45+
},
46+
{
47+
key: 'lname',
48+
type: 'input'
49+
}
50+
],
51+
model: {
52+
fname: '',
53+
lname: 'smith'
54+
}
55+
};
56+
57+
createForm('<formly-form :fields="schema" :model="model"></formly-form>', data);
58+
59+
//check the elements have been created
60+
expect(vm.$el.querySelectorAll('fieldset')).to.be.length(1);
61+
expect(vm.$el.querySelectorAll('.formly-field')).to.be.length(2);
62+
63+
//check their data
64+
expect(vm.$el.querySelector('#lname_model').textContent).to.contain('smith');
65+
expect(JSON.parse(vm.$el.querySelector('#lname_field').textContent)).to.deep.equal(data.schema[1]);
66+
67+
});
68+
69+
});

0 commit comments

Comments
 (0)