patch tests for search

This commit is contained in:
AllenFang
2018-06-23 14:24:15 +08:00
parent 0d4d32c6e4
commit 7b15bf45d9
4 changed files with 95 additions and 2 deletions

View File

@@ -134,7 +134,6 @@ class Container extends React.Component {
}
handleTableChange = (type, { searchText }) => {
console.log('table change');
setTimeout(() => {
const result = products.filter((row) => {
for (let cidx = 0; cidx < columns.length; cidx += 1) {

View File

@@ -44,7 +44,7 @@ export default ExtendBase =>
isRemoteSearch = () => {
const { remote } = this.props;
return remote === true || (_.isObject(remote) && remote.search);
return remote === true || (_.isObject(remote) && remote.search) || this.isRemotePagination();
}
isRemotePagination = () => {

View File

@@ -134,6 +134,38 @@ describe('Context', () => {
});
});
describe('if search is enable', () => {
beforeEach(() => {
const SearchContext = React.createContext();
const search = {
createContext: jest.fn().mockReturnValue({
Provider: SearchContext.Provider,
Consumer: SearchContext.Consumer
}),
searchText: ''
};
wrapper = shallow(
<BootstrapTable
keyField={ keyField }
data={ data }
columns={ columns }
search={ search }
/>
);
wrapper.render();
});
it('should create contexts correctly', () => {
expect(wrapper.instance().DataContext).toBeDefined();
expect(wrapper.instance().SearchContext).toBeDefined();
expect(wrapper.instance().SortContext).not.toBeDefined();
expect(wrapper.instance().SelectionContext).not.toBeDefined();
expect(wrapper.instance().CellEditContext).not.toBeDefined();
expect(wrapper.instance().FilterContext).not.toBeDefined();
expect(wrapper.instance().PaginationContext).not.toBeDefined();
});
});
describe('if column filter is enable', () => {
beforeEach(() => {
const FilterContext = React.createContext();

View File

@@ -186,6 +186,48 @@ describe('remoteResolver', () => {
});
});
describe('isRemoteSearch', () => {
describe('when remote is false', () => {
beforeEach(() => {
shallowContainer();
});
it('should return false', () => {
expect(wrapper.instance().isRemoteSearch()).toBeFalsy();
});
});
describe('when remote is true', () => {
beforeEach(() => {
shallowContainer({ remote: true });
});
it('should return true', () => {
expect(wrapper.instance().isRemoteSearch()).toBeTruthy();
});
});
describe('when remote.search is true', () => {
beforeEach(() => {
shallowContainer({ remote: { search: true } });
});
it('should return true', () => {
expect(wrapper.instance().isRemoteSearch()).toBeTruthy();
});
});
describe('when this.isRemotePagination return true', () => {
beforeEach(() => {
shallowContainer({ remote: { pagination: true } });
});
it('should return true', () => {
expect(wrapper.instance().isRemoteSearch()).toBeTruthy();
});
});
});
describe('handleRemoteCellChange', () => {
const onTableChangeCB = sinon.stub();
const rowId = 1;
@@ -244,6 +286,26 @@ describe('remoteResolver', () => {
});
});
describe('handleRemoteSearchChange', () => {
const onTableChangeCB = sinon.stub();
const searchText = 'abc';
beforeEach(() => {
onTableChangeCB.reset();
shallowContainer({
onTableChange: onTableChangeCB
});
wrapper.instance().handleRemoteSearchChange(searchText);
});
it('should calling props.onTableChange correctly', () => {
expect(onTableChangeCB.calledOnce).toBeTruthy();
expect(onTableChangeCB.calledWith('search', wrapper.instance().getNewestState({
searchText
}))).toBeTruthy();
});
});
describe('handleRemoteFilterChange', () => {
const onTableChangeCB = sinon.stub();
const filters = { price: { filterVal: 20, filterType: 'TEXT' } };