From 2f136ee08a27523f05fd74aeb30476644d527aae Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Mon, 4 Mar 2024 22:17:30 +0530 Subject: [PATCH] update test suite --- .../react-table/src/components/ReactTable.js | 6 +- .../src/components/SelectColumn.js | 5 +- .../react-table/src/tests/utils.test.js | 90 +++++++++---------- .../react-table/src/utils/utils.js | 62 ++++++------- 4 files changed, 78 insertions(+), 85 deletions(-) diff --git a/benchexec/tablegenerator/react-table/src/components/ReactTable.js b/benchexec/tablegenerator/react-table/src/components/ReactTable.js index fb039b649..f8fc194f4 100644 --- a/benchexec/tablegenerator/react-table/src/components/ReactTable.js +++ b/benchexec/tablegenerator/react-table/src/components/ReactTable.js @@ -591,7 +591,7 @@ const Table = (props) => { const value = sort.length ? sort : undefined; const prevParams = getHashSearch(); if (prevParams["sort"] !== value) { - setHashSearch({ sort: value }, { keepOthers: true }); + setHashSearch({ sort: value }, true); } }, [sortBy]); @@ -600,7 +600,7 @@ const Table = (props) => { const value = pageSize !== initialPageSize ? pageSize : undefined; const prevParams = getHashSearch(); if (prevParams["pageSize"] !== value) { - setHashSearch({ pageSize: value }, { keepOthers: true }); + setHashSearch({ pageSize: value }, true); } }, [pageSize]); @@ -610,7 +610,7 @@ const Table = (props) => { pageIndex && pageIndex !== 0 ? Number(pageIndex) + 1 : undefined; const prevParams = getHashSearch(); if (prevParams["page"] !== value) { - setHashSearch({ page: value }, { keepOthers: true }); + setHashSearch({ page: value }, true); } }, [pageIndex]); diff --git a/benchexec/tablegenerator/react-table/src/components/SelectColumn.js b/benchexec/tablegenerator/react-table/src/components/SelectColumn.js index 618c55539..bcf059821 100644 --- a/benchexec/tablegenerator/react-table/src/components/SelectColumn.js +++ b/benchexec/tablegenerator/react-table/src/components/SelectColumn.js @@ -67,10 +67,7 @@ export default class SelectColumn extends React.Component { hiddenParams["hidden"] = null; } - setHashSearch(hiddenParams, { - keepOthers: true, - history: this.props.history, - }); + setHashSearch(hiddenParams, true, this.props.history); } // -------------------------Rendering------------------------- diff --git a/benchexec/tablegenerator/react-table/src/tests/utils.test.js b/benchexec/tablegenerator/react-table/src/tests/utils.test.js index c0c9b441e..03276fef5 100644 --- a/benchexec/tablegenerator/react-table/src/tests/utils.test.js +++ b/benchexec/tablegenerator/react-table/src/tests/utils.test.js @@ -14,6 +14,7 @@ import { constructQueryString, decodeFilter, hasSameEntries, + getHashURL, makeFilterSerializer, makeFilterDeserializer, splitUrlPathForMatchingPrefix, @@ -127,54 +128,47 @@ describe("hashRouting helpers", () => { }); }); - // describe("setHashSearch", () => { - // test("should set hash search without side effects", () => { - // const params = { key1: "value1", key2: "value2" }; - // const queryString = constructQueryString(params); - // const hrefString = `http://localhost/?${queryString}`; - - // const updatedUrl = setHashSearch(params, { returnString: true }); - // expect(updatedUrl).toBe(hrefString); - // }); - - // test("should set hash search and update history", () => { - // const params = { key1: "value1", key2: "value2" }; - // const queryString = constructQueryString(params); - // const hrefString = `http://localhost/?${queryString}`; - - // const mockHistory = { - // push: jest.fn(), - // }; - - // setHashSearch(params, { history: mockHistory }); - // expect(mockHistory.push).toHaveBeenCalledWith(hrefString); - // }); - - // test("should merge params with existing ones if keepOthers is true", () => { - // const params = { key1: "value1", key2: "value2" }; - // const queryString = constructQueryString({ - // ...params, - // existingKey: "existingValue", - // }); - // const hrefString = `http://localhost/?${queryString}`; - - // const updatedUrl = setHashSearch(params, { - // keepOthers: true, - // returnString: true, - // }); - // expect(updatedUrl).toBe(hrefString); - // }); - - // test("should use the baseUrl if provided", () => { - // const params = { key1: "value1", key2: "value2" }; - // const baseUrl = "http://customurl.com"; - // const queryString = constructQueryString(params); - // const hrefString = `${baseUrl}?${queryString}`; - - // const updatedUrl = setHashSearch(params, { baseUrl, returnString: true }); - // expect(updatedUrl).toBe(hrefString); - // }); - // }); + describe("getHashURL", () => { + test("should construct URL hash with provided parameters", () => { + const baseUrl = "http://example.com"; + const params = { key1: "value1", key2: "value2" }; + const keepOthers = false; + + expect(getHashURL(baseUrl, params, keepOthers)).toEqual( + "http://example.com?key1=value1&key2=value2", + ); + }); + + test("should construct URL hash with provided parameters and keep others", () => { + const baseUrl = "http://example.com?existingKey=existingValue"; + const params = { key1: "value1", key2: "value2" }; + const keepOthers = true; + + expect(getHashURL(baseUrl, params, keepOthers)).toEqual( + "http://example.com?existingKey=existingValue&key1=value1&key2=value2", + ); + }); + + test("should return the same URL with exisiting params if no parameters are provided and keepOthers is true", () => { + const baseUrl = "http://example.com?exisitingKey=existingValue"; + const params = {}; + const keepOthers = true; + + expect(getHashURL(baseUrl, params, keepOthers)).toEqual( + "http://example.com?exisitingKey=existingValue", + ); + }); + + test("should return the same URL with no params if no parameters are provided and keepOthers is false", () => { + const baseUrl = "http://example.com?exisitingKey=existingValue"; + const params = {}; + const keepOthers = false; + + expect(getHashURL(baseUrl, params, keepOthers)).toEqual( + "http://example.com", + ); + }); + }); }); describe("decodeFilter", () => { diff --git a/benchexec/tablegenerator/react-table/src/utils/utils.js b/benchexec/tablegenerator/react-table/src/utils/utils.js index 7e6914ffe..0ee01dd5e 100644 --- a/benchexec/tablegenerator/react-table/src/utils/utils.js +++ b/benchexec/tablegenerator/react-table/src/utils/utils.js @@ -205,7 +205,7 @@ const EXTENDED_DISCRETE_COLOR_RANGE = [ /** * Parses the search parameters from the URL hash or a provided string. * - * @param {string} [str] - Optional string to parse. If not provided, parses the URL hash of the current document. + * @param {string} - Optional string to parse. If not provided, parses the URL hash of the current document. * @returns {Object} - An object containing the parsed search parameters. */ const getHashSearch = (str) => { @@ -241,38 +241,40 @@ export const constructQueryString = (params) => { .join("&"); }; +/** + * Constructs a URL hash from the provided parameters + * + * @param {string} url - The URL to be processed + * @param {Object} params - The parameters to be included in the hash + * @param {boolean} [keepOthers=false] - Whether to keep existing parameters in the URL hash or not + * @returns {string} - The constructed URL hash + */ +export const getHashURL = (url, params = {}, keepOthers = false) => { + const additionalParams = keepOthers ? getHashSearch(url) : {}; + const mergedParams = { ...additionalParams, ...params }; + + const queryString = constructQueryString(mergedParams); + const baseURL = url.split("?")[0]; + + return queryString.length > 0 ? `${baseURL}?${queryString}` : baseURL; +}; + /** * Sets or updates the search parameters in the URL hash of the current page. * - * @param {Object} params - The parameters to be set or updated in the URL hash. - * @param {Object} options - Additional settings for the operation. - * @param {boolean} [options.returnString=false] - Whether to return the modified URL string or not - * @param {string} [options.baseUrl=null] - The base URL to be used instead of the current page's URL - * @param {boolean} [options.keepOthers=false] - Whether to keep existing parameters in the URL hash or not - * @param {Object} [options.history=null] - The history object to push the new URL state into - * @returns {string|void} - The modified URL string if options.returnString is true, otherwise void. + * @param {Object} params - The parameters to be set or updated in the URL hash + * @param {boolean} [keepOthers=false] - Whether to keep existing parameters in the URL hash or not + * @param {Object} [history=undefined] - The history object to be used for navigation + * @returns {void} */ -const setHashSearch = (params = {}, options = {}) => { - const { - returnString = false, - baseUrl = null, - keepOthers = false, - history = null, - } = options; - - const additionalParams = keepOthers ? getHashSearch() : {}; // Retrieve existing parameters if keepOthers is true - const mergedParams = { ...additionalParams, ...params }; // Merge existing and new parameters - const queryString = constructQueryString(mergedParams); // Construct the query string - - // Construct the final URL - const url = (baseUrl || document.location.href).split("?")[0]; - const hrefString = encodeURI(`${url}?${queryString}`); - - if (history) history.push(hrefString); - document.location.href = hrefString; - - // Perform side effects based on options - if (returnString) return hrefString; +const setHashSearch = ( + params = {}, + keepOthers = false, + history = undefined, +) => { + const newUrl = getHashURL(document.location.href, params, keepOthers); + if (history) history.push(newUrl); + document.location.href = newUrl; }; const makeUrlFilterDeserializer = (statusValues, categoryValues) => { @@ -680,7 +682,7 @@ const setConstantHashSearch = (paramString) => { * @param {Object} param The Key-Value pair to be added to the current query param list */ const setParam = (param) => { - setHashSearch({ ...getHashSearch(), ...param }); + setHashSearch(param, true); }; const stringAsBoolean = (str) => str === "true";