diff --git a/frontend/src/container/ListOfDashboard/SearchFilter/__tests__/utils.test.ts b/frontend/src/container/ListOfDashboard/SearchFilter/__tests__/utils.test.ts new file mode 100644 index 0000000000..db9825b677 --- /dev/null +++ b/frontend/src/container/ListOfDashboard/SearchFilter/__tests__/utils.test.ts @@ -0,0 +1,58 @@ +import { Dashboard } from 'types/api/dashboard/getAll'; +import { v4 as uuid } from 'uuid'; + +import { TOperator } from '../types'; +import { executeSearchQueries } from '../utils'; + +describe('executeSearchQueries', () => { + const firstDashboard: Dashboard = { + id: 11111, + uuid: uuid(), + created_at: '', + updated_at: '', + data: { + title: 'first dashboard', + }, + }; + const secondDashboard: Dashboard = { + id: 22222, + uuid: uuid(), + created_at: '', + updated_at: '', + data: { + title: 'second dashboard', + }, + }; + const thirdDashboard: Dashboard = { + id: 333333, + uuid: uuid(), + created_at: '', + updated_at: '', + data: { + title: 'third dashboard (with special characters +?\\)', + }, + }; + const dashboards = [firstDashboard, secondDashboard, thirdDashboard]; + + it('should filter dashboards based on title', () => { + const query = { + category: 'title', + id: 'someid', + operator: '=' as TOperator, + value: 'first dashboard', + }; + + expect(executeSearchQueries([query], dashboards)).toEqual([firstDashboard]); + }); + + it('should filter dashboards with special characters', () => { + const query = { + category: 'title', + id: 'someid', + operator: '=' as TOperator, + value: 'third dashboard (with special characters +?\\)', + }; + + expect(executeSearchQueries([query], dashboards)).toEqual([thirdDashboard]); + }); +}); diff --git a/frontend/src/container/ListOfDashboard/SearchFilter/utils.ts b/frontend/src/container/ListOfDashboard/SearchFilter/utils.ts index 5f9b37cc3e..6487ccd789 100644 --- a/frontend/src/container/ListOfDashboard/SearchFilter/utils.ts +++ b/frontend/src/container/ListOfDashboard/SearchFilter/utils.ts @@ -42,6 +42,8 @@ export const executeSearchQueries = ( if (!searchData.length || !queries.length) { return searchData; } + const escapeRegExp = (regExp: string): string => + regExp.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); queries.forEach((query: IQueryStructure) => { const { operator } = query; @@ -61,7 +63,7 @@ export const executeSearchQueries = ( for (const searchSpaceItem of searchSpace) { if (searchSpaceItem) for (const queryValue of value) { - if (searchSpaceItem.match(queryValue)) { + if (searchSpaceItem.match(escapeRegExp(queryValue))) { return resolveOperator(true, operator); } }