[improvement] Search: tsx (#3028)

This commit is contained in:
neverland
2019-03-21 17:20:06 +08:00
committed by GitHub
parent 60657f800f
commit 7c4908a2e3
7 changed files with 186 additions and 106 deletions
+48 -7
View File
@@ -2,34 +2,75 @@ import Search from '..';
import { mount } from '../../../test/utils';
test('listen input event', () => {
const wrapper = mount(Search);
const onInput = jest.fn();
const wrapper = mount(Search, {
context: {
on: {
input: onInput
}
}
});
const input = wrapper.find('input');
input.element.value = '1';
input.trigger('input');
expect(wrapper.emitted('input')[0][0]).toEqual('1');
expect(onInput).toBeCalledWith('1');
});
test('cancel search', () => {
const onInput = jest.fn();
const onCancel = jest.fn();
const wrapper = mount(Search, {
propsData: {
value: 'test',
showAction: true
},
context: {
on: {
input: onInput,
cancel: onCancel
}
}
});
const cancel = wrapper.find('.van-search__action div');
cancel.trigger('click');
expect(wrapper.emitted('input')[0][0]).toEqual('');
expect(wrapper.emitted('cancel')).toBeTruthy();
expect(onInput).toBeCalledWith('');
expect(onCancel).toBeCalled();
});
test('emit a search event', () => {
const wrapper = mount(Search);
const onSearch = jest.fn();
const onKeypress = jest.fn();
const wrapper = mount(Search, {
context: {
on: {
search: onSearch,
keypress: onKeypress
}
}
});
const input = wrapper.find('input');
input.trigger('keypress.enter');
input.trigger('keypress.a');
expect(wrapper.emitted('search')).toBeTruthy();
expect(wrapper.emitted('keypress')).toBeTruthy();
expect(onSearch).toBeCalled();
expect(onKeypress).toBeCalled();
});
test('render label slot', () => {
const wrapper = mount(Search, {
scopedSlots: {
label() {
return 'Custom Label';
}
}
});
expect(wrapper).toMatchSnapshot();
});