test(ContactEdit): update test cases

This commit is contained in:
chenjiahan 2020-12-15 19:45:45 +08:00
parent 7a2a9eb36d
commit 7bae2ebaba
3 changed files with 104 additions and 77 deletions

View File

@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`should validate contact name before submitting form 1`] = `
<div class="van-field__error-message">
请输入正确的姓名
</div>
`;
exports[`should validate contact tel before submitting form 1`] = `
<div class="van-field__error-message">
请输入正确的手机号
</div>
`;

View File

@ -1,77 +0,0 @@
import ContactEdit from '..';
import { mount, later } from '../../../test';
const contactInfo = {
name: 'test',
tel: '123123213',
};
const createComponent = () => {
const wrapper = mount(ContactEdit, {
props: {
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('should validate contact name before submit form', () => {
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();
});
test('should validate contact tel before submit form', () => {
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('should watch contact info', () => {
const wrapper = mount(ContactEdit);
wrapper.setProps({ contactInfo: { name: '123' } });
expect(wrapper.vm.data.name).toEqual('123');
});
test('should allow deleting contact', async () => {
const wrapper = mount(ContactEdit, {
props: {
isEdit: true,
},
});
const deleteButton = wrapper.findAll('.van-button').at(1);
deleteButton.trigger('click');
await later();
document.querySelector('.van-dialog__confirm').click();
await later();
expect(wrapper.emitted('delete')).toBeTruthy();
});

View File

@ -0,0 +1,91 @@
import ContactEdit from '..';
import { mount, later } from '../../../test';
const contactInfo = {
name: 'foo',
tel: '13000000000',
};
test('should validate contact name before submitting form', async () => {
const wrapper = mount(ContactEdit, {
props: {
contactInfo: {
name: '',
tel: '',
},
},
});
const button = wrapper.find('.van-button');
button.trigger('click');
await later();
expect(wrapper.find('.van-field__error-message').html()).toMatchSnapshot();
const fields = wrapper.findAll('.van-field__control');
fields[0].trigger('focus');
await later();
expect(wrapper.find('.van-field__error-message').exists()).toBeFalsy();
});
test('should validate contact tel before submitting form', async () => {
const wrapper = mount(ContactEdit, {
props: {
contactInfo: {
name: 'name',
tel: '',
},
},
});
const button = wrapper.find('.van-button');
button.trigger('click');
await later();
expect(wrapper.find('.van-field__error-message').html()).toMatchSnapshot();
const fields = wrapper.findAll('.van-field__control');
fields[1].trigger('focus');
await later();
expect(wrapper.find('.van-field__error-message').exists()).toBeFalsy();
});
test('should emit save event after submitting form', async () => {
const wrapper = mount(ContactEdit, {
props: {
contactInfo,
},
});
const button = wrapper.find('.van-button');
button.trigger('click');
await later();
expect(wrapper.emitted('save')[0][0]).toEqual(contactInfo);
});
test('should watch contact info', async () => {
const wrapper = mount(ContactEdit);
const button = wrapper.find('.van-button');
wrapper.setProps({ contactInfo });
await later();
button.trigger('click');
expect(wrapper.emitted('save')[0][0]).toEqual(contactInfo);
});
test('should allow deleting contact', async () => {
const wrapper = mount(ContactEdit, {
props: {
isEdit: true,
},
});
const deleteButton = wrapper.findAll('.van-button')[1];
deleteButton.trigger('click');
await later();
document.querySelector('.van-dialog__confirm').click();
await later();
expect(wrapper.emitted('delete')).toBeTruthy();
});