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

Export "createBroadcast" #614

Merged
merged 4 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/emotion-theming/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,40 @@ import { channel } from 'emotion-theming';
console.log(channel); '__EMOTION_THEMING__';
```

### createBroadcast: Function

Creates a broadcast that is used to listen to theme events via context. This is probably only useful for testing.
If you have styled components that depend on `theme` via `ThemeProvider`, one option is to wrap all your components being tested
in `ThemeProvider`. However if you're using `enzyme`, you'll lose the ability to call some methods that require it to be run on the root instance.
Instead you can `mount` a component a pass the channel and broadcast as part of `context`.

```js
import {channel, createBroadcast} from 'emotion-theming';
import {mount} from 'enzyme';
import PropTypes from 'prop-types';
import React from 'react';

describe('emotion-theming', function() {
it('can use theme from a ThemeProvider', function() {
const myTheme = {color: 'green'};
const broadcast = createBroadcast(myTheme);
const wrapper = mount(<MyComponent />, {
context: {
[channel]: broadcast,
},
childContextTypes: {
[channel]: PropTypes.object,
},
});

wrapper.setState({foo: 'bar'});
expect(wrapper.state('foo')).toBe('bar');
});
});
```



## Credits

Thanks goes to the [styled-components team](https://github.com/styled-components/styled-components) and [their contributors](https://github.com/styled-components/styled-components/graphs/contributors) who originally wrote this.
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion-theming/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow
export { default as ThemeProvider } from './theme-provider'
export { default as withTheme } from './with-theme'
export { channel, contextTypes } from './utils'
export { channel, contextTypes, createBroadcast } from './utils'
1 change: 1 addition & 0 deletions packages/emotion-theming/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export const contextTypes = {
}

export { default as channel } from './channel'
export { default as createBroadcast } from './create-broadcast'

export type Theme = Object | ((theme: Object) => Object)