-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: refactor the setHash
function
#1006
Conversation
The splitting is definitively improving things already. Maybe we can refactor even further, like pushing the whole URL construction into the utility method? JSDom seems to be a transitive dependency of Jest, and |
Hm, we mock
That seems to work. Maybe try the same? Maybe it also only works in the setup code and not in the test itself? (If you tried out the latter.) |
I have updated the PR in the following manner:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This is already a nice improvement, and I fully agree with your functionality split and testing strategy. I guess it is the best that we can do.
I have just a few minor comments left, and one question: Could you maybe have a look at the callers of setHashSearch
and check whether we can simplify them? IIRC some of them call getHashSearch
and merge values manually, but we could use keepOthers
instead.
While writing this I noticed that at least in ReactTable.js
, we have several calls that first check whether the value of a parameter changes compared to getHashSearch
. We could easily simplify this if we just do nothing in setHashSearch
if the URL happens to remain the same, right? I don't think this would break anything.
benchexec/tablegenerator/react-table/src/components/ReactTable.js
Outdated
Show resolved
Hide resolved
I have updated the PR to address all the review comments. Two noteworthy changes:
|
Nice idea to use Maybe we can even get rid of the |
Makes sense! It would be better to remove the |
I have made the changes as described in the comment above. I have also added a new utility method called PS. I am still a bit unsure if I refactored the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right, setParam
is probably the better choice (or maybe setURLParameter
?). I guess we could rename getHashSearch
then as well, to something like getURLParameters
?
benchexec/tablegenerator/react-table/src/components/ReactTable.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice now and a significant improvement compared to the old code. Exactly what I had hoped for in #807. Thank you very much!
const value = sort.length ? sort : undefined; | ||
const prevParams = getHashSearch(); | ||
if (prevParams["sort"] !== value) { | ||
setHashSearch({ sort: value }, { keepOthers: true }); | ||
} | ||
setURLParameter({ sort: value }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is good that we now document the feature of parameter deletion via undefined
values, because here we can see that it was actually used already!
); | ||
}); | ||
|
||
test("should not remove exisiting parameters if they are updated to falsy values", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice thinking!
Thanks a lot for the continued support and reviews! |
if (history && history.push) { | ||
history.push(newUrl); | ||
} | ||
document.location.href = hrefString; | ||
document.location.href = newUrl; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the record: We overlooked that this changed the behavior and thus broke something: The old method only either set document.location
or pushed to the history, and when pushing to the history it only pushed the search string. A fix is in 3ecba1f.
Related Issue
Fixes #807
Implementation notes
getHashSearch
to increase it's readabilitysetHashSearch
function into two functionssetHashSearch
: Responsible for actually setting the query parameters correctly in the URL based on the configuration options providedconstructQueryString
: Responsible for taking in an object ofparams
and generating a query string from them. This utility offers a layer of abstraction and can be reused in the codebase at many placesRoadblock
The
setHashSeach
function is responsible for creating the correct URL and setting thedocument.location.href
to the same. This poses a problem to us, asJSDom
does not mock routing and thus throws an error when we try to test the same in the unit tests for the function. It throws the error described in this Stack Overflow thread, but all of the solutions mentioned there, as when the issue for the same on the JSDom repository does not fix it. The same can be verified by pulling this PR, commenting out the test suite forsetHashSearch
, and then running the test suite.I could not find the specific version of JSDom we are using in the project in the
package.json
, and I also had trouble understanding the testing setup in the repository. With more context, I can suggest a better approach to tackle this problem.