Skip to content

Commit

Permalink
Fix broken pagination in Remote Clusters table.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcenizal committed May 9, 2020
1 parent 4dbdd12 commit edbd9c1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export const setup = props => {
remoteClusterLink.simulate('click');
};

const clickPaginationNextButton = () => {
testBed.find('remoteClusterListTable.pagination-button-next').simulate('click');
};

return {
...testBed,
actions: {
Expand All @@ -77,6 +81,7 @@ export const setup = props => {
clickRowActionButtonAt,
clickConfirmModalDeleteRemoteCluster,
clickRemoteClusterAt,
clickPaginationNextButton,
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,52 @@ describe('<RemoteClusterList />', () => {
});
});

describe('when there are multiple pages of remote clusters', () => {
let table;
let actions;
let waitFor;
let form;
let component;

const remoteClusters = [
{
name: 'unique',
seeds: [],
},
];

for (let i = 0; i < 29; i++) {
remoteClusters.push({
name: `name${i}`,
seeds: [],
});
}

beforeEach(async () => {
httpRequestsMockHelpers.setLoadRemoteClustersResponse(remoteClusters);

await act(async () => {
({ component, table, actions, waitFor, form } = setup());
await waitFor('remoteClusterListTable');
});
});

test('pagination works', () => {
actions.clickPaginationNextButton();
const { tableCellsValues } = table.getMetaData('remoteClusterListTable');

// Pagination defaults to 20 remote clusters per page. We loaded 30 remote clusters,
// so the second page should have 10.
expect(tableCellsValues.length).toBe(10);
});

test('search works', () => {
form.setInputValue(component.find('input[type="search"]'), 'unique');
const { tableCellsValues } = table.getMetaData('remoteClusterListTable');
expect(tableCellsValues.length).toBe(1);
});
});

describe('when there are remote clusters', () => {
let find;
let exists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ import { PROXY_MODE } from '../../../../../common/constants';
import { getRouterLinkProps, trackUiMetric, METRIC_TYPE } from '../../../services';
import { ConnectionStatus, RemoveClusterButtonProvider } from '../components';

const getFilteredClusters = (clusters, queryText) => {
if (queryText) {
const normalizedSearchText = queryText.toLowerCase();

return clusters.filter(cluster => {
const { name, seeds } = cluster;
const normalizedName = name.toLowerCase();
if (normalizedName.toLowerCase().includes(normalizedSearchText)) {
return true;
}

return seeds.some(seed => seed.includes(normalizedSearchText));
});
} else {
return clusters;
}
};

export class RemoteClusterTable extends Component {
static propTypes = {
clusters: PropTypes.array,
Expand All @@ -35,46 +53,47 @@ export class RemoteClusterTable extends Component {
clusters: [],
};

static getDerivedStateFromProps(props, state) {
const { clusters } = props;
const { prevClusters, queryText } = state;

// If a remote cluster gets deleted, we need to recreate the cached filtered clusters.
if (prevClusters !== clusters) {
return {
prevClusters: clusters,
filteredClusters: getFilteredClusters(clusters, queryText),
};
}

return null;
}

constructor(props) {
super(props);

this.state = {
queryText: undefined,
prevClusters: props.clusters,
selectedItems: [],
filteredClusters: props.clusters,
queryText: '',
};
}

onSearch = ({ query }) => {
const { clusters } = this.props;
const { text } = query;
const normalizedSearchText = text.toLowerCase();

// We cache the filtered indices instead of calculating them inside render() because
// of https://github.com/elastic/eui/issues/3445.
this.setState({
queryText: normalizedSearchText,
queryText: text,
filteredClusters: getFilteredClusters(clusters, text),
});
};

getFilteredClusters = () => {
const { clusters } = this.props;
const { queryText } = this.state;

if (queryText) {
return clusters.filter(cluster => {
const { name, seeds } = cluster;
const normalizedName = name.toLowerCase();
if (normalizedName.toLowerCase().includes(queryText)) {
return true;
}

return seeds.some(seed => seed.includes(queryText));
});
} else {
return clusters.slice(0);
}
};

render() {
const { openDetailPanel } = this.props;

const { selectedItems } = this.state;
const { selectedItems, filteredClusters } = this.state;

const columns = [
{
Expand Down Expand Up @@ -327,8 +346,6 @@ export class RemoteClusterTable extends Component {
selectable: ({ isConfiguredByNode }) => !isConfiguredByNode,
};

const filteredClusters = this.getFilteredClusters();

return (
<EuiInMemoryTable
items={filteredClusters}
Expand Down

0 comments on commit edbd9c1

Please sign in to comment.