Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
fix(theme): nested themes not being republished on outer theme changes (
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and Kent C. Dodds committed Dec 22, 2017
1 parent cb6de3a commit 8cd90bd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/__tests__/__snapshots__/theme-provider.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@ exports[`merges nested themes 1`] = `
</div>
`;

exports[`propagates theme updates through nested ThemeProviders 1`] = `
.glamor-0,
[data-glamor-0] {
background-color: black;
color: red;
}
<ThemeProvider
theme={
Object {
"bg": "black",
}
}
>
<ThemeProvider
theme={[Function]}
>
<glamorous(div)>
<div
className="glamor-0"
/>
</glamorous(div)>
</ThemeProvider>
</ThemeProvider>
`;

exports[`renders a component with theme 1`] = `
.glamor-0,
[data-glamor-0] {
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/theme-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ test('merges nested themes', () => {
).toMatchSnapshot()
})

test('propagates theme updates through nested ThemeProviders', () => {
const theme = {bg: 'white'}
const augment = outerTheme => Object.assign({}, outerTheme, {color: 'red'})
const update = {bg: 'black'}

const Child = glamorous.div(({theme: {bg, color}}) => ({
backgroundColor: bg,
color,
}))

const wrapper = mount(
<ThemeProvider theme={theme}>
<ThemeProvider theme={augment}>
<Child />
</ThemeProvider>
</ThemeProvider>,
)

wrapper.setProps({theme: Object.assign({}, theme, update)})

expect(wrapper).toMatchSnapshot()
})

test('renders if children are null', () => {
expect(
mount(
Expand Down
8 changes: 6 additions & 2 deletions src/theme-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class ThemeProvider extends React.Component {

setOuterTheme = theme => {
this.outerTheme = theme
this.publishTheme()
}

publishTheme(theme) {
this.broadcast.setState(this.getTheme(theme))
}

componentDidMount() {
Expand All @@ -53,13 +58,12 @@ class ThemeProvider extends React.Component {
// set broadcast state by merging outer theme with own
if (this.context[CHANNEL]) {
this.setOuterTheme(this.context[CHANNEL].getState())
this.broadcast.setState(this.getTheme())
}
}

componentWillReceiveProps(nextProps) {
if (this.props.theme !== nextProps.theme) {
this.broadcast.setState(this.getTheme(nextProps.theme))
this.publishTheme(nextProps.theme)
}
}

Expand Down

0 comments on commit 8cd90bd

Please sign in to comment.