Skip to content
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

Merged

Conversation

abepeterkin
Copy link
Collaborator

@abepeterkin abepeterkin commented Feb 13, 2018

BREAKING CHANGE: Anything relying on putting a falsy value into ReducerList will now
throw an error

fix #188

…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
@coveralls
Copy link

coveralls commented Feb 13, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling d31f61a on abepeterkin:bugfix/falsy-values-treated-as-reducers into 1d9801e on ViacomInc:master.

expect(() => factory.create(createReducer, [0])).toThrow()
expect(() => factory.create(createReducer, [undefined])).toThrow()
expect(() => factory.create(createReducer, ['$foo.bar', false])).toThrow()
})
Copy link
Contributor

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

@raingerber
Copy link
Contributor

raingerber commented Feb 13, 2018

Thanks for this PR, @abepeterkin! A few quick notes:

  1. please fill out the Checklist and what/why/how when opening a PR 😀

  2. It looks like you didn't branch off the latest version of master - please update that branch on your fork, merge that into your feature branch, and run the tests again (there shouldn't be any changes here)

Copy link
Collaborator

@paulmolluzzo paulmolluzzo left a 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()
})
Copy link
Collaborator

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

Copy link
Contributor

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
screen shot 2018-02-15 at 11 03 54 am

@abepeterkin
Copy link
Collaborator Author

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', () => {
Copy link
Contributor

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 = []) {
Copy link
Contributor

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?

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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

Copy link
Contributor

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()
})
Copy link
Contributor

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
}

Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

@acatl
Copy link
Collaborator

acatl commented Feb 21, 2018

closing this PR since it hasn't been active, please re-open it once you have it ready

@acatl acatl closed this Feb 21, 2018
Abraham Peterkin added 2 commits February 22, 2018 15:45
Using toThrowErrorMatchingSnapshot() instead of toThrow(), fixed issue where passing null or
undefined as a reducer would result in a weird error message

ViacomInc#212
@abepeterkin abepeterkin reopened this Feb 22, 2018
Abraham Peterkin added 2 commits February 22, 2018 17:08
Whitespace changes in factory.test.js

ViacomInc#212
Removed test case for calling factory.create with one argument

ViacomInc#212
Copy link
Contributor

@raingerber raingerber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@raingerber raingerber changed the title fix(reducer-list): ReducerList factory now throws an error if any fal… fix(reducer-list): ReducerList factory now throws an error for falsy input Feb 23, 2018
@acatl
Copy link
Collaborator

acatl commented Feb 24, 2018

@paulmolluzzo if you have any time to check this PR, looks good to me

Copy link
Collaborator

@paulmolluzzo paulmolluzzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @abepeterkin! 🙌

@acatl acatl merged commit 2c65794 into ViacomInc:master Feb 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Falsy values are treated as reducers
5 participants