-
Notifications
You must be signed in to change notification settings - Fork 75
Tests for fixed bug in unsubscribing consumers #8
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,21 @@ import Adapter from 'enzyme-adapter-react-16'; | |
|
||
Enzyme.configure({ adapter: new Adapter() }); | ||
|
||
const maybeHideChildren = (children, hiddenChildren) => | ||
hiddenChildren | ||
? React.Children.toArray(children).filter((child, i) => !hiddenChildren.includes(i)) | ||
: children; | ||
|
||
|
||
type Theme = 'light' | 'dark'; | ||
// Pass a default theme to ensure type correctness | ||
const ThemeContext: Context<Theme> = createReactContext('light'); | ||
|
||
class ThemeToggler extends React.Component< | ||
{ children: Node }, | ||
{ theme: Theme } | ||
{ theme: Theme, hiddenChildren: ?number[] } | ||
> { | ||
state = { theme: 'light' }; | ||
state = { theme: 'light', hiddenChildren: null }; | ||
render() { | ||
return ( | ||
// Pass the current context value to the Provider's `value` prop. | ||
|
@@ -30,7 +36,7 @@ class ThemeToggler extends React.Component< | |
> | ||
Toggle theme | ||
</button> | ||
{this.props.children} | ||
{maybeHideChildren(this.props.children, this.state.hiddenChildren)} | ||
</ThemeContext.Provider> | ||
); | ||
} | ||
|
@@ -72,3 +78,19 @@ test('with provider', () => { | |
wrapper.find('button').simulate('click'); | ||
expect(wrapper).toMatchSnapshot('with provider - after click'); | ||
}); | ||
|
||
test('unsubscribes correct consumer', () => { | ||
const wrapper = mount( | ||
<ThemeToggler> | ||
<Title>Hello World</Title> | ||
<Title>Foo</Title> | ||
<Title>Bar</Title> | ||
</ThemeToggler> | ||
); | ||
|
||
expect(wrapper).toMatchSnapshot(); | ||
wrapper.setState({ hiddenChildren: [1] }); | ||
expect(wrapper).toMatchSnapshot(); | ||
wrapper.find('button').simulate('click'); | ||
expect(wrapper).toMatchSnapshot(); | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not really clear that this tests unsubscribing the right consumer. Instead of modifying the existing component, could you create a new one that wraps two different There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what is the idea behind proposed tweaked test - I mean I'm not sure in what way would you see it tweaked. This test is testing that correct component got unsubscribed (or rather that others are still subscribed). Each |
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 a moment I thought there was no test setup in this package (confused it with unstated repo), so I've started creating one and I've bumped jest version by doing so - can revert that change if you dont like it