Skip to content

Commit

Permalink
[Endpoint] add new policy fields (elastic#67323)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlog authored Jun 2, 2020
1 parent 8373eb8 commit 7da774f
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { sendGetEndpointSpecificDatasources } from './services/ingest';
import { isOnPolicyListPage, urlSearchParams } from './selectors';
import { ImmutableMiddlewareFactory } from '../../../../../common/store';
import { Immutable } from '../../../../../../common/endpoint/types';
import { initialPolicyListState } from './reducer';

export const policyListMiddlewareFactory: ImmutableMiddlewareFactory<Immutable<PolicyListState>> = (
coreStart
Expand All @@ -19,6 +20,7 @@ export const policyListMiddlewareFactory: ImmutableMiddlewareFactory<Immutable<P
next(action);

const state = getState();
const initialState = initialPolicyListState();
if (action.type === 'userChangedUrl' && isOnPolicyListPage(state)) {
const { page_index: pageIndex, page_size: pageSize } = urlSearchParams(state);
let response: GetPolicyListResponse;
Expand All @@ -38,15 +40,13 @@ export const policyListMiddlewareFactory: ImmutableMiddlewareFactory<Immutable<P
return;
}

const { items: policyItems, total } = response;

dispatch({
type: 'serverReturnedPolicyListData',
payload: {
policyItems,
policyItems: response ? response.items : initialState.policyItems,
pageIndex,
pageSize,
total,
total: response ? response.total : initialState.total,
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { GetPolicyListResponse } from '../../types';
import { EndpointDocGenerator } from '../../../../../../common/endpoint/generate_data';

export const mockPolicyResultList: (options?: {
total?: number;
request_page_size?: number;
request_page_index?: number;
}) => GetPolicyListResponse = (options = {}) => {
const {
total = 1,
request_page_size: requestPageSize = 10,
request_page_index: requestPageIndex = 0,
} = options;

// Skip any that are before the page we're on
const numberToSkip = requestPageSize * requestPageIndex;

// total - numberToSkip is the count of non-skipped ones, but return no more than a pageSize, and no less than 0
const actualCountToReturn = Math.max(Math.min(total - numberToSkip, requestPageSize), 0);

const policies = [];
for (let index = 0; index < actualCountToReturn; index++) {
const generator = new EndpointDocGenerator('seed');
policies.push(generator.generatePolicyDatasource());
}
const mock: GetPolicyListResponse = {
items: policies,
total,
page: requestPageIndex,
perPage: requestPageSize,
success: true,
};
return mock;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import * as reactTestingLibrary from '@testing-library/react';

import { PolicyList } from './index';
import { mockPolicyResultList } from '../store/policy_list/mock_policy_result_list';
import { AppContextTestRender, createAppRootMockRenderer } from '../../../../common/mock/endpoint';
import { AppAction } from '../../../../common/store/actions';

describe('when on the policies page', () => {
let render: () => ReturnType<AppContextTestRender['render']>;
let history: AppContextTestRender['history'];
let store: AppContextTestRender['store'];

beforeEach(() => {
const mockedContext = createAppRootMockRenderer();
({ history, store } = mockedContext);
render = () => mockedContext.render(<PolicyList />);
});

it('should show a table', async () => {
const renderResult = render();
const table = await renderResult.findByTestId('policyTable');
expect(table).not.toBeNull();
});

describe('when list data loads', () => {
let firstPolicyID: string;
beforeEach(() => {
reactTestingLibrary.act(() => {
history.push('/management/policy');
reactTestingLibrary.act(() => {
const policyListData = mockPolicyResultList({ total: 3 });
firstPolicyID = policyListData.items[0].id;
const action: AppAction = {
type: 'serverReturnedPolicyListData',
payload: {
policyItems: policyListData.items,
total: policyListData.total,
pageSize: policyListData.perPage,
pageIndex: policyListData.page,
},
};
store.dispatch(action);
});
});
});
it('should display rows in the table', async () => {
const renderResult = render();
const rows = await renderResult.findAllByRole('row');
expect(rows).toHaveLength(4);
});
it('should display policy name value as a link', async () => {
const renderResult = render();
const policyNameLink = (await renderResult.findAllByTestId('policyNameLink'))[0];
expect(policyNameLink).not.toBeNull();
expect(policyNameLink.getAttribute('href')).toContain(`policy/${firstPolicyID}`);
});
});
});
Loading

0 comments on commit 7da774f

Please sign in to comment.