diff --git a/src/contact-edit/test/__snapshots__/index.spec.js.snap b/src/contact-edit/test/__snapshots__/index.spec.js.snap new file mode 100644 index 000000000..9b5f5c7ae --- /dev/null +++ b/src/contact-edit/test/__snapshots__/index.spec.js.snap @@ -0,0 +1,13 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should validate contact name before submitting form 1`] = ` +
+ 请输入正确的姓名 +
+`; + +exports[`should validate contact tel before submitting form 1`] = ` +
+ 请输入正确的手机号 +
+`; diff --git a/src/contact-edit/test/index.legacy.js b/src/contact-edit/test/index.legacy.js deleted file mode 100644 index 77677911f..000000000 --- a/src/contact-edit/test/index.legacy.js +++ /dev/null @@ -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(); -}); diff --git a/src/contact-edit/test/index.spec.js b/src/contact-edit/test/index.spec.js new file mode 100644 index 000000000..498ebfdfd --- /dev/null +++ b/src/contact-edit/test/index.spec.js @@ -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(); +});