|
| 1 | +import expect from 'expect' |
| 2 | +import React, { PropTypes, Component } from 'react' |
| 3 | +import TestUtils from 'react-addons-test-utils' |
| 4 | +import { ThemeProvider } from '../../src/index' |
| 5 | + |
| 6 | +describe('ThemeProvider', () => { |
| 7 | + class Child extends Component { |
| 8 | + render() { |
| 9 | + return <div /> |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + Child.contextTypes = { |
| 14 | + themr: PropTypes.object.isRequired |
| 15 | + } |
| 16 | + |
| 17 | + it('enforces a single child', () => { |
| 18 | + const theme = {} |
| 19 | + |
| 20 | + // Ignore propTypes warnings |
| 21 | + const propTypes = ThemeProvider.propTypes |
| 22 | + ThemeProvider.propTypes = {} |
| 23 | + |
| 24 | + try { |
| 25 | + expect(() => TestUtils.renderIntoDocument( |
| 26 | + <ThemeProvider theme={theme}> |
| 27 | + <div /> |
| 28 | + </ThemeProvider> |
| 29 | + )).toNotThrow() |
| 30 | + |
| 31 | + expect(() => TestUtils.renderIntoDocument( |
| 32 | + <ThemeProvider theme={theme}> |
| 33 | + </ThemeProvider> |
| 34 | + )).toThrow(/exactly one child/) |
| 35 | + |
| 36 | + expect(() => TestUtils.renderIntoDocument( |
| 37 | + <ThemeProvider theme={theme}> |
| 38 | + <div /> |
| 39 | + <div /> |
| 40 | + </ThemeProvider> |
| 41 | + )).toThrow(/exactly one child/) |
| 42 | + } finally { |
| 43 | + ThemeProvider.propTypes = propTypes |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + it('should add the theme to the child context', () => { |
| 48 | + const theme = {} |
| 49 | + |
| 50 | + TestUtils.renderIntoDocument( |
| 51 | + <ThemeProvider theme={theme}> |
| 52 | + <Child /> |
| 53 | + </ThemeProvider> |
| 54 | + ) |
| 55 | + |
| 56 | + const spy = expect.spyOn(console, 'error') |
| 57 | + const tree = TestUtils.renderIntoDocument( |
| 58 | + <ThemeProvider theme={theme}> |
| 59 | + <Child /> |
| 60 | + </ThemeProvider> |
| 61 | + ) |
| 62 | + spy.destroy() |
| 63 | + expect(spy.calls.length).toBe(0) |
| 64 | + |
| 65 | + const child = TestUtils.findRenderedComponentWithType(tree, Child) |
| 66 | + expect(child.context.themr.theme).toBe(theme) |
| 67 | + }) |
| 68 | +}) |
0 commit comments