element/test/unit/specs/input.spec.js

201 lines
4.6 KiB
Vue
Raw Normal View History

2016-11-03 02:14:36 +00:00
import { createVue, destroyVM } from '../util';
2016-10-18 10:09:19 +00:00
describe('Input', () => {
2016-11-03 02:14:36 +00:00
let vm;
afterEach(() => {
destroyVM(vm);
});
2016-10-18 10:09:19 +00:00
it('create', () => {
2016-11-03 02:14:36 +00:00
vm = createVue({
2016-10-18 10:09:19 +00:00
template: `
<el-input
2016-10-21 08:04:03 +00:00
:minlength="3"
:maxlength="5"
2016-10-18 10:09:19 +00:00
placeholder="请输入内容"
2016-10-20 10:51:18 +00:00
@focus="handleFocus"
2016-10-18 10:09:19 +00:00
value="input">
</el-input>
2016-10-20 10:51:18 +00:00
`,
data() {
return {
inputFocus: false
};
},
methods: {
handleFocus() {
this.inputFocus = true;
}
}
2016-10-18 10:09:19 +00:00
}, true);
let inputElm = vm.$el.querySelector('input');
2016-10-20 10:51:18 +00:00
inputElm.focus();
expect(vm.inputFocus).to.be.true;
2016-10-18 10:09:19 +00:00
expect(inputElm.getAttribute('placeholder')).to.equal('请输入内容');
expect(inputElm.value).to.equal('input');
expect(inputElm.getAttribute('minlength')).to.equal('3');
expect(inputElm.getAttribute('maxlength')).to.equal('5');
});
it('disabled', () => {
2016-11-03 02:14:36 +00:00
vm = createVue({
2016-10-18 10:09:19 +00:00
template: `
<el-input disabled>
</el-input>
`
}, true);
expect(vm.$el.querySelector('input').getAttribute('disabled')).to.ok;
});
it('icon', () => {
2016-11-03 02:14:36 +00:00
vm = createVue({
2016-10-18 10:09:19 +00:00
template: `
2016-10-20 10:51:18 +00:00
<el-input
icon="time"
@click="handleIconClick"
>
2016-10-18 10:09:19 +00:00
</el-input>
2016-10-20 10:51:18 +00:00
`,
data() {
return {
iconClicked: false
};
},
methods: {
handleIconClick(ev) {
this.iconClicked = true;
}
}
2016-10-18 10:09:19 +00:00
}, true);
2016-10-20 10:51:18 +00:00
var icon = vm.$el.querySelector('.el-input__icon');
icon.click();
expect(icon.classList.contains('el-icon-time')).to.true;
expect(vm.iconClicked).to.true;
2016-10-18 10:09:19 +00:00
});
it('size', () => {
2016-11-03 02:14:36 +00:00
vm = createVue({
2016-10-18 10:09:19 +00:00
template: `
<el-input size="large">
</el-input>
`
}, true);
2016-10-25 03:33:39 +00:00
expect(vm.$el.classList.contains('el-input--large')).to.true;
2016-10-18 10:09:19 +00:00
});
it('type', () => {
2016-11-03 02:14:36 +00:00
vm = createVue({
2016-10-18 10:09:19 +00:00
template: `
<el-input type="textarea">
</el-input>
`
}, true);
expect(vm.$el.classList.contains('el-textarea')).to.true;
});
it('rows', () => {
2016-11-03 02:14:36 +00:00
vm = createVue({
2016-10-18 10:09:19 +00:00
template: `
<el-input type="textarea" :rows="3">
</el-input>
`
}, true);
expect(vm.$el.querySelector('.el-textarea__inner').getAttribute('rows')).to.be.equal('3');
});
2016-10-18 10:09:19 +00:00
it('autosize', done => {
2016-11-03 02:14:36 +00:00
vm = createVue({
2016-10-18 10:09:19 +00:00
template: `
2016-10-20 10:51:18 +00:00
<div>
<el-input
ref="limitSize"
type="textarea"
:autosize="{minRows: 3, maxRows: 5}"
v-model="textareaValue"
>
</el-input>
<el-input
ref="limitlessSize"
type="textarea"
autosize
v-model="textareaValue"
>
</el-input>
</div>
2016-10-18 10:09:19 +00:00
`,
data() {
return {
textareaValue: 'sda\ndasd\nddasdsda\ndasd\nddasdsda\ndasd\nddasdsda\ndasd\nddasd'
};
}
2016-10-20 10:51:18 +00:00
}, true);
2016-10-18 10:09:19 +00:00
2016-10-20 10:51:18 +00:00
var limitSizeInput = vm.$refs.limitSize;
var limitlessSizeInput = vm.$refs.limitlessSize;
expect(limitSizeInput.textareaStyle.height).to.be.equal('117px');
expect(limitlessSizeInput.textareaStyle.height).to.be.equal('201px');
2016-10-18 10:09:19 +00:00
2016-10-20 10:51:18 +00:00
vm.textareaValue = '';
2016-10-18 10:09:19 +00:00
setTimeout(_ => {
2016-10-20 10:51:18 +00:00
expect(limitSizeInput.textareaStyle.height).to.be.equal('75px');
expect(limitlessSizeInput.textareaStyle.height).to.be.equal('33px');
2016-10-18 10:09:19 +00:00
done();
}, 200);
});
describe('Input Events', () => {
it('event:focus & blur', done => {
vm = createVue({
template: `
<el-input
ref="input"
placeholder="请输入内容"
value="input">
</el-input>
`
}, true);
const spyFocus = sinon.spy();
const spyBlur = sinon.spy();
vm.$refs.input.$on('focus', spyFocus);
vm.$refs.input.$on('blur', spyBlur);
vm.$el.querySelector('input').focus();
vm.$el.querySelector('input').blur();
vm.$nextTick(_ => {
expect(spyFocus.calledOnce).to.be.true;
expect(spyBlur.calledOnce).to.be.true;
done();
});
});
it('event:change', done => {
vm = createVue({
template: `
<el-input
ref="input"
placeholder="请输入内容"
:value="input">
</el-input>
`,
data() {
return {
input: 'a'
};
}
}, true);
const spy = sinon.spy();
vm.$refs.input.$on('change', spy);
vm.input = 'b';
vm.$nextTick(_ => {
expect(spy.withArgs('b').calledOnce).to.be.true;
done();
});
});
});
2016-10-18 10:09:19 +00:00
});