Skip to content

Commit

Permalink
Merge pull request #2366 from bjoernricks/fix-report-result-filter-de…
Browse files Browse the repository at this point in the history
…termination

Fix report result filter determination
  • Loading branch information
saberlynx authored Aug 6, 2020
2 parents 81e354e + ef85565 commit 8cc8f34
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Deleting a single entity now removes its ID from store [#1839](https://github.com/greenbone/gsa/pull/1839)

### Fixed
- Don't use stored result list page filter at report results tab [#2366](https://github.com/greenbone/gsa/pull/2366)
- Fixed flickering reports [#2359](https://github.com/greenbone/gsa/pull/2359)
- Fixed "Hosts scanned" in report details disappearing during page refresh [#2357](https://github.com/greenbone/gsa/pull/2357)
- Fixed schedule_periods not forwarded if there is no schedule [#2331](https://github.com/greenbone/gsa/pull/2331)
Expand Down
62 changes: 54 additions & 8 deletions gsa/src/web/entities/__tests__/filterprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('FilterProvider component tests', () => {

const locationQuery = {filter: 'location=query'};

const defaultSettingfilter = Filter.fromString('foo=bar');
const defaultSettingFilter = Filter.fromString('foo=bar');

const getSetting = jest.fn().mockResolvedValue({});
const gmp = {
Expand All @@ -50,7 +50,7 @@ describe('FilterProvider component tests', () => {

store.dispatch(loadingActions.success({rowsperpage: {value: '42'}}));
store.dispatch(
defaultFilterLoadingActions.success('task', defaultSettingfilter),
defaultFilterLoadingActions.success('task', defaultSettingFilter),
);

const renderFunc = jest
Expand All @@ -76,7 +76,7 @@ describe('FilterProvider component tests', () => {

const emptyFilter = Filter.fromString('rows=42');

const defaultSettingfilter = Filter.fromString('foo=bar');
const defaultSettingFilter = Filter.fromString('foo=bar');

const getSetting = jest.fn().mockResolvedValue({});
const gmp = {
Expand All @@ -93,7 +93,7 @@ describe('FilterProvider component tests', () => {

store.dispatch(loadingActions.success({rowsperpage: {value: '42'}}));
store.dispatch(
defaultFilterLoadingActions.success('task', defaultSettingfilter),
defaultFilterLoadingActions.success('task', defaultSettingFilter),
);
store.dispatch(pageFilter('task', pFilter));

Expand All @@ -111,10 +111,56 @@ describe('FilterProvider component tests', () => {
expect(renderFunc).not.toHaveBeenCalledWith({filter: emptyFilter});
});

test('should allow to set a pageName for loading the pageFilter', async () => {
const gmpName = 'task';
const pageName = 'foo-bar-baz';
const resultingFilter = Filter.fromString('page=filter rows=42');

const pFilter = Filter.fromString('page=filter');

const emptyFilter = Filter.fromString('rows=42');

const defaultSettingFilter = Filter.fromString('foo=bar');

const getSetting = jest.fn().mockResolvedValue({});
const gmp = {
user: {
getSetting,
},
};

const {render, store} = rendererWith({
gmp,
store: true,
router: true,
});

store.dispatch(loadingActions.success({rowsperpage: {value: '42'}}));
store.dispatch(
defaultFilterLoadingActions.success(gmpName, defaultSettingFilter),
);
store.dispatch(pageFilter(pageName, pFilter));

const renderFunc = jest
.fn()
.mockReturnValue(<span data-testid="awaiting-span" />);

const {getByTestId} = render(
<FilterProvider gmpname={gmpName} pageName={pageName}>
{renderFunc}
</FilterProvider>,
);

await waitForElement(() => getByTestId('awaiting-span'));

expect(renderFunc).toHaveBeenCalledWith({filter: resultingFilter});
expect(renderFunc).not.toHaveBeenCalledWith({filter: emptyFilter});
});

test('should use defaultSettingFilter', async () => {
const resultingFilter = Filter.fromString('foo=bar rows=42');

const defaultSettingfilter = Filter.fromString('foo=bar');
const defaultSettingFilter = Filter.fromString('foo=bar');

const emptyFilter = Filter.fromString('rows=42');

Expand All @@ -133,7 +179,7 @@ describe('FilterProvider component tests', () => {

store.dispatch(loadingActions.success({rowsperpage: {value: '42'}}));
store.dispatch(
defaultFilterLoadingActions.success('task', defaultSettingfilter),
defaultFilterLoadingActions.success('task', defaultSettingFilter),
);

const renderFunc = jest
Expand All @@ -150,7 +196,7 @@ describe('FilterProvider component tests', () => {
expect(renderFunc).not.toHaveBeenCalledWith({filter: emptyFilter});
});

test('should use fallbackfilter if defaultSettingFilter could not be loaded', async () => {
test('should use fallbackFilter if defaultSettingFilter could not be loaded', async () => {
const resultingFilter = Filter.fromString('fall=back rows=42');

const fallbackFilter = Filter.fromString('fall=back');
Expand Down Expand Up @@ -231,7 +277,7 @@ describe('FilterProvider component tests', () => {
});

test('should use default fallbackFilter as last resort', async () => {
const resultingFilter = DEFAULT_FALLBACK_FILTER.set('rows', 42);
const resultingFilter = DEFAULT_FALLBACK_FILTER.copy().set('rows', 42);

const getSetting = jest.fn().mockResolvedValue({});
const subscribe = jest.fn();
Expand Down
8 changes: 6 additions & 2 deletions gsa/src/web/entities/filterprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const FilterProvider = ({
children,
fallbackFilter,
gmpname,
pageName = gmpname,
locationQuery = {},
}) => {
const gmp = useGmp();
Expand Down Expand Up @@ -96,12 +97,14 @@ const FilterProvider = ({

useEffect(() => {
if (isDefined(locationQuery.filter)) {
dispatch(setPageFilter(gmpname, Filter.fromString(locationQuery.filter)));
dispatch(
setPageFilter(pageName, Filter.fromString(locationQuery.filter)),
);
}
setLocationQueryFilter(undefined);
}, []); // eslint-disable-line react-hooks/exhaustive-deps

const pageFilter = useSelector(state => getPage(state).getFilter(gmpname));
const pageFilter = useSelector(state => getPage(state).getFilter(pageName));

if (isDefined(locationQueryFilter)) {
returnedFilter = locationQueryFilter;
Expand Down Expand Up @@ -146,6 +149,7 @@ FilterProvider.propTypes = {
locationQuery: PropTypes.shape({
filter: PropTypes.string,
}),
pageName: PropTypes.string,
};

export default FilterProvider;
Expand Down
17 changes: 16 additions & 1 deletion gsa/src/web/pages/reports/detailspage.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ const load = ({
reportId,
// eslint-disable-next-line no-shadow
loadReportWithThreshold,
pageFilter,
reportFilter,
updateFilter,
}) => filter => {
Expand All @@ -714,6 +715,16 @@ const load = ({
filter = reportFilter;
}

if (!hasValue(filter)) {
// use filter from store
filter = pageFilter;
}

if (!hasValue(filter)) {
// use filter from user setting
filter = defaultFilter;
}

if (!hasValue(filter)) {
// use fallback filter
filter = DEFAULT_FILTER;
Expand All @@ -724,7 +735,11 @@ const load = ({
};

const ReportDetailsWrapper = ({reportFilter, ...props}) => (
<FilterProvider fallbackFilter={DEFAULT_FILTER} gmpname="result">
<FilterProvider
fallbackFilter={DEFAULT_FILTER}
gmpname="result"
pageName={`report-${props.reportId}`}
>
{({filter}) => (
<Reload
name={`report-${props.reportId}`}
Expand Down

0 comments on commit 8cc8f34

Please sign in to comment.