vant/test/unit/specs/search.spec.js
Yao f02048a3dc [breaking change] Search: 修改原有结构 (#198)
* 修改文档 注释

* [breaking change] change type showcase to simple

* [breaking change] search 组件传入参数更改

* search 文档更改

* [breaking change] delete type && add background && showSearch

* showCancel 更名为 showAction && 增加 slot action

* add search test code
2017-10-12 20:56:03 +08:00

121 lines
3.1 KiB
JavaScript

import Search from 'packages/search';
import { mount } from 'avoriaz';
describe('Search', () => {
let wrapper;
afterEach(() => {
wrapper && wrapper.destroy();
});
it('create a search', () => {
wrapper = mount(Search);
expect(wrapper.hasClass('van-search')).to.be.true;
expect(wrapper.data().focusStatus).to.be.false;
expect(wrapper.data().isFocus).to.be.false;
});
it('focus on input', () => {
wrapper = mount(Search);
const input = wrapper.find('.van-search__input')[0];
input.trigger('focus');
expect(wrapper.data().isFocus).to.be.true;
});
it('create a search with searchText', (done) => {
wrapper = mount(Search, {
propsData: {
value: 'search text'
}
});
wrapper.vm.$nextTick(() => {
const input = wrapper.find('.van-search__input')[0];
expect(input.element.value === 'search text').to.be.true;
done();
});
});
it('emit input event', () => {
wrapper = mount(Search);
const input = wrapper.find('.van-search__input')[0];
const eventStub = sinon.stub(wrapper.vm, '$emit');
input.trigger('input', { target: { value: 'search' }});
expect(eventStub.calledOnce).to.be.true;
expect(eventStub.calledWith('input')).to.be.true;
});
it('handle clean click and refocus', (done) => {
wrapper = mount(Search);
wrapper.setProps({ value: 'test' });
const eventStub = sinon.stub(wrapper.vm, '$emit');
const input = wrapper.find('.van-search__input')[0];
input.trigger('focus');
const cleanBtn = wrapper.find('.van-icon-clear')[0];
cleanBtn.trigger('click');
wrapper.vm.$nextTick(() => {
expect(eventStub.calledOnce).to.be.true;
expect(eventStub.calledWith('input')).to.be.true;
done();
});
});
it('handle cancel click', (done) => {
wrapper = mount(Search);
wrapper.setProps({ value: 'test', showAction: true });
expect(wrapper.vm.value).to.be.equal('test');
const eventStub = sinon.stub(wrapper.vm, '$emit');
const cancelBtn = wrapper.find('.van-search__action-text')[0];
cancelBtn.trigger('click');
wrapper.vm.$nextTick(() => {
expect(wrapper.vm.focusStatus).to.be.false;
expect(wrapper.vm.isFocus).to.be.false;
expect(eventStub.calledTwice).to.be.true;
expect(eventStub.calledWith('input'));
expect(eventStub.calledWith('change'));
done();
});
});
it('emit a search event', (done) => {
wrapper = mount(Search);
const eventStub = sinon.stub(wrapper.vm, '$emit');
const input = wrapper.find('.van-search__input')[0];
input.trigger('keypress.enter');
wrapper.vm.$nextTick(() => {
expect(eventStub.calledOnce).to.be.true;
expect(eventStub.calledWith('search'));
done();
});
});
it('blur after click outside', () => {
wrapper = mount(Search);
const input = wrapper.find('.van-search__input')[0];
input.trigger('focus');
expect(wrapper.vm.isFocus).to.be.true;
const body = document.body;
body.click();
expect(wrapper.vm.isFocus).to.be.false;
expect(wrapper.vm.focusStatus).to.be.false;
});
});