UNPKG

styled-components

Version:

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅

140 lines (127 loc) • 3.52 kB
import React from 'react' import { render } from 'enzyme' import { resetStyled, expectCSSMatches } from './utils' import ThemeProvider from '../models/ThemeProvider' let styled describe('theming', () => { beforeEach(() => { styled = resetStyled() }) it('should inject props.theme into a styled component', () => { const Comp = styled.div` color: ${props => props.theme.color}; ` const theme = { color: 'black' } render( <ThemeProvider theme={theme}> <Comp /> </ThemeProvider> ) expectCSSMatches(`.a { color: ${theme.color}; }`) }) it('should inject props.theme into a styled component multiple levels deep', () => { const Comp = styled.div` color: ${props => props.theme.color}; ` const theme = { color: 'black' } render( <ThemeProvider theme={theme}> <div> <div> <Comp /> </div> </div> </ThemeProvider> ) expectCSSMatches(`.a { color: ${theme.color}; }`) }) it('should properly allow a component to fallback to its default props when a theme is not provided', () => { const Comp1 = styled.div` color: ${props => props.theme.color}; ` Comp1.defaultProps = { theme: { color: "purple" } } render( <div> <Comp1 /> </div> ) expectCSSMatches(`.a { color: purple; }`) }) it('should properly set the theme with an empty object when no teme is provided and no defaults are set', () => { const Comp1 = styled.div` color: ${props => props.theme.color}; ` render( <div> <Comp1 /> </div> ) expectCSSMatches(`.a { color: ; }`) }) it('should only inject props.theme into styled components within its child component tree', () => { const Comp1 = styled.div` color: ${props => props.theme.color}; ` const Comp2 = styled.div` background: ${props => props.theme.color}; ` const theme = { color: 'black' } render( <div> <ThemeProvider theme={theme}> <div> <Comp1 /> </div> </ThemeProvider> <Comp2 /> </div> ) expectCSSMatches(`.a { color: ${theme.color}; } .b { background: ; }`) }) it('should inject props.theme into all styled components within the child component tree', () => { const Comp1 = styled.div` color: ${props => props.theme.color}; ` const Comp2 = styled.div` background: ${props => props.theme.color}; ` const theme = { color: 'black' } render( <ThemeProvider theme={theme}> <div> <div> <Comp1 /> </div> <Comp2 /> </div> </ThemeProvider> ) expectCSSMatches(`.a { color: ${theme.color}; } .b { background: ${theme.color}; }`) }) it('should inject new CSS when the theme changes', () => { const Comp = styled.div` color: ${props => props.theme.color}; ` const originalTheme = { color: 'black' } const newTheme = { color: 'blue' } let theme = originalTheme // Force render the component const renderComp = () => { render( <ThemeProvider theme={theme}> <Comp /> </ThemeProvider> ) } renderComp() const initialCSS = expectCSSMatches(`.a { color: ${theme.color}; }`) // Change the theme theme = newTheme renderComp() expectCSSMatches(`${initialCSS}.b { color: ${newTheme.color}; }`) }) })