mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[Improvement] Contact: add test cases (#1195)
This commit is contained in:
parent
834f44e3d8
commit
f8ccebf085
84
packages/contact-card/test/index.spec.js
Normal file
84
packages/contact-card/test/index.spec.js
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import ContactEdit from '../../contact-edit';
|
||||||
|
import { mount } from '@vue/test-utils';
|
||||||
|
|
||||||
|
const contactInfo = {
|
||||||
|
name: 'test',
|
||||||
|
tel: '123123213'
|
||||||
|
};
|
||||||
|
|
||||||
|
const createComponent = () => {
|
||||||
|
const wrapper = mount(ContactEdit, {
|
||||||
|
propsData: {
|
||||||
|
contactInfo
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const button = wrapper.find('.van-button');
|
||||||
|
const field = wrapper.findAll('.van-field__control');
|
||||||
|
const { errorInfo, data } = wrapper.vm;
|
||||||
|
return {
|
||||||
|
wrapper,
|
||||||
|
data,
|
||||||
|
field,
|
||||||
|
button,
|
||||||
|
errorInfo
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
test('valid name', () => {
|
||||||
|
const { data, field, button, errorInfo } = createComponent();
|
||||||
|
|
||||||
|
// name empty
|
||||||
|
data.name = '';
|
||||||
|
button.trigger('click');
|
||||||
|
expect(errorInfo.name).toBeTruthy();
|
||||||
|
field.at(0).trigger('focus');
|
||||||
|
expect(errorInfo.name).toBeFalsy();
|
||||||
|
|
||||||
|
// name too long
|
||||||
|
data.name = '1'.repeat(30);
|
||||||
|
button.trigger('click');
|
||||||
|
expect(errorInfo.name).toBeTruthy();
|
||||||
|
field.at(0).trigger('focus');
|
||||||
|
expect(errorInfo.name).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('valid tel', () => {
|
||||||
|
const { data, field, button, errorInfo, wrapper } = createComponent();
|
||||||
|
data.tel = '';
|
||||||
|
button.trigger('click');
|
||||||
|
expect(errorInfo.tel).toBeTruthy();
|
||||||
|
field.at(1).trigger('focus');
|
||||||
|
expect(errorInfo.tel).toBeFalsy();
|
||||||
|
|
||||||
|
data.tel = '13000000000';
|
||||||
|
button.trigger('click');
|
||||||
|
expect(errorInfo.tel).toBeFalsy();
|
||||||
|
expect(wrapper.emitted('save')[0][0]).toEqual({
|
||||||
|
name: 'test',
|
||||||
|
tel: '13000000000'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('watch contact info', () => {
|
||||||
|
const wrapper = mount(ContactEdit);
|
||||||
|
wrapper.setProps({ contactInfo: { name: '123' }});
|
||||||
|
expect(wrapper.vm.data.name).toEqual('123');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('delete contact', done => {
|
||||||
|
const wrapper = mount(ContactEdit, {
|
||||||
|
propsData: {
|
||||||
|
isEdit: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const deleteButton = wrapper.findAll('.van-button').at(1);
|
||||||
|
deleteButton.trigger('click');
|
||||||
|
document.querySelector('.van-dialog__confirm').click();
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
expect(wrapper.emitted('delete')).toBeTruthy();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
@ -33,6 +33,12 @@ import Toast from '../toast';
|
|||||||
import validateMobile from '../utils/validate/mobile';
|
import validateMobile from '../utils/validate/mobile';
|
||||||
import create from '../utils/create';
|
import create from '../utils/create';
|
||||||
|
|
||||||
|
const defaultContact = {
|
||||||
|
id: '',
|
||||||
|
tel: '',
|
||||||
|
name: ''
|
||||||
|
};
|
||||||
|
|
||||||
export default create({
|
export default create({
|
||||||
name: 'contact-edit',
|
name: 'contact-edit',
|
||||||
|
|
||||||
@ -47,11 +53,7 @@ export default create({
|
|||||||
isDeleting: Boolean,
|
isDeleting: Boolean,
|
||||||
contactInfo: {
|
contactInfo: {
|
||||||
type: Object,
|
type: Object,
|
||||||
default: () => ({
|
default: () => ({ ...defaultContact })
|
||||||
id: '',
|
|
||||||
tel: '',
|
|
||||||
name: ''
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
telValidator: {
|
telValidator: {
|
||||||
type: Function,
|
type: Function,
|
||||||
@ -61,7 +63,10 @@ export default create({
|
|||||||
|
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
data: this.contactInfo,
|
data: {
|
||||||
|
...this.defaultContact,
|
||||||
|
...this.contactInfo
|
||||||
|
},
|
||||||
errorInfo: {
|
errorInfo: {
|
||||||
name: false,
|
name: false,
|
||||||
tel: false
|
tel: false
|
||||||
@ -106,13 +111,11 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onDelete() {
|
onDelete() {
|
||||||
if (!this.isDeleting) {
|
Dialog.confirm({
|
||||||
Dialog.confirm({
|
message: this.$t('confirmDelete')
|
||||||
message: this.$t('confirmDelete')
|
}).then(() => {
|
||||||
}).then(() => {
|
this.$emit('delete', this.data);
|
||||||
this.$emit('delete', this.data);
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user