Skip to content

Commit

Permalink
fix(RecsListTable): Fixing the category sorting (#263)
Browse files Browse the repository at this point in the history
* fix(RecsListTable): Fixing the category sorting

* added a test to check sorting

* Refactored the compare function

* Refactored the code
Updated the test

* Enchance rule data

* [RecsListTable] Fix category test

* Updated the test after the discussion with Iker
Added a comment that was requested by PM

Co-authored-by: Iker Reyes <[email protected]>
  • Loading branch information
Fewwy and ikerreyes authored May 13, 2022
1 parent c5355c0 commit 41a5c09
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cypress/fixtures/api/insights-results-aggregator/v2/rule.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"total_risk": 2,
"impact": 2,
"likelihood": 2,
"tags": ["openshift", "configuration", "service_availability"],
"tags": ["openshift", "configuration", "service_availability", "performance"],
"disabled": false,
"risk_of_change": 0,
"impacted_clusters_count": 42
Expand All @@ -47,7 +47,7 @@
"total_risk": 2,
"impact": 1,
"likelihood": 4,
"tags": ["service_availability", "vsphere", "disk"],
"tags": ["service_availability", "vsphere", "disk", "security"],
"disabled": false,
"risk_of_change": 0,
"impacted_clusters_count": 0
Expand Down
9 changes: 6 additions & 3 deletions src/Components/RecsListTable/RecsListTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,18 @@ const RecsListTable = ({ query }) => {
},
]);
};

/* the category sorting compares only the first element of the array.
Could be refactored later when we assign a priority numbers to each of the category
and sort them in the array based on the priority.
*/
const buildDisplayedRows = (rows, index, direction) => {
const sortingRows = [...rows].sort((firstItem, secondItem) => {
const d = direction === SortByDirection.asc ? 1 : -1;
const fst = firstItem[0].rule[RECS_LIST_COLUMNS_KEYS[index]];
const snd = secondItem[0].rule[RECS_LIST_COLUMNS_KEYS[index]];
if (index === 3) {
return extractCategories(fst)[0].localeCompare(
extractCategories(snd)[0]
return (
d * extractCategories(fst)[0].localeCompare(extractCategories(snd)[0])
);
}
return fst > snd ? d : snd > fst ? -d : 0;
Expand Down
25 changes: 19 additions & 6 deletions src/Components/RecsListTable/RecsListTable.spec.ct.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
itemsPerPage,
} from '../../../cypress/utils/pagination';
import { TOTAL_RISK, CATEGORIES } from '../../../cypress/utils/globals';
import { RECS_LIST_COLUMNS } from '../../AppConstants';
import { RECS_LIST_COLUMNS, RULE_CATEGORIES } from '../../AppConstants';
import {
checkRowCounts,
columnName2UrlParam,
Expand Down Expand Up @@ -368,10 +368,15 @@ describe('successful non-empty recommendations list table', () => {
});

describe('sorting', () => {
// TODO implement category sorting
_.zip(
['description', 'publish_date', 'total_risk', 'impacted_clusters_count'],
['Name', 'Modified', 'Total risk', 'Clusters'] // TODO use TABLE_HEADERS
[
'description',
'publish_date',
'tags',
'total_risk',
'impacted_clusters_count',
],
TABLE_HEADERS
).forEach(([category, label]) => {
SORTING_ORDERS.forEach((order) => {
it(`${order} by ${label}`, () => {
Expand Down Expand Up @@ -399,13 +404,21 @@ describe('successful non-empty recommendations list table', () => {
)
);
}

let orderIteratee = category;
if (category === 'tags') {
orderIteratee = (it) =>
_.first(
it.tags.filter((string) =>
Object.keys(RULE_CATEGORIES).includes(string)
)
);
}
// add property name to clusters
let sortedData = _.map(
// all tables must preserve original ordering
_.orderBy(
_.cloneDeep(filterData(DEFAULT_FILTERS)),
[category],
[orderIteratee],
[order === 'ascending' ? 'asc' : 'desc']
),
'description'
Expand Down

0 comments on commit 41a5c09

Please sign in to comment.