test: Add jest-enzyme assertion library for better frontend tests (#10459)

* adding jest-enzyme

* enzymeify lots of assertions

* types for jest-enzyme
This commit is contained in:
David Aaron Suddjian
2020-07-29 10:53:06 -07:00
committed by GitHub
parent 671461d0d0
commit 7f70a241f9
111 changed files with 1074 additions and 315 deletions

View File

@@ -75,7 +75,7 @@ function setup(overrides) {
describe('AdhocFilterControl', () => {
it('renders an onPasteSelect', () => {
const { wrapper } = setup();
expect(wrapper.find(OnPasteSelect)).toHaveLength(1);
expect(wrapper.find(OnPasteSelect)).toExist();
});
it('handles saved metrics being selected to filter on', () => {

View File

@@ -80,8 +80,8 @@ function setup(overrides) {
describe('AdhocFilterEditPopover', () => {
it('renders simple tab content by default', () => {
const { wrapper } = setup();
expect(wrapper.find(Popover)).toHaveLength(1);
expect(wrapper.find(Tabs)).toHaveLength(1);
expect(wrapper.find(Popover)).toExist();
expect(wrapper.find(Tabs)).toExist();
expect(wrapper.find(Tab)).toHaveLength(2);
expect(wrapper.find(Button)).toHaveLength(2);
expect(wrapper.find(AdhocFilterEditPopoverSimpleTabContent)).toHaveLength(
@@ -91,11 +91,11 @@ describe('AdhocFilterEditPopover', () => {
it('renders sql tab content when the adhoc filter expressionType is sql', () => {
const { wrapper } = setup({ adhocFilter: sqlAdhocFilter });
expect(wrapper.find(Popover)).toHaveLength(1);
expect(wrapper.find(Tabs)).toHaveLength(1);
expect(wrapper.find(Popover)).toExist();
expect(wrapper.find(Tabs)).toExist();
expect(wrapper.find(Tab)).toHaveLength(2);
expect(wrapper.find(Button)).toHaveLength(2);
expect(wrapper.find(AdhocFilterEditPopoverSqlTabContent)).toHaveLength(1);
expect(wrapper.find(AdhocFilterEditPopoverSqlTabContent)).toExist();
});
it('overwrites the adhocFilter in state with onAdhocFilterChange', () => {
@@ -106,20 +106,20 @@ describe('AdhocFilterEditPopover', () => {
it('prevents saving if the filter is invalid', () => {
const { wrapper } = setup();
expect(wrapper.find(Button).find({ disabled: true })).toHaveLength(0);
expect(wrapper.find(Button).find({ disabled: true })).not.toExist();
wrapper
.instance()
.onAdhocFilterChange(simpleAdhocFilter.duplicateWith({ operator: null }));
expect(wrapper.find(Button).find({ disabled: true })).toHaveLength(1);
expect(wrapper.find(Button).find({ disabled: true })).toExist();
wrapper.instance().onAdhocFilterChange(sqlAdhocFilter);
expect(wrapper.find(Button).find({ disabled: true })).toHaveLength(0);
expect(wrapper.find(Button).find({ disabled: true })).not.toExist();
});
it('highlights save if changes are present', () => {
const { wrapper } = setup();
expect(wrapper.find(Button).find({ bsStyle: 'primary' })).toHaveLength(0);
expect(wrapper.find(Button).find({ bsStyle: 'primary' })).not.toExist();
wrapper.instance().onAdhocFilterChange(sqlAdhocFilter);
expect(wrapper.find(Button).find({ bsStyle: 'primary' })).toHaveLength(1);
expect(wrapper.find(Button).find({ bsStyle: 'primary' })).toExist();
});
it('will initiate a drag when clicked', () => {
@@ -127,7 +127,7 @@ describe('AdhocFilterEditPopover', () => {
wrapper.instance().onDragDown = sinon.spy();
wrapper.instance().forceUpdate();
expect(wrapper.find('i.fa-expand')).toHaveLength(1);
expect(wrapper.find('i.fa-expand')).toExist();
expect(wrapper.instance().onDragDown.calledOnce).toBe(false);
wrapper.find('i.fa-expand').simulate('mouseDown', {});
expect(wrapper.instance().onDragDown.calledOnce).toBe(true);

View File

@@ -55,7 +55,7 @@ describe('AdhocFilterOption', () => {
const overlay = wrapper.find(OverlayTrigger);
expect(overlay).toHaveLength(1);
expect(overlay.props().defaultOverlayShown).toBe(false);
expect(wrapper.find(Label)).toHaveLength(1);
expect(wrapper.find(Label)).toExist();
});
it('should open new filter popup by default', () => {
const { wrapper } = setup({

View File

@@ -51,7 +51,7 @@ function setup(overrides) {
describe('AdhocMetricEditPopoverTitle', () => {
it('renders an OverlayTrigger wrapper with the title', () => {
const { wrapper } = setup();
expect(wrapper.find(OverlayTrigger)).toHaveLength(1);
expect(wrapper.find(OverlayTrigger)).toExist();
expect(wrapper.find(OverlayTrigger).find('span').text()).toBe(
'My Metric\xa0',
);

View File

@@ -60,7 +60,7 @@ function setup(overrides) {
describe('AdhocMetricEditPopover', () => {
it('renders a popover with edit metric form contents', () => {
const { wrapper } = setup();
expect(wrapper.find(Popover)).toHaveLength(1);
expect(wrapper.find(Popover)).toExist();
expect(wrapper.find(FormGroup)).toHaveLength(3);
expect(wrapper.find(Button)).toHaveLength(2);
});
@@ -106,20 +106,20 @@ describe('AdhocMetricEditPopover', () => {
it('prevents saving if no column or aggregate is chosen', () => {
const { wrapper } = setup();
expect(wrapper.find(Button).find({ disabled: true })).toHaveLength(0);
expect(wrapper.find(Button).find({ disabled: true })).not.toExist();
wrapper.instance().onColumnChange(null);
expect(wrapper.find(Button).find({ disabled: true })).toHaveLength(1);
expect(wrapper.find(Button).find({ disabled: true })).toExist();
wrapper.instance().onColumnChange({ column: columns[0] });
expect(wrapper.find(Button).find({ disabled: true })).toHaveLength(0);
expect(wrapper.find(Button).find({ disabled: true })).not.toExist();
wrapper.instance().onAggregateChange(null);
expect(wrapper.find(Button).find({ disabled: true })).toHaveLength(1);
expect(wrapper.find(Button).find({ disabled: true })).toExist();
});
it('highlights save if changes are present', () => {
const { wrapper } = setup();
expect(wrapper.find(Button).find({ bsStyle: 'primary' })).toHaveLength(0);
expect(wrapper.find(Button).find({ bsStyle: 'primary' })).not.toExist();
wrapper.instance().onColumnChange({ column: columns[1] });
expect(wrapper.find(Button).find({ bsStyle: 'primary' })).toHaveLength(1);
expect(wrapper.find(Button).find({ bsStyle: 'primary' })).toExist();
});
it('will initiate a drag when clicked', () => {
@@ -127,7 +127,7 @@ describe('AdhocMetricEditPopover', () => {
wrapper.instance().onDragDown = sinon.spy();
wrapper.instance().forceUpdate();
expect(wrapper.find('i.fa-expand')).toHaveLength(1);
expect(wrapper.find('i.fa-expand')).toExist();
expect(wrapper.instance().onDragDown.calledOnce).toBe(false);
wrapper.find('i.fa-expand').simulate('mouseDown');
expect(wrapper.instance().onDragDown.calledOnce).toBe(true);

View File

@@ -52,8 +52,8 @@ function setup(overrides) {
describe('AdhocMetricOption', () => {
it('renders an overlay trigger wrapper for the label', () => {
const { wrapper } = setup();
expect(wrapper.find(OverlayTrigger)).toHaveLength(1);
expect(wrapper.find(Label)).toHaveLength(1);
expect(wrapper.find(OverlayTrigger)).toExist();
expect(wrapper.find(Label)).toExist();
});
it('overlay should open if metric is new', () => {

View File

@@ -53,7 +53,7 @@ describe('ColorPickerControl', () => {
it('renders a OverlayTrigger', () => {
const controlHeader = wrapper.find(ControlHeader);
expect(controlHeader).toHaveLength(1);
expect(wrapper.find(OverlayTrigger)).toHaveLength(1);
expect(wrapper.find(OverlayTrigger)).toExist();
});
it('renders a Popover with a SketchPicker', () => {

View File

@@ -37,6 +37,6 @@ describe('ColorSchemeControl', () => {
});
it('renders a Creatable', () => {
expect(wrapper.find(Creatable)).toHaveLength(1);
expect(wrapper.find(Creatable)).toExist();
});
});

View File

@@ -43,7 +43,7 @@ describe('ControlPanelSection', () => {
it('renders a Panel component', () => {
wrapper = shallow(<ControlPanelSection {...defaultProps} />);
expect(wrapper.find(Panel)).toHaveLength(1);
expect(wrapper.find(Panel)).toExist();
});
describe('with optional props', () => {

View File

@@ -23,12 +23,12 @@ import ControlSetRow from 'src/explore/components/ControlRow';
describe('ControlSetRow', () => {
it('renders a single row with one element', () => {
const wrapper = shallow(<ControlSetRow controls={[<a />]} />);
expect(wrapper.find('.row')).toHaveLength(1);
expect(wrapper.find('.row').find('a')).toHaveLength(1);
expect(wrapper.find('.row')).toExist();
expect(wrapper.find('.row').find('a')).toExist();
});
it('renders a single row with two elements', () => {
const wrapper = shallow(<ControlSetRow controls={[<a />, <a />]} />);
expect(wrapper.find('.row')).toHaveLength(1);
expect(wrapper.find('.row')).toExist();
expect(wrapper.find('.row').find('a')).toHaveLength(2);
});
});

View File

@@ -62,17 +62,17 @@ describe('DatasourceControl', () => {
it('renders a Modal', () => {
const wrapper = setup();
expect(wrapper.find(DatasourceModal)).toHaveLength(1);
expect(wrapper.find(DatasourceModal)).toExist();
});
it('renders a ChangeDatasourceModal', () => {
const wrapper = setup();
expect(wrapper.find(ChangeDatasourceModal)).toHaveLength(1);
expect(wrapper.find(ChangeDatasourceModal)).toExist();
});
it('show or hide Edit Datasource option', () => {
let wrapper = setup();
expect(wrapper.find('#datasource_menu')).toHaveLength(1);
expect(wrapper.find('#datasource_menu')).toExist();
expect(wrapper.find('#datasource_menu').dive().find(MenuItem)).toHaveLength(
3,
);
@@ -80,7 +80,7 @@ describe('DatasourceControl', () => {
wrapper = setup({
isEditable: false,
});
expect(wrapper.find('#datasource_menu')).toHaveLength(1);
expect(wrapper.find('#datasource_menu')).toExist();
expect(wrapper.find('#datasource_menu').dive().find(MenuItem)).toHaveLength(
2,
);

View File

@@ -69,10 +69,10 @@ describe('DateFilterControl', () => {
const label = wrapper.find(Label).first();
label.simulate('click');
setTimeout(() => {
expect(wrapper.find('.popover')).toHaveLength(1);
expect(wrapper.find('.popover')).toExist();
expect(wrapper.find('.ok')).first().simulate('click');
setTimeout(() => {
expect(wrapper.find('.popover')).toHaveLength(0);
expect(wrapper.find('.popover')).not.toExist();
}, 10);
}, 10);
});

View File

@@ -37,7 +37,7 @@ describe('EmbedCodeButton', () => {
it('renders overlay trigger', () => {
const wrapper = shallow(<EmbedCodeButton {...defaultProps} />);
expect(wrapper.find(OverlayTrigger)).toHaveLength(1);
expect(wrapper.find(OverlayTrigger)).toExist();
});
it('returns correct embed code', () => {

View File

@@ -61,8 +61,8 @@ describe('ExploreChartHeader', () => {
});
it('renders', () => {
expect(wrapper.find(EditableTitle)).toHaveLength(1);
expect(wrapper.find(ExploreActionButtons)).toHaveLength(1);
expect(wrapper.find(EditableTitle)).toExist();
expect(wrapper.find(ExploreActionButtons)).toExist();
});
it('should update title but not save', () => {

View File

@@ -68,15 +68,15 @@ describe('ExploreViewContainer', () => {
});
it('renders QueryAndSaveButtons', () => {
expect(wrapper.find(QueryAndSaveBtns)).toHaveLength(1);
expect(wrapper.find(QueryAndSaveBtns)).toExist();
});
it('renders ControlPanelsContainer', () => {
expect(wrapper.find(ControlPanelsContainer)).toHaveLength(1);
expect(wrapper.find(ControlPanelsContainer)).toExist();
});
it('renders ChartContainer', () => {
expect(wrapper.find(ChartContainer)).toHaveLength(1);
expect(wrapper.find(ChartContainer)).toExist();
});
describe('componentWillReceiveProps()', () => {

View File

@@ -45,7 +45,7 @@ describe('FilterBoxItemControl', () => {
});
it('renders an OverlayTrigger', () => {
expect(wrapper.find(OverlayTrigger)).toHaveLength(1);
expect(wrapper.find(OverlayTrigger)).toExist();
});
it('renderForms does the job', () => {

View File

@@ -37,14 +37,14 @@ describe('FilterDefinitionOption', () => {
const wrapper = shallow(
<FilterDefinitionOption option={{ column_name: 'a_column' }} />,
);
expect(wrapper.find(ColumnOption)).toHaveLength(1);
expect(wrapper.find(ColumnOption)).toExist();
});
it('renders a AdhocMetricStaticOption given an adhoc metric', () => {
const wrapper = shallow(
<FilterDefinitionOption option={sumValueAdhocMetric} />,
);
expect(wrapper.find(AdhocMetricStaticOption)).toHaveLength(1);
expect(wrapper.find(AdhocMetricStaticOption)).toExist();
});
it('renders the metric name given a saved metric', () => {

View File

@@ -37,7 +37,7 @@ describe('FixedOrMetricControl', () => {
});
it('renders a TextControl and a SelectControl', () => {
expect(wrapper.find(TextControl)).toHaveLength(1);
expect(wrapper.find(MetricsControl)).toHaveLength(1);
expect(wrapper.find(TextControl)).toExist();
expect(wrapper.find(MetricsControl)).toExist();
});
});

View File

@@ -36,16 +36,16 @@ describe('MetricDefinitionOption', () => {
it('renders a MetricOption given a saved metric', () => {
const wrapper = setup({ option: { metric_name: 'a_saved_metric' } });
expect(wrapper.find(MetricOption)).toHaveLength(1);
expect(wrapper.find(MetricOption)).toExist();
});
it('renders a ColumnOption given a column', () => {
const wrapper = setup({ option: { column_name: 'a_column' } });
expect(wrapper.find(ColumnOption)).toHaveLength(1);
expect(wrapper.find(ColumnOption)).toExist();
});
it('renders an AggregateOption given an aggregate metric', () => {
const wrapper = setup({ option: { aggregate_name: 'an_aggregate' } });
expect(wrapper.find(AggregateOption)).toHaveLength(1);
expect(wrapper.find(AggregateOption)).toExist();
});
});

View File

@@ -36,7 +36,7 @@ describe('MetricDefinitionValue', () => {
const wrapper = shallow(
<MetricDefinitionValue option={{ metric_name: 'a_saved_metric' }} />,
);
expect(wrapper.find(MetricOption)).toHaveLength(1);
expect(wrapper.find(MetricOption)).toExist();
});
it('renders an AdhocMetricOption given an adhoc metric', () => {
@@ -46,6 +46,6 @@ describe('MetricDefinitionValue', () => {
option={sumValueAdhocMetric}
/>,
);
expect(wrapper.find(AdhocMetricOption)).toHaveLength(1);
expect(wrapper.find(AdhocMetricOption)).toExist();
});
});

View File

@@ -65,7 +65,7 @@ const sumValueAdhocMetric = new AdhocMetric({
describe('MetricsControl', () => {
it('renders an OnPasteSelect', () => {
const { wrapper } = setup();
expect(wrapper.find(OnPasteSelect)).toHaveLength(1);
expect(wrapper.find(OnPasteSelect)).toExist();
});
describe('constructor', () => {

View File

@@ -36,8 +36,8 @@ describe('RowCountLabel', () => {
});
it('renders a Label and a TooltipWrapper', () => {
const wrapper = shallow(<RowCountLabel {...defaultProps} />);
expect(wrapper.find(Label)).toHaveLength(1);
expect(wrapper.find(TooltipWrapper)).toHaveLength(1);
expect(wrapper.find(Label)).toExist();
expect(wrapper.find(TooltipWrapper)).toExist();
});
it('renders a danger when limit is reached', () => {
const props = {

View File

@@ -45,6 +45,6 @@ describe('RunQueryActionButton', () => {
});
it('renders a single Button', () => {
expect(wrapper.find(Button)).toHaveLength(1);
expect(wrapper.find(Button)).toExist();
});
});

View File

@@ -75,8 +75,8 @@ describe('SaveModal', () => {
it('renders a Modal with the right set of components', () => {
const wrapper = getWrapper();
expect(wrapper.find(Modal)).toHaveLength(1);
expect(wrapper.find(FormControl)).toHaveLength(1);
expect(wrapper.find(Modal)).toExist();
expect(wrapper.find(FormControl)).toExist();
expect(wrapper.find(Button)).toHaveLength(3);
expect(wrapper.find(Radio)).toHaveLength(2);
});

View File

@@ -48,19 +48,19 @@ describe('SelectControl', () => {
});
it('renders with Select by default', () => {
expect(wrapper.find(OnPasteSelect)).toHaveLength(0);
expect(wrapper.find(OnPasteSelect)).not.toExist();
expect(wrapper.findWhere(x => x.type() === Select)).toHaveLength(1);
});
it('renders with OnPasteSelect when multi', () => {
wrapper.setProps({ multi: true });
expect(wrapper.find(OnPasteSelect)).toHaveLength(1);
expect(wrapper.find(OnPasteSelect)).toExist();
expect(wrapper.findWhere(x => x.type() === Select)).toHaveLength(0);
});
it('renders with Creatable when freeForm', () => {
wrapper.setProps({ freeForm: true });
expect(wrapper.find(OnPasteSelect)).toHaveLength(0);
expect(wrapper.find(OnPasteSelect)).not.toExist();
expect(wrapper.findWhere(x => x.type() === CreatableSelect)).toHaveLength(
1,
);

View File

@@ -38,7 +38,7 @@ describe('SelectControl', () => {
});
it('renders a FormControl', () => {
expect(wrapper.find(FormControl)).toHaveLength(1);
expect(wrapper.find(FormControl)).toExist();
});
it('calls onChange when toggled', () => {
@@ -51,7 +51,7 @@ describe('SelectControl', () => {
const props = { ...defaultProps };
props.language = 'markdown';
wrapper = shallow(<TextAreaControl {...props} />);
expect(wrapper.find(FormControl)).toHaveLength(0);
expect(wrapper.find(AceEditor)).toHaveLength(1);
expect(wrapper.find(FormControl)).not.toExist();
expect(wrapper.find(AceEditor)).toExist();
});
});

View File

@@ -39,7 +39,7 @@ describe('SelectControl', () => {
});
it('renders an OverlayTrigger', () => {
expect(wrapper.find(OverlayTrigger)).toHaveLength(1);
expect(wrapper.find(OverlayTrigger)).toExist();
});
it('renders an Popover', () => {

View File

@@ -46,7 +46,7 @@ describe('ViewportControl', () => {
it('renders a OverlayTrigger', () => {
const controlHeader = wrapper.find(ControlHeader);
expect(controlHeader).toHaveLength(1);
expect(wrapper.find(OverlayTrigger)).toHaveLength(1);
expect(wrapper.find(OverlayTrigger)).toExist();
});
it('renders a Popover with 5 TextControl', () => {

View File

@@ -55,7 +55,7 @@ describe('VizTypeControl', () => {
});
it('renders a Modal', () => {
expect(wrapper.find(Modal)).toHaveLength(1);
expect(wrapper.find(Modal)).toExist();
});
it('calls onChange when toggled', () => {
@@ -66,6 +66,6 @@ describe('VizTypeControl', () => {
it('filters images based on text input', () => {
expect(wrapper.find('img')).toHaveLength(2);
wrapper.setState({ filter: 'vis2' });
expect(wrapper.find('img')).toHaveLength(1);
expect(wrapper.find('img')).toExist();
});
});