|
1 | 1 | /* eslint-disable react/prop-types */
|
2 | 2 |
|
3 | 3 | import * as React from 'react'
|
| 4 | +import TestRenderer from 'react-test-renderer' |
4 | 5 | import {renderToString} from 'react-dom/server'
|
5 | 6 | import expect from 'expect.js'
|
6 | 7 | import {stripIndent} from 'common-tags'
|
7 | 8 | import createCommonBaseTests from '../test-utils/createCommonBaseTests'
|
| 9 | +import {getCss, getStyle, removeWhitespace, resetSheets} from '../../../tests/utils' |
8 | 10 | import {createUseStyles, JssProvider, SheetsRegistry} from '.'
|
9 | 11 |
|
10 | 12 | const createStyledComponent = (styles, options) => {
|
@@ -41,4 +43,139 @@ describe('React-JSS: createUseStyles', () => {
|
41 | 43 | `)
|
42 | 44 | })
|
43 | 45 | })
|
| 46 | + |
| 47 | + describe('multiple components that share same hook', () => { |
| 48 | + const useStyles = createUseStyles({ |
| 49 | + item: (props) => ({ |
| 50 | + color: props.active ? 'red' : 'blue', |
| 51 | + '&:hover': { |
| 52 | + fontSize: 60 |
| 53 | + } |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + function Item({active = false, onClick, children}) { |
| 58 | + const classes = useStyles({active}) |
| 59 | + return ( |
| 60 | + <button type="button" className={classes.item} onClick={onClick}> |
| 61 | + {children} |
| 62 | + </button> |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + function App() { |
| 67 | + const [activeKey, setActiveKey] = React.useState(1) |
| 68 | + return ( |
| 69 | + <main> |
| 70 | + {[1, 2].map(key => ( |
| 71 | + <Item key={key} active={key === activeKey} onClick={() => setActiveKey(key)}> |
| 72 | + {key} |
| 73 | + </Item> |
| 74 | + ))} |
| 75 | + </main> |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + beforeEach(resetSheets()) |
| 80 | + |
| 81 | + let registry |
| 82 | + let root |
| 83 | + beforeEach(() => { |
| 84 | + registry = new SheetsRegistry() |
| 85 | + |
| 86 | + TestRenderer.act(() => { |
| 87 | + root = TestRenderer.create( |
| 88 | + <JssProvider registry={registry} generateId={rule => `${rule.key}-id`}> |
| 89 | + <App /> |
| 90 | + </JssProvider> |
| 91 | + ) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe('initial rendering', () => { |
| 96 | + it('should return correct registry.toString', () => { |
| 97 | + expect(registry.toString()).to.be(stripIndent` |
| 98 | + .item-id {} |
| 99 | + .item-d0-id { |
| 100 | + color: red; |
| 101 | + } |
| 102 | + .item-d0-id:hover { |
| 103 | + font-size: 60px; |
| 104 | + } |
| 105 | + .item-d1-id { |
| 106 | + color: blue; |
| 107 | + } |
| 108 | + .item-d1-id:hover { |
| 109 | + font-size: 60px; |
| 110 | + } |
| 111 | + `) |
| 112 | + }) |
| 113 | + |
| 114 | + it('should render correct CSS in DOM', () => { |
| 115 | + const style = getStyle() |
| 116 | + expect(getCss(style)).to.be( |
| 117 | + removeWhitespace(stripIndent` |
| 118 | + .item-id {} |
| 119 | + .item-d0-id { |
| 120 | + color: red; |
| 121 | + } |
| 122 | + .item-d0-id:hover { |
| 123 | + font-size: 60px; |
| 124 | + } |
| 125 | + .item-d1-id { |
| 126 | + color: blue; |
| 127 | + } |
| 128 | + .item-d1-id:hover { |
| 129 | + font-size: 60px; |
| 130 | + } |
| 131 | + `) |
| 132 | + ) |
| 133 | + }) |
| 134 | + }) |
| 135 | + |
| 136 | + describe('update via click', () => { |
| 137 | + beforeEach(() => { |
| 138 | + TestRenderer.act(() => { |
| 139 | + root.root.findAllByType('button')[1].props.onClick() |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + const eachRules = [ |
| 144 | + '.item-id {}', |
| 145 | + stripIndent` |
| 146 | + .item-d0-id { |
| 147 | + color: blue; |
| 148 | + } |
| 149 | + `, |
| 150 | + stripIndent` |
| 151 | + .item-d0-id:hover { |
| 152 | + font-size: 60px; |
| 153 | + } |
| 154 | + `, |
| 155 | + stripIndent` |
| 156 | + .item-d1-id { |
| 157 | + color: red; |
| 158 | + } |
| 159 | + `, |
| 160 | + stripIndent` |
| 161 | + .item-d1-id:hover { |
| 162 | + font-size: 60px; |
| 163 | + } |
| 164 | + ` |
| 165 | + ] |
| 166 | + |
| 167 | + describe('check each rules', () => { |
| 168 | + eachRules.forEach(rule => { |
| 169 | + it(`should contain ${rule} in registry.toString()`, () => { |
| 170 | + expect(registry.toString()).to.contain(rule) |
| 171 | + }) |
| 172 | + |
| 173 | + it(`should render ${rule} in DOM`, () => { |
| 174 | + const style = getStyle() |
| 175 | + expect(getCss(style)).to.contain(removeWhitespace(rule)) |
| 176 | + }) |
| 177 | + }) |
| 178 | + }) |
| 179 | + }) |
| 180 | + }) |
44 | 181 | })
|
0 commit comments