-
Notifications
You must be signed in to change notification settings - Fork 33
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(reducer-list): ReducerList factory now throws an error for falsy input #212
fix(reducer-list): ReducerList factory now throws an error for falsy input #212
Conversation
…sy, non-string value is passed BREAKING CHANGE: Anything relying on putting a falsy, non-string value into ReducerList will now throw an error fix ViacomInc#188
expect(() => factory.create(createReducer, [0])).toThrow() | ||
expect(() => factory.create(createReducer, [undefined])).toThrow() | ||
expect(() => factory.create(createReducer, ['$foo.bar', false])).toThrow() | ||
}) |
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.
the description implies that strings are treated differently than other types, so we should have at least one test with a string as input. having a test with a falsy string ''
and another test with a string of length > 0
is even better.
why are strings treated differently though? IMO, any falsy input should be treated as an invalid reducer
Thanks for this PR, @abepeterkin! A few quick notes:
|
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.
Would like to see the tests cleaned up, and there is another comment from @raingerber that should be addressed.
expect(() => factory.create(createReducer, [0])).toThrow() | ||
expect(() => factory.create(createReducer, [undefined])).toThrow() | ||
expect(() => factory.create(createReducer, ['$foo.bar', false])).toThrow() | ||
}) |
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.
Could we please break each of these scenarios into a separate test? It makes it easier to read the tested scenarios at a glance and prints out nicer when jest
is run with the --verbose
flag.
For an example, you can take a look at this from #207: https://github.com/raingerber/data-point/blob/e0d3ae139baab9eb40f4591336824b34dd0794f8/packages/data-point/lib/reducer-types/reducer-helpers/utils/index.test.js#L5-L45
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 should really update the contributing guidelines with the points you brought up here and on my PR #207
…hub.com/abepeterkin/data-point into bugfix/falsy-values-treated-as-reducers
I split up the tests. I also added some tests for strings, and it now throws an error if you try to pass an empty string as a reducer. |
expect(() => factory.create(createReducer, [undefined])).toThrow() | ||
}) | ||
|
||
test('factory#throw error if reducer is zero', () => { |
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.
"factory#throw" can be replaced with "should throw" - the #
is normally used to indicate a method/property, like how factory#create
is used because create
is a method of factory
@@ -78,7 +77,7 @@ module.exports.parse = parse | |||
* @param {Array} source | |||
* @return {reducer} | |||
*/ | |||
function create (createReducer, source) { | |||
function create (createReducer, source = []) { |
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 do we need a default parameter here?
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's a test case asserting that calling factory.create with only one argument should result in a ReducerList with an empty set of reducers. But this test broke when removed the calls to _.compact
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 could also take this default parameter out and just change the test
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.
Yeah let's just remove that test instead of adding the default param. Thanks
|
||
test('factory#throw error if reducer is false', () => { | ||
expect(() => factory.create(createReducer, [false])).toThrow() | ||
}) |
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.
can we use toThrowErrorMatchingSnapshot
instead of toThrow
? that way, we can test to be sure that it's throwing an error with a useful message (the error message should be Invalid reducer type. Could not find a matching reducer type while parsing the value
, but I think the current message is Cannot read property 'Symbol(reducer stub)' of undefined
)
To get the correct error message, I think you need to update the isType
function in reducer-stub.js
.
before
function isType (source) {
return source[REDUCER_STUB_SYMBOL] === true
}
after
function isType (source) {
return !!source && source[REDUCER_STUB_SYMBOL] === true
}
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.
can we use toThrowErrorMatchingSnapshot instead of toThrow? that way, we can test to be sure that it's throwing an error with a useful message (the error message should be Invalid reducer type. Could not find a matching reducer type while parsing the value, but I think the current message is Cannot read property 'Symbol(reducer stub)' of undefined)
👍 In fact, it'd be great to have errors snapshotted across the tests.
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.
Great idea, I just added snapshots with the proper error messages.
closing this PR since it hasn't been active, please re-open it once you have it ready |
Using toThrowErrorMatchingSnapshot() instead of toThrow(), fixed issue where passing null or undefined as a reducer would result in a weird error message ViacomInc#212
Whitespace changes in factory.test.js ViacomInc#212
Removed test case for calling factory.create with one argument ViacomInc#212
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!
@paulmolluzzo if you have any time to check this PR, looks good to me |
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.
LGTM. Thanks @abepeterkin! 🙌
BREAKING CHANGE: Anything relying on putting a falsy value into ReducerList will now
throw an error
fix #188