index.spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import ContactCard from '..';
  2. import ContactList from '../../contact-list';
  3. import ContactEdit from '../../contact-edit';
  4. import { mount, later } from '../../../test/utils';
  5. const contactInfo = {
  6. name: 'test',
  7. tel: '123123213'
  8. };
  9. describe('ContactCard', () => {
  10. test('click event', () => {
  11. const click = jest.fn();
  12. const wrapper = mount(ContactCard, {
  13. context: {
  14. on: {
  15. click
  16. }
  17. }
  18. });
  19. wrapper.trigger('click');
  20. expect(click).toHaveBeenCalledTimes(1);
  21. });
  22. test('not editable', () => {
  23. const click = jest.fn();
  24. const wrapper = mount(ContactCard, {
  25. propsData: {
  26. editable: false
  27. },
  28. context: {
  29. on: {
  30. click
  31. }
  32. }
  33. });
  34. wrapper.trigger('click');
  35. expect(click).toHaveBeenCalledTimes(0);
  36. });
  37. });
  38. describe('ContactList', () => {
  39. test('render', () => {
  40. const wrapper = mount(ContactList, {
  41. propsData: {
  42. list: [contactInfo]
  43. }
  44. });
  45. expect(wrapper).toMatchSnapshot();
  46. });
  47. test('select event', () => {
  48. const onSelect = jest.fn();
  49. const wrapper = mount(ContactList, {
  50. propsData: {
  51. list: [contactInfo]
  52. },
  53. context: {
  54. on: {
  55. select: onSelect
  56. }
  57. }
  58. });
  59. wrapper.find('.van-radio__icon').trigger('click');
  60. expect(onSelect).toHaveBeenCalled();
  61. });
  62. });
  63. describe('ContactEdit', () => {
  64. const createComponent = () => {
  65. const wrapper = mount(ContactEdit, {
  66. propsData: {
  67. contactInfo
  68. }
  69. });
  70. const button = wrapper.find('.van-button');
  71. const field = wrapper.findAll('.van-field__control');
  72. const { errorInfo, data } = wrapper.vm;
  73. return {
  74. wrapper,
  75. data,
  76. field,
  77. button,
  78. errorInfo
  79. };
  80. };
  81. test('valid name', () => {
  82. const { data, field, button, errorInfo } = createComponent();
  83. // name empty
  84. data.name = '';
  85. button.trigger('click');
  86. expect(errorInfo.name).toBeTruthy();
  87. field.at(0).trigger('focus');
  88. expect(errorInfo.name).toBeFalsy();
  89. });
  90. it('valid tel', () => {
  91. const { data, field, button, errorInfo, wrapper } = createComponent();
  92. data.tel = '';
  93. button.trigger('click');
  94. expect(errorInfo.tel).toBeTruthy();
  95. field.at(1).trigger('focus');
  96. expect(errorInfo.tel).toBeFalsy();
  97. data.tel = '13000000000';
  98. button.trigger('click');
  99. expect(errorInfo.tel).toBeFalsy();
  100. expect(wrapper.emitted('save')[0][0]).toEqual({
  101. name: 'test',
  102. tel: '13000000000'
  103. });
  104. });
  105. test('watch contact info', () => {
  106. const wrapper = mount(ContactEdit);
  107. wrapper.setProps({ contactInfo: { name: '123' } });
  108. expect(wrapper.vm.data.name).toEqual('123');
  109. });
  110. test('delete contact', async () => {
  111. const wrapper = mount(ContactEdit, {
  112. propsData: {
  113. isEdit: true
  114. }
  115. });
  116. const deleteButton = wrapper.findAll('.van-button').at(1);
  117. deleteButton.trigger('click');
  118. await later();
  119. document.querySelector('.van-dialog__confirm').click();
  120. await later();
  121. expect(wrapper.emitted('delete')).toBeTruthy();
  122. });
  123. });