index.spec.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import Search from '..';
  2. import { mount } from '../../../test/utils';
  3. test('listen input event', () => {
  4. const onInput = jest.fn();
  5. const wrapper = mount(Search, {
  6. context: {
  7. on: {
  8. input: onInput
  9. }
  10. }
  11. });
  12. const input = wrapper.find('input');
  13. input.element.value = '1';
  14. input.trigger('input');
  15. expect(onInput).toHaveBeenCalledWith('1');
  16. });
  17. test('cancel search', () => {
  18. const onInput = jest.fn();
  19. const onCancel = jest.fn();
  20. const wrapper = mount(Search, {
  21. propsData: {
  22. value: 'test',
  23. showAction: true
  24. },
  25. context: {
  26. on: {
  27. input: onInput,
  28. cancel: onCancel
  29. }
  30. }
  31. });
  32. const cancel = wrapper.find('.van-search__action div');
  33. cancel.trigger('click');
  34. expect(onInput).toHaveBeenCalledWith('');
  35. expect(onCancel).toHaveBeenCalled();
  36. });
  37. test('emit a search event', () => {
  38. const onSearch = jest.fn();
  39. const onKeypress = jest.fn();
  40. const wrapper = mount(Search, {
  41. context: {
  42. on: {
  43. search: onSearch,
  44. keypress: onKeypress
  45. }
  46. }
  47. });
  48. const input = wrapper.find('input');
  49. input.trigger('keypress.enter');
  50. input.trigger('keypress.a');
  51. expect(onSearch).toHaveBeenCalled();
  52. expect(onKeypress).toHaveBeenCalled();
  53. });
  54. test('render label slot', () => {
  55. const wrapper = mount(Search, {
  56. scopedSlots: {
  57. label() {
  58. return 'Custom Label';
  59. }
  60. }
  61. });
  62. expect(wrapper).toMatchSnapshot();
  63. });