2020-01-16 00:48:55 -05:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
import React from 'react';
|
2020-03-13 15:35:00 -04:00
|
|
|
import { mount, shallow } from 'enzyme';
|
2020-01-16 00:48:55 -05:00
|
|
|
import { act } from 'react-dom/test-utils';
|
|
|
|
import { MenuItem, Pagination } from 'react-bootstrap';
|
2020-04-13 16:39:55 -04:00
|
|
|
import Select from 'react-select';
|
2020-01-16 00:48:55 -05:00
|
|
|
|
|
|
|
import ListView from 'src/components/ListView/ListView';
|
2020-04-13 16:39:55 -04:00
|
|
|
import ListViewFilters from 'src/components/ListView/Filters';
|
2020-04-01 12:20:17 -04:00
|
|
|
import ListViewPagination from 'src/components/ListView/Pagination';
|
2020-03-13 15:35:00 -04:00
|
|
|
import { areArraysShallowEqual } from 'src/reduxUtils';
|
2020-01-16 00:48:55 -05:00
|
|
|
|
2020-04-13 16:39:55 -04:00
|
|
|
const mockedProps = {
|
|
|
|
title: 'Data Table',
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
accessor: 'id',
|
|
|
|
Header: 'ID',
|
|
|
|
sortable: true,
|
|
|
|
},
|
2020-04-21 15:04:11 -04:00
|
|
|
{
|
|
|
|
accessor: 'age',
|
|
|
|
Header: 'Age',
|
|
|
|
},
|
2020-04-13 16:39:55 -04:00
|
|
|
{
|
|
|
|
accessor: 'name',
|
|
|
|
Header: 'Name',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
Header: 'Name',
|
|
|
|
id: 'name',
|
|
|
|
operators: [{ label: 'Starts With', value: 'sw' }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
data: [
|
|
|
|
{ id: 1, name: 'data 1' },
|
|
|
|
{ id: 2, name: 'data 2' },
|
|
|
|
],
|
|
|
|
count: 2,
|
|
|
|
pageSize: 1,
|
|
|
|
fetchData: jest.fn(() => []),
|
|
|
|
loading: false,
|
|
|
|
bulkActions: [{ name: 'do something', onSelect: jest.fn() }],
|
|
|
|
};
|
|
|
|
|
2020-01-16 00:48:55 -05:00
|
|
|
describe('ListView', () => {
|
|
|
|
const wrapper = mount(<ListView {...mockedProps} />);
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mockedProps.fetchData.mockClear();
|
2020-01-27 13:23:41 -05:00
|
|
|
mockedProps.bulkActions.forEach(ba => {
|
|
|
|
ba.onSelect.mockClear();
|
|
|
|
});
|
2020-01-16 00:48:55 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls fetchData on mount', () => {
|
|
|
|
expect(wrapper.find(ListView)).toHaveLength(1);
|
|
|
|
expect(mockedProps.fetchData.mock.calls[0]).toMatchInlineSnapshot(`
|
2020-03-13 15:35:00 -04:00
|
|
|
Array [
|
|
|
|
Object {
|
|
|
|
"filters": Array [],
|
|
|
|
"pageIndex": 0,
|
|
|
|
"pageSize": 1,
|
|
|
|
"sortBy": Array [],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`);
|
2020-01-16 00:48:55 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls fetchData on sort', () => {
|
|
|
|
wrapper
|
|
|
|
.find('[data-test="sort-header"]')
|
2020-01-27 13:23:41 -05:00
|
|
|
.at(1)
|
2020-01-16 00:48:55 -05:00
|
|
|
.simulate('click');
|
2020-01-27 13:23:41 -05:00
|
|
|
|
|
|
|
expect(mockedProps.fetchData).toHaveBeenCalled();
|
2020-01-16 00:48:55 -05:00
|
|
|
expect(mockedProps.fetchData.mock.calls[0]).toMatchInlineSnapshot(`
|
2020-03-13 15:35:00 -04:00
|
|
|
Array [
|
|
|
|
Object {
|
|
|
|
"filters": Array [],
|
|
|
|
"pageIndex": 0,
|
|
|
|
"pageSize": 1,
|
|
|
|
"sortBy": Array [
|
|
|
|
Object {
|
|
|
|
"desc": false,
|
|
|
|
"id": "id",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`);
|
2020-01-16 00:48:55 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls fetchData on filter', () => {
|
|
|
|
act(() => {
|
|
|
|
wrapper
|
|
|
|
.find('.dropdown-toggle')
|
|
|
|
.children('button')
|
2020-01-27 13:23:41 -05:00
|
|
|
.at(0)
|
2020-01-16 00:48:55 -05:00
|
|
|
.props()
|
|
|
|
.onClick();
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
.find(MenuItem)
|
2020-01-27 13:23:41 -05:00
|
|
|
.at(0)
|
2020-01-16 00:48:55 -05:00
|
|
|
.props()
|
|
|
|
.onSelect({ id: 'name', Header: 'name' });
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
wrapper.find('.filter-inputs input[type="text"]').prop('onChange')({
|
2020-02-24 13:16:11 -05:00
|
|
|
persist() {},
|
2020-01-16 00:48:55 -05:00
|
|
|
currentTarget: { value: 'foo' },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
wrapper
|
|
|
|
.find('[data-test="apply-filters"]')
|
|
|
|
.last()
|
|
|
|
.prop('onClick')();
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
expect(mockedProps.fetchData.mock.calls[0]).toMatchInlineSnapshot(`
|
2020-04-13 16:39:55 -04:00
|
|
|
Array [
|
|
|
|
Object {
|
|
|
|
"filters": Array [
|
|
|
|
Object {
|
|
|
|
"id": "name",
|
|
|
|
"operator": "sw",
|
|
|
|
"value": "foo",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"pageIndex": 0,
|
|
|
|
"pageSize": 1,
|
|
|
|
"sortBy": Array [
|
|
|
|
Object {
|
|
|
|
"desc": false,
|
|
|
|
"id": "id",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`);
|
2020-01-16 00:48:55 -05:00
|
|
|
});
|
2020-04-13 16:39:55 -04:00
|
|
|
|
2020-04-01 12:20:17 -04:00
|
|
|
it('renders pagination controls', () => {
|
|
|
|
expect(wrapper.find(Pagination).exists()).toBe(true);
|
|
|
|
expect(wrapper.find(Pagination.Prev).exists()).toBe(true);
|
|
|
|
expect(wrapper.find(Pagination.Item).exists()).toBe(true);
|
|
|
|
expect(wrapper.find(Pagination.Next).exists()).toBe(true);
|
|
|
|
});
|
2020-04-13 16:39:55 -04:00
|
|
|
|
2020-01-16 00:48:55 -05:00
|
|
|
it('calls fetchData on page change', () => {
|
|
|
|
act(() => {
|
2020-04-01 12:20:17 -04:00
|
|
|
wrapper.find(ListViewPagination).prop('onChange')(2);
|
2020-01-16 00:48:55 -05:00
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
expect(mockedProps.fetchData.mock.calls[0]).toMatchInlineSnapshot(`
|
2020-04-13 16:39:55 -04:00
|
|
|
Array [
|
|
|
|
Object {
|
|
|
|
"filters": Array [
|
|
|
|
Object {
|
|
|
|
"id": "name",
|
|
|
|
"operator": "sw",
|
|
|
|
"value": "foo",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"pageIndex": 1,
|
|
|
|
"pageSize": 1,
|
|
|
|
"sortBy": Array [
|
|
|
|
Object {
|
|
|
|
"desc": false,
|
|
|
|
"id": "id",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`);
|
2020-01-27 13:23:41 -05:00
|
|
|
});
|
2020-04-13 16:39:55 -04:00
|
|
|
|
2020-01-27 13:23:41 -05:00
|
|
|
it('handles bulk actions on 1 row', () => {
|
|
|
|
act(() => {
|
|
|
|
wrapper
|
|
|
|
.find('input[title="Toggle Row Selected"]')
|
|
|
|
.at(0)
|
|
|
|
.prop('onChange')({ target: { value: 'on' } });
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
.find('.dropdown-toggle')
|
|
|
|
.children('button')
|
|
|
|
.at(1)
|
|
|
|
.props()
|
|
|
|
.onClick();
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
const bulkActionsProps = wrapper
|
|
|
|
.find(MenuItem)
|
|
|
|
.last()
|
|
|
|
.props();
|
|
|
|
|
|
|
|
bulkActionsProps.onSelect(bulkActionsProps.eventKey);
|
|
|
|
expect(mockedProps.bulkActions[0].onSelect.mock.calls[0])
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-03-13 15:35:00 -04:00
|
|
|
Array [
|
|
|
|
Array [
|
|
|
|
Object {
|
|
|
|
"id": 1,
|
|
|
|
"name": "data 1",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]
|
|
|
|
`);
|
2020-01-27 13:23:41 -05:00
|
|
|
});
|
2020-04-13 16:39:55 -04:00
|
|
|
|
2020-01-27 13:23:41 -05:00
|
|
|
it('handles bulk actions on all rows', () => {
|
|
|
|
act(() => {
|
|
|
|
wrapper
|
|
|
|
.find('input[title="Toggle All Rows Selected"]')
|
|
|
|
.at(0)
|
|
|
|
.prop('onChange')({ target: { value: 'on' } });
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
.find('.dropdown-toggle')
|
|
|
|
.children('button')
|
|
|
|
.at(1)
|
|
|
|
.props()
|
|
|
|
.onClick();
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
const bulkActionsProps = wrapper
|
|
|
|
.find(MenuItem)
|
|
|
|
.last()
|
|
|
|
.props();
|
|
|
|
|
|
|
|
bulkActionsProps.onSelect(bulkActionsProps.eventKey);
|
|
|
|
expect(mockedProps.bulkActions[0].onSelect.mock.calls[0])
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-03-13 15:35:00 -04:00
|
|
|
Array [
|
|
|
|
Array [
|
|
|
|
Object {
|
|
|
|
"id": 1,
|
|
|
|
"name": "data 1",
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"id": 2,
|
|
|
|
"name": "data 2",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]
|
|
|
|
`);
|
|
|
|
});
|
2020-04-13 16:39:55 -04:00
|
|
|
|
2020-03-13 15:35:00 -04:00
|
|
|
it('Throws an exception if filter missing in columns', () => {
|
|
|
|
expect.assertions(1);
|
|
|
|
const props = {
|
|
|
|
...mockedProps,
|
|
|
|
filters: [...mockedProps.filters, { id: 'some_column' }],
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
shallow(<ListView {...props} />);
|
|
|
|
} catch (e) {
|
|
|
|
expect(e).toMatchInlineSnapshot(
|
|
|
|
`[ListViewError: Invalid filter config, some_column is not present in columns]`,
|
|
|
|
);
|
|
|
|
}
|
2020-01-16 00:48:55 -05:00
|
|
|
});
|
|
|
|
});
|
2020-04-13 16:39:55 -04:00
|
|
|
|
|
|
|
describe('ListView with new UI filters', () => {
|
2020-04-21 15:04:11 -04:00
|
|
|
const fetchSelectsMock = jest.fn(() => []);
|
2020-04-13 16:39:55 -04:00
|
|
|
const newFiltersProps = {
|
|
|
|
...mockedProps,
|
|
|
|
useNewUIFilters: true,
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
Header: 'ID',
|
|
|
|
id: 'id',
|
|
|
|
input: 'select',
|
|
|
|
selects: [{ label: 'foo', value: 'bar' }],
|
|
|
|
operator: 'eq',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Header: 'Name',
|
|
|
|
id: 'name',
|
|
|
|
input: 'search',
|
|
|
|
operator: 'ct',
|
|
|
|
},
|
2020-04-21 15:04:11 -04:00
|
|
|
{
|
|
|
|
Header: 'Age',
|
|
|
|
id: 'age',
|
|
|
|
input: 'select',
|
|
|
|
fetchSelects: fetchSelectsMock,
|
|
|
|
operator: 'eq',
|
|
|
|
},
|
2020-04-13 16:39:55 -04:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(<ListView {...newFiltersProps} />);
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mockedProps.fetchData.mockClear();
|
|
|
|
mockedProps.bulkActions.forEach(ba => {
|
|
|
|
ba.onSelect.mockClear();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders UI filters', () => {
|
|
|
|
expect(wrapper.find(ListViewFilters)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
2020-04-21 15:04:11 -04:00
|
|
|
it('fetched selects if function is provided', () => {
|
|
|
|
expect(fetchSelectsMock).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2020-04-13 16:39:55 -04:00
|
|
|
it('calls fetchData on filter', () => {
|
|
|
|
act(() => {
|
|
|
|
wrapper
|
|
|
|
.find('[data-test="filters-select"]')
|
2020-04-21 15:04:11 -04:00
|
|
|
.first()
|
2020-04-13 16:39:55 -04:00
|
|
|
.props()
|
|
|
|
.onChange({ value: 'bar' });
|
|
|
|
});
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
wrapper
|
|
|
|
.find('[data-test="filters-search"]')
|
2020-04-21 15:04:11 -04:00
|
|
|
.first()
|
2020-04-13 16:39:55 -04:00
|
|
|
.props()
|
|
|
|
.onChange({ currentTarget: { value: 'something' } });
|
|
|
|
});
|
|
|
|
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
wrapper
|
|
|
|
.find('[data-test="filters-search"]')
|
|
|
|
.last()
|
|
|
|
.props()
|
|
|
|
.onBlur();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(newFiltersProps.fetchData.mock.calls[0]).toMatchInlineSnapshot(`
|
2020-04-21 15:04:11 -04:00
|
|
|
Array [
|
|
|
|
Object {
|
|
|
|
"filters": Array [
|
|
|
|
Object {
|
|
|
|
"id": "id",
|
|
|
|
"operator": "eq",
|
|
|
|
"value": "bar",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"pageIndex": 0,
|
|
|
|
"pageSize": 1,
|
|
|
|
"sortBy": Array [],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`);
|
2020-04-13 16:39:55 -04:00
|
|
|
|
|
|
|
expect(newFiltersProps.fetchData.mock.calls[1]).toMatchInlineSnapshot(`
|
2020-04-21 15:04:11 -04:00
|
|
|
Array [
|
|
|
|
Object {
|
|
|
|
"filters": Array [
|
|
|
|
Object {
|
|
|
|
"id": "id",
|
|
|
|
"operator": "eq",
|
|
|
|
"value": "bar",
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"id": "name",
|
|
|
|
"operator": "ct",
|
|
|
|
"value": "something",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"pageIndex": 0,
|
|
|
|
"pageSize": 1,
|
|
|
|
"sortBy": Array [],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`);
|
2020-04-13 16:39:55 -04:00
|
|
|
});
|
|
|
|
});
|