-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Fix counter vizualization #4385
Conversation
@@ -0,0 +1,168 @@ | |||
import { getCounterData } from './utils'; |
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.
Not able to stub getCounterData
and other private functions in utils at the moment. Will be addressed in another PR.
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.
Why would you want to stub it?
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.
I meant to say 'getRowNumber
and other private functions'. Stubbing (mocking) would depend on the testing strategy though.
Atm all these 'private' functions including getRowNumber
, numberFormat
, formatValue
etc:
- Contain logic and are not covered. This is fixable by simply exporting and running cases on them.
- From a puristic perspective, when executing tests on
getCounterData
one would want to mock the calls to these private functions to isolate the test subject. Otherwise it's more of an integration testing (which is not too bad anyway). This type of mocking is not an easy one in React as it does not have dependency injection and available solutions are cumbersome or would require changing coding style.
I assume it's ok to test on integration level and gradually add tests for other functions.
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.
I meant to say '
getRowNumber
and other private functions'.
Oh, ok. This makes sense now :-)
I assume it's ok to test on integration level and gradually add tests for other functions.
It's indeed ok. We're not purists here but rather pragmatists :-) So even having just a test for the getCounterData
function is enough considering its scope/size.
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.
We're not purists here but rather pragmatists :-)
Nice one. Aligned.
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.
Thank you for fixing this! Taking the extra effort of adding the tests is appreciated 🙇♂️
@@ -70,31 +70,27 @@ function formatTooltip(value, formatString) { | |||
return toString(value); | |||
} | |||
|
|||
export function getCounterData(rows, options, visualizationName) { | |||
export function getCounterData(rows = [], options = {}, visualizationName = '') { |
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.
Why add the default values here? This function has a very specific usage and will always get these values.
If it's for the tests, then it's better to add the defaults in the tests... Will also make sure we're actually testing real use cases.
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.
I agree refactoring code base for the sole sake of testing is generally not ideal.
rows = []
was meant to protect against calling getCounterData()
at runtime which would trow TypeError on rows.length
(and since there is no type checking at build time). Fair enough such case is not realistic and rows
should be a mandatory param.
Same for options = {}
although the nature of options may suggest they should be optional. In that sense keeping the default value is not just for tests and the same is true for visualizationName = ''
which one may want to fallback to an empty string.
Would following amended function signature do the job here?:
function getCounterData(rows, options = {}, visualizationName = '')
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.
There will always be options and a visualization name... I'm actually not sure about rows
as I don't know what happens when we're still waiting for the results to load. But having a default value won't help in such case anyway, because we definitely pass some value, it just might be empty.
My suggestion is to revert the function definition to be as it was to make it clear that it needs these values, but for convenience in the test you can wrap it with a function that has default 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.
ok cool, default param values removed
@@ -0,0 +1,168 @@ | |||
import { getCounterData } from './utils'; |
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.
Why would you want to stub it?
Am not a huge fan of putting an extreme emphasis on tests but here the code with logic has been begging for decent coverage 🤓. On related note |
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.
{ city: 'Tokyo', population: 38140000 }, | ||
], | ||
options: {}, | ||
visualisationName: 'Visualisation Name', |
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.
visualisation
-> visualization
🙂
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.
Nice catch and both forms are correct in terms of grammar if it's what you meant. These are just test dummies; might as well be foo name. Please feel free to rename to whatever you prefer.
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 was actually to keep it as is in the rest of the codebase, but NP, I'll revisit those tests in #4392.
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.
{ city: 'Tokyo', population: 38140000 }, | ||
], | ||
options: {}, | ||
visualisationName: 'Visualisation Name', |
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 was actually to keep it as is in the rest of the codebase, but NP, I'll revisit those tests in #4392.
Thank you @dmudro for the contribution and patience during the review :-) |
What type of PR is this? (check all applicable)
Description
Small change in behaviour of the counter visualisation as we want to always display a value when count rows option is enabled; even when the value is 0 (i. e. 0 rows in the underlying data set). Introduces tests for logic contained in utils, though there is more to be done in this area (another PR coming as it touches a more general point).
Related Tickets & Documents
Closes #4265.