From fc34ea12e668b60292ba73aca2a0d69c643e141a Mon Sep 17 00:00:00 2001 From: AllenFang Date: Sat, 10 Feb 2018 15:44:11 +0800 Subject: [PATCH] patch test for number filter --- .../test/components/number.test.js | 310 ++++++++++++++++++ .../test/filter.test.js | 114 ++++++- 2 files changed, 422 insertions(+), 2 deletions(-) create mode 100644 packages/react-bootstrap-table2-filter/test/components/number.test.js diff --git a/packages/react-bootstrap-table2-filter/test/components/number.test.js b/packages/react-bootstrap-table2-filter/test/components/number.test.js new file mode 100644 index 0000000..6ba66f7 --- /dev/null +++ b/packages/react-bootstrap-table2-filter/test/components/number.test.js @@ -0,0 +1,310 @@ +import 'jsdom-global/register'; +import React from 'react'; +import sinon from 'sinon'; +import { mount } from 'enzyme'; +import NumberFilter from '../../src/components/number'; +import { FILTER_TYPE } from '../../src/const'; +import * as Comparator from '../../src/comparison'; + + +describe('Number Filter', () => { + let wrapper; + const onFilter = sinon.stub(); + const column = { + dataField: 'price', + text: 'Product Price' + }; + + afterEach(() => { + onFilter.reset(); + }); + + describe('initialization', () => { + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should have correct state', () => { + expect(wrapper.state().isSelected).toBeFalsy(); + }); + + it('should rendering component successfully', () => { + expect(wrapper).toHaveLength(1); + expect(wrapper.find('select')).toHaveLength(1); + expect(wrapper.find('input[type="number"]')).toHaveLength(1); + expect(wrapper.find('.number-filter')).toHaveLength(1); + }); + + it('should rendering comparator options correctly', () => { + const select = wrapper.find('select'); + expect(select.find('option')).toHaveLength(wrapper.prop('comparators').length + 1); + }); + }); + + describe('when withoutEmptyComparatorOption prop is true', () => { + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering comparator options correctly', () => { + const select = wrapper.find('select'); + expect(select.find('option')).toHaveLength(wrapper.prop('comparators').length); + }); + }); + + describe('when defaultValue.number props is defined', () => { + const number = 203; + + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering input successfully', () => { + expect(wrapper).toHaveLength(1); + const input = wrapper.find('input[type="number"]'); + expect(input).toHaveLength(1); + expect(input.props().defaultValue).toEqual(number); + }); + }); + + describe('when defaultValue.comparator props is defined', () => { + const comparator = Comparator.EQ; + + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering comparator select successfully', () => { + expect(wrapper).toHaveLength(1); + const select = wrapper.find('.number-filter-comparator'); + expect(select).toHaveLength(1); + expect(select.props().defaultValue).toEqual(comparator); + }); + }); + + describe('when defaultValue.number and defaultValue.comparator props is defined', () => { + const number = 203; + const comparator = Comparator.EQ; + + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should have correct state', () => { + expect(wrapper.state().isSelected).toBeTruthy(); + }); + + it('should calling onFilter on componentDidMount', () => { + expect(onFilter.calledOnce).toBeTruthy(); + expect(onFilter.calledWith( + column, { number: `${number}`, comparator }, FILTER_TYPE.NUMBER)).toBeTruthy(); + }); + }); + + describe('when options props is defined', () => { + const options = [2100, 2103, 2105]; + + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering number options instead of number input', () => { + expect(wrapper).toHaveLength(1); + const select = wrapper.find('.select-filter.placeholder-selected'); + expect(select).toHaveLength(1); + expect(select.find('option')).toHaveLength(options.length + 1); + }); + + describe('when withoutEmptyNumberOption props is defined', () => { + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering number options instead of number input', () => { + const select = wrapper.find('.select-filter.placeholder-selected'); + expect(select).toHaveLength(1); + expect(select.find('option')).toHaveLength(options.length); + }); + }); + + describe('when defaultValue.number props is defined', () => { + const number = 203; + + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering number options successfully', () => { + const select = wrapper.find('.select-filter.placeholder-selected'); + expect(select).toHaveLength(1); + expect(select.props().defaultValue).toEqual(number); + }); + }); + + describe('when defaultValue.number and defaultValue.comparator props is defined', () => { + const number = options[1]; + const comparator = Comparator.EQ; + + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering number options successfully', () => { + let select = wrapper.find('.placeholder-selected'); + expect(select).toHaveLength(0); + + select = wrapper.find('.select-filter'); + expect(select).toHaveLength(1); + }); + }); + }); + + describe('when style props is defined', () => { + const style = { backgroundColor: 'red' }; + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering component successfully', () => { + expect(wrapper).toHaveLength(1); + expect(wrapper.find('.number-filter').prop('style')).toEqual(style); + }); + }); + + describe('when numberStyle props is defined', () => { + const numberStyle = { backgroundColor: 'red' }; + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering component successfully', () => { + expect(wrapper).toHaveLength(1); + expect(wrapper.find('.number-filter-input').prop('style')).toEqual(numberStyle); + }); + }); + + describe('when comparatorStyle props is defined', () => { + const comparatorStyle = { backgroundColor: 'red' }; + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering component successfully', () => { + expect(wrapper).toHaveLength(1); + expect(wrapper.find('select').prop('style')).toEqual(comparatorStyle); + }); + }); + + describe('when className props is defined', () => { + const className = 'test'; + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering component successfully', () => { + expect(wrapper).toHaveLength(1); + expect(wrapper.hasClass(className)).toBeTruthy(); + }); + }); + + describe('when numberClassName props is defined', () => { + const className = 'test'; + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering component successfully', () => { + expect(wrapper).toHaveLength(1); + expect(wrapper.find('.number-filter-input').prop('className').indexOf(className) > -1).toBeTruthy(); + }); + }); + + describe('when comparatorClassName props is defined', () => { + const className = 'test'; + beforeEach(() => { + wrapper = mount( + + ); + }); + + it('should rendering component successfully', () => { + expect(wrapper).toHaveLength(1); + expect(wrapper.find('select').prop('className').indexOf(className) > -1).toBeTruthy(); + }); + }); +}); diff --git a/packages/react-bootstrap-table2-filter/test/filter.test.js b/packages/react-bootstrap-table2-filter/test/filter.test.js index e834f17..28be4a5 100644 --- a/packages/react-bootstrap-table2-filter/test/filter.test.js +++ b/packages/react-bootstrap-table2-filter/test/filter.test.js @@ -4,7 +4,7 @@ import Store from 'react-bootstrap-table-next/src/store'; import { filters } from '../src/filter'; import { FILTER_TYPE } from '../src/const'; -import { LIKE, EQ } from '../src/comparison'; +import { LIKE, EQ, GT, GE, LT, LE, NE } from '../src/comparison'; const data = []; for (let i = 0; i < 20; i += 1) { @@ -37,7 +37,7 @@ describe('filter', () => { }]; }); - describe('text filter', () => { + describe('filterByText', () => { beforeEach(() => { filterFn = filters(store, columns, _); }); @@ -91,4 +91,114 @@ describe('filter', () => { }); }); }); + + describe('filterByNumber', () => { + beforeEach(() => { + filterFn = filters(store, columns, _); + }); + + describe('when currFilters.filterVal.comparator is empty', () => { + it('should returning correct result', () => { + currFilters.price = { + filterVal: { comparator: '', number: '203' }, + filterType: FILTER_TYPE.NUMBER + }; + + let result = filterFn(currFilters); + expect(result).toHaveLength(data.length); + + currFilters.price.filterVal.comparator = undefined; + result = filterFn(currFilters); + expect(result).toHaveLength(data.length); + }); + }); + + describe('when currFilters.filterVal.number is empty', () => { + it('should returning correct result', () => { + currFilters.price = { + filterVal: { comparator: EQ, number: '' }, + filterType: FILTER_TYPE.NUMBER + }; + + const result = filterFn(currFilters); + expect(result).toHaveLength(data.length); + }); + }); + + describe(`when currFilters.filterVal.comparator is ${EQ}`, () => { + it('should returning correct result', () => { + currFilters.price = { + filterVal: { comparator: EQ, number: '203' }, + filterType: FILTER_TYPE.NUMBER + }; + + let result = filterFn(currFilters); + expect(result).toHaveLength(1); + + currFilters.price.filterVal.number = '0'; + result = filterFn(currFilters); + expect(result).toHaveLength(0); + }); + }); + + describe(`when currFilters.filterVal.comparator is ${GT}`, () => { + it('should returning correct result', () => { + currFilters.price = { + filterVal: { comparator: GT, number: '203' }, + filterType: FILTER_TYPE.NUMBER + }; + + const result = filterFn(currFilters); + expect(result).toHaveLength(16); + }); + }); + + describe(`when currFilters.filterVal.comparator is ${GE}`, () => { + it('should returning correct result', () => { + currFilters.price = { + filterVal: { comparator: GE, number: '203' }, + filterType: FILTER_TYPE.NUMBER + }; + + const result = filterFn(currFilters); + expect(result).toHaveLength(17); + }); + }); + + describe(`when currFilters.filterVal.comparator is ${LT}`, () => { + it('should returning correct result', () => { + currFilters.price = { + filterVal: { comparator: LT, number: '203' }, + filterType: FILTER_TYPE.NUMBER + }; + + const result = filterFn(currFilters); + expect(result).toHaveLength(3); + }); + }); + + describe(`when currFilters.filterVal.comparator is ${LE}`, () => { + it('should returning correct result', () => { + currFilters.price = { + filterVal: { comparator: LE, number: '203' }, + filterType: FILTER_TYPE.NUMBER + }; + + const result = filterFn(currFilters); + expect(result).toHaveLength(4); + }); + }); + + describe(`when currFilters.filterVal.comparator is ${NE}`, () => { + it('should returning correct result', () => { + currFilters.price = { + filterVal: { comparator: NE, number: '203' }, + filterType: FILTER_TYPE.NUMBER + }; + + const result = filterFn(currFilters); + expect(result).toHaveLength(19); + }); + }); + }); });